/
render_configuration.rs
106 lines (92 loc) 路 3.19 KB
/
render_configuration.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
use ::datamodel::{Configuration, Datasource, Generator, StringFromEnvVar};
use schema_ast::string_literal;
use std::fmt::{self, Write as _};
pub(crate) fn render_configuration(config: &Configuration, out: &mut String) {
for generator in &config.generators {
render_generator(generator, out).unwrap();
}
for source in &config.datasources {
render_datasource(source, out).unwrap();
}
}
fn render_datasource(datasource: &Datasource, out: &mut String) -> fmt::Result {
if let Some(docs) = &datasource.documentation {
super::render_documentation(docs, false, out);
}
writeln!(
out,
"datasource {} {{\nprovider = {}",
datasource.name,
string_literal(datasource.active_provider)
)?;
out.push_str("url = ");
render_string_from_env(&datasource.url, out)?;
out.push('\n');
if let Some((shadow_database_url, _)) = &datasource.shadow_database_url {
out.push_str("shadowDatabaseUrl = ");
render_string_from_env(shadow_database_url, out)?;
out.push('\n');
}
if let Some(referential_integrity) = datasource.referential_integrity {
writeln!(
out,
"referentialIntegrity = {}",
string_literal(&referential_integrity.to_string())
)?;
}
out.write_str("}\n")
}
fn render_generator(generator: &Generator, out: &mut String) -> fmt::Result {
if let Some(docs) = &generator.documentation {
super::render_documentation(docs, false, out);
}
out.push_str("generator ");
out.push_str(&generator.name);
out.push_str(" {\n");
// Provider
{
out.push_str("provider = \"");
out.push_str(generator.provider.as_literal().unwrap());
out.push_str("\"\n");
}
if let Some(output) = &generator.output {
out.push_str("output = ");
render_string_from_env(output, out)?;
out.push('\n');
}
if let Some(ref features) = &generator.preview_features {
let mut feats = features.iter().peekable();
out.push_str("previewFeatures = [");
while let Some(feature) = feats.next() {
write!(out, "{}", string_literal(&feature.to_string()))?;
if feats.peek().is_some() {
out.push(',');
}
}
out.push_str("]\n");
}
if !generator.binary_targets.is_empty() {
let mut targets = generator.binary_targets.iter().peekable();
out.push_str("binaryTargets = [");
while let Some(target) = targets.next() {
render_string_from_env(target, out)?;
if targets.peek().is_some() {
out.push(',');
}
}
out.push_str("]\n");
}
for (key, value) in &generator.config {
writeln!(out, "{key} = {}", string_literal(value))?;
}
out.write_str("}\n")
}
fn render_string_from_env(string_from_env: &StringFromEnvVar, out: &mut String) -> fmt::Result {
match &string_from_env.from_env_var {
Some(var_name) => out.write_fmt(format_args!("env({})", string_literal(var_name))),
None => out.write_fmt(format_args!(
"{}",
string_literal(string_from_env.value.as_ref().unwrap())
)),
}
}