-
Notifications
You must be signed in to change notification settings - Fork 212
/
lib.rs
267 lines (234 loc) Β· 11.5 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! This crate is responsible for parsing, rendering and formatting a Prisma Schema.
//! A Prisma Schema consists out of two parts:
//! 1. The `Datamodel` part refers to the model and enum definitions.
//! 2. The `Configuration` part refers to the generator and datasource definitions.
//!
//! The data structures are organized into 3 layers:
//! * The AST layer contains the data data structures representing the schema input.
//! * The model layer contains the data structures that are semantically rich and therefore engines can build upon.
//! * The JSON layer contains the data structures that represent the contract for the DMMF which is the API for client generators.
//!
//! The responsibilities of each top level module is as following:
//! * `common`: contains constants and generic helpers
//! * `error`: contains the error and result types
//! * `ast`: contains the data structures for the AST of a Prisma schema. And the parsing functions to turn an input string into an AST.
//! * `dml`: contains the models representing the Datamodel part of a Prisma schema
//! * `configuration`: contains the models representing the Datasources and Generators of a Prisma schema
//! * `transform`: contains the logic to turn an AST into models and vice versa
//! * `json`: contains the logic to turn models into their JSON/DMMF representation
//!
//! The flow between the layers is depicted in the following diagram.
//!<pre>
//! ββββββββββββββββββββ
//! β json β
//! ββββββββββ²ββββββββββ
//! β
//! β
//! ββββββββββββββββββββββββββββββββββββββββ
//! β dml ββ configuration β
//! ββββββββββββββββββββββββββββββββββββββββ
//! β β²
//!βββββββββββββββββββββββ β β βββββββββββββββββββββββ
//!βtransform::dml_to_astβ β β βtransform::ast_to_dmlβ
//!βββββββββββββββββββββββ β β βββββββββββββββββββββββ
//! β β
//! βΌ β
//! ββββββββββββββββββββ
//! β ast β
//! ββββββββββββββββββββ
//! β β²
//! β β
//! β β
//! βΌ β
//! ββββββββββββββββββββ
//! β schema string β
//! ββββββββββββββββββββ
//!</pre>
//!
//! The usage dependencies between the main modules is depicted in the following diagram.
//! The modules `error` and `common` are not shown as any module may depend on them.
//!<pre>
//! ββββββββββββββββββββ
//! β transform β
//! ββββββββββββββββββββ
//! β
//! β use
//! ββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
//! β β β
//! β β β
//! βΌ βΌ βΌ
//!ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
//!β ast β β dml β β configuration β
//!ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
//! β² β²
//! β β
//! ββββββββββββββββββββββββββββ
//! β use
//! β
//! ββββββββββββββββββββ
//! β json β
//! ββββββββββββββββββββ
//!</pre>
//!
#![deny(rust_2018_idioms, unsafe_code)]
pub mod ast;
pub mod common;
pub mod diagnostics;
pub mod dml;
pub mod json;
pub mod walkers;
mod configuration;
mod transform;
pub use crate::dml::*;
pub use configuration::{Configuration, Datasource, Generator, StringFromEnvVar};
pub use transform::ast_to_dml::reserved_model_names;
use crate::diagnostics::{Validated, ValidatedConfiguration, ValidatedDatamodel};
use crate::{ast::SchemaAst, common::preview_features::PreviewFeature};
use diagnostics::Diagnostics;
use enumflags2::BitFlags;
use transform::{
ast_to_dml::{DatasourceLoader, GeneratorLoader, ValidationPipeline},
dml_to_ast::{DatasourceSerializer, GeneratorSerializer, LowerDmlToAst},
};
/// Parse and validate the whole schema
pub fn parse_schema(schema_str: &str) -> Result<(Configuration, Datamodel), String> {
parse_datamodel_internal(schema_str, false)
.map_err(|err| err.to_pretty_string("schema.prisma", schema_str))
.map(|v| v.subject)
}
/// Parses and validates a datamodel string, using core attributes only.
pub fn parse_datamodel(datamodel_string: &str) -> Result<ValidatedDatamodel, diagnostics::Diagnostics> {
parse_datamodel_internal(datamodel_string, false).map(|validated| Validated {
subject: validated.subject.1,
warnings: validated.warnings,
})
}
pub fn parse_datamodel_for_formatter(datamodel_string: &str) -> Result<ValidatedDatamodel, diagnostics::Diagnostics> {
parse_datamodel_internal(datamodel_string, true).map(|validated| Validated {
subject: validated.subject.1,
warnings: validated.warnings,
})
}
/// Parses and validates a datamodel string, using core attributes only.
/// In case of an error, a pretty, colorful string is returned.
pub fn parse_datamodel_or_pretty_error(datamodel_string: &str, file_name: &str) -> Result<ValidatedDatamodel, String> {
parse_datamodel_internal(datamodel_string, false)
.map_err(|err| err.to_pretty_string(file_name, datamodel_string))
.map(|validated| Validated {
subject: validated.subject.1,
warnings: validated.warnings,
})
}
fn parse_datamodel_internal(
datamodel_string: &str,
transform: bool,
) -> Result<Validated<(Configuration, Datamodel)>, diagnostics::Diagnostics> {
let mut diagnostics = diagnostics::Diagnostics::new();
let ast = ast::parse_schema(datamodel_string)?;
let generators = GeneratorLoader::load_generators_from_ast(&ast, &mut diagnostics);
let preview_features = preview_features(&generators);
let datasources = load_sources(&ast, preview_features, &mut diagnostics);
diagnostics.to_result()?;
let validator = ValidationPipeline::new(&datasources, preview_features);
match validator.validate(&ast, transform) {
Ok(mut src) => {
src.warnings.append(diagnostics.warnings_mut());
Ok(Validated {
subject: (
Configuration {
generators,
datasources,
},
src.subject,
),
warnings: src.warnings,
})
}
Err(mut err) => {
diagnostics.append(&mut err);
Err(diagnostics)
}
}
}
pub fn parse_schema_ast(datamodel_string: &str) -> Result<SchemaAst, diagnostics::Diagnostics> {
ast::parse_schema(datamodel_string)
}
/// Loads all configuration blocks from a datamodel using the built-in source definitions.
pub fn parse_configuration(schema: &str) -> Result<ValidatedConfiguration, diagnostics::Diagnostics> {
let ast = ast::parse_schema(schema)?;
let mut diagnostics = Diagnostics::default();
let generators = GeneratorLoader::load_generators_from_ast(&ast, &mut diagnostics);
let preview_features = preview_features(&generators);
let datasources = load_sources(&ast, preview_features, &mut diagnostics);
diagnostics.to_result()?;
Ok(ValidatedConfiguration {
subject: Configuration {
generators,
datasources,
},
warnings: diagnostics.warnings().to_owned(),
})
}
fn load_sources(
schema_ast: &SchemaAst,
preview_features: BitFlags<PreviewFeature>,
diagnostics: &mut Diagnostics,
) -> Vec<Datasource> {
DatasourceLoader.load_datasources_from_ast(schema_ast, preview_features, diagnostics)
}
//
// ************** RENDERING FUNCTIONS **************
//
/// Renders to a return string.
pub fn render_datamodel_to_string(datamodel: &dml::Datamodel) -> String {
let mut writable_string = String::with_capacity(datamodel.models.len() * 20);
render_datamodel_to(&mut writable_string, datamodel, None);
writable_string
}
/// Renders an AST to a string.
pub fn render_schema_ast_to_string(schema: &SchemaAst) -> String {
let mut writable_string = String::with_capacity(schema.tops.len() * 20);
render_schema_ast_to(&mut writable_string, schema, 2);
writable_string
}
/// Renders as a string into the stream.
pub fn render_datamodel_to(
stream: &mut dyn std::fmt::Write,
datamodel: &dml::Datamodel,
datasource: Option<&Datasource>,
) {
let lowered = LowerDmlToAst::new(datasource, BitFlags::empty()).lower(datamodel);
render_schema_ast_to(stream, &lowered, 2);
}
/// Renders a datamodel, sources and generators to a string.
pub fn render_datamodel_and_config_to_string(
datamodel: &dml::Datamodel,
config: &configuration::Configuration,
) -> String {
let mut writable_string = String::with_capacity(datamodel.models.len() * 20);
render_datamodel_and_config_to(&mut writable_string, datamodel, config);
writable_string
}
/// Renders a datamodel, generators and sources to a stream as a string.
fn render_datamodel_and_config_to(
stream: &mut dyn std::fmt::Write,
datamodel: &dml::Datamodel,
config: &configuration::Configuration,
) {
let features = config.preview_features().map(Clone::clone).collect();
let mut lowered = LowerDmlToAst::new(config.datasources.first(), features).lower(datamodel);
DatasourceSerializer::add_sources_to_ast(config.datasources.as_slice(), &mut lowered);
GeneratorSerializer::add_generators_to_ast(&config.generators, &mut lowered);
render_schema_ast_to(stream, &lowered, 2);
}
/// Renders as a string into the stream.
fn render_schema_ast_to(stream: &mut dyn std::fmt::Write, schema: &ast::SchemaAst, ident_width: usize) {
let mut renderer = ast::Renderer::new(stream, ident_width);
renderer.render(schema);
}
fn preview_features(generators: &[Generator]) -> BitFlags<PreviewFeature> {
generators
.iter()
.flat_map(|gen| gen.preview_features.iter().cloned())
.collect()
}