Skip to main content

morphir_config/
parse.rs

1//! Content-based parsing for supported Morphir configuration formats.
2
3use crate::legacy::{LegacyProjectConfig, normalize_legacy_config};
4use serde_json::Value;
5use serde_saphyr::budget::BudgetBreach;
6use serde_saphyr::granit_parser::{Event, Parser, ScalarStyle};
7use serde_saphyr::options::{DuplicateKeyPolicy, MergeKeyPolicy};
8use serde_saphyr::{Error as YamlError, alias_limits, budget, options};
9use thiserror::Error;
10
11const MAX_YAML_INPUT_BYTES: usize = 512 * 1024 * 1024;
12
13/// A failure to parse or normalize configuration content.
14#[derive(Debug, Error)]
15pub enum ConfigParseError {
16    /// The configuration name does not identify a supported serialization.
17    #[error("Unsupported Morphir config format for {name} (expected .toml, .yaml, .yml, or .json)")]
18    UnsupportedFormat {
19        /// Name used to select the serialization.
20        name: String,
21    },
22    /// TOML parsing failed.
23    #[error("Failed to parse TOML config {name}: {source}")]
24    Toml {
25        /// Configuration name.
26        name: String,
27        /// Parser error.
28        #[source]
29        source: toml::de::Error,
30    },
31    /// TOML could not be normalized into JSON.
32    #[error("Failed to normalize TOML config {name}: {source}")]
33    NormalizeToml {
34        /// Configuration name.
35        name: String,
36        /// Normalization error.
37        #[source]
38        source: serde_json::Error,
39    },
40    /// Strict YAML parsing or validation failed.
41    #[error("Failed to parse YAML config {name}: {message}")]
42    Yaml {
43        /// Configuration name.
44        name: String,
45        /// Human-readable reason.
46        message: String,
47    },
48    /// Legacy JSON parsing failed.
49    #[error("Failed to parse legacy JSON config {name}: {source}")]
50    LegacyJson {
51        /// Configuration name.
52        name: String,
53        /// Parser error.
54        #[source]
55        source: serde_json::Error,
56    },
57}
58
59/// Parse configuration content based on its name without accessing a filesystem.
60///
61/// TOML and YAML inputs are normalized into JSON values. JSON inputs use the
62/// legacy `morphir.json` shape and are normalized to the current configuration
63/// structure.
64pub fn parse_config(name: &str, content: &str) -> Result<Value, ConfigParseError> {
65    let extension = name
66        .rsplit_once('.')
67        .map(|(_, extension)| extension.to_ascii_lowercase());
68
69    match extension.as_deref() {
70        Some("toml") => parse_toml(name, content),
71        Some("yaml" | "yml") => parse_yaml(name, content),
72        Some("json") => parse_legacy_json(name, content),
73        _ => Err(ConfigParseError::UnsupportedFormat {
74            name: name.to_owned(),
75        }),
76    }
77}
78
79fn parse_toml(name: &str, content: &str) -> Result<Value, ConfigParseError> {
80    let value =
81        toml::from_str::<toml::Value>(content).map_err(|source| ConfigParseError::Toml {
82            name: name.to_owned(),
83            source,
84        })?;
85    serde_json::to_value(value).map_err(|source| ConfigParseError::NormalizeToml {
86        name: name.to_owned(),
87        source,
88    })
89}
90
91fn parse_yaml(name: &str, content: &str) -> Result<Value, ConfigParseError> {
92    validate_yaml_profile(name, content)?;
93    let value = serde_saphyr::from_str_with_options(content, yaml_options())
94        .map_err(|error| yaml_parse_error(name, &error))?;
95    validate_yaml_value(name, &value, true)?;
96    Ok(value)
97}
98
99fn parse_legacy_json(name: &str, content: &str) -> Result<Value, ConfigParseError> {
100    let legacy = serde_json::from_str::<LegacyProjectConfig>(content).map_err(|source| {
101        ConfigParseError::LegacyJson {
102            name: name.to_owned(),
103            source,
104        }
105    })?;
106    Ok(normalize_legacy_config(legacy))
107}
108
109fn yaml_options() -> serde_saphyr::Options {
110    options! {
111        budget: budget! {
112            max_reader_input_bytes: Some(MAX_YAML_INPUT_BYTES),
113            max_events: 50_000_000,
114            max_aliases: 0,
115            max_anchors: 0,
116            max_depth: 512,
117            max_documents: 1,
118            max_nodes: 20_000_000,
119            max_total_scalar_bytes: MAX_YAML_INPUT_BYTES,
120            max_merge_keys: 0,
121        },
122        duplicate_keys: DuplicateKeyPolicy::Error,
123        merge_keys: MergeKeyPolicy::Error,
124        alias_limits: alias_limits! {
125            max_total_replayed_events: 0,
126            max_replay_stack_depth: 0,
127            max_alias_expansions_per_anchor: 0,
128        },
129        emit_comments: false,
130        strict_booleans: true,
131        legacy_octal_numbers: false,
132        reject_non_finite_typeless_float: true,
133        with_snippet: false,
134    }
135}
136
137fn yaml_parse_error(name: &str, error: &YamlError) -> ConfigParseError {
138    let message = match error.without_snippet() {
139        YamlError::DuplicateMappingKey { .. } => "YAML config must not contain duplicate keys",
140        YamlError::MultipleDocuments { .. }
141        | YamlError::Budget {
142            breach: BudgetBreach::Documents { .. },
143            ..
144        } => "YAML config must contain exactly one document",
145        YamlError::MergeKeyNotAllowed { .. } => "YAML config must not use merge keys",
146        YamlError::TaggedScalarCannotDeserializeIntoString { .. }
147        | YamlError::TaggedEnumMismatch { .. } => "YAML config must not contain custom tags",
148        YamlError::AliasReplayCounterOverflow { .. }
149        | YamlError::AliasReplayLimitExceeded { .. }
150        | YamlError::AliasExpansionLimitExceeded { .. }
151        | YamlError::AliasReplayStackDepthExceeded { .. }
152        | YamlError::AliasError { .. }
153        | YamlError::Budget {
154            breach: BudgetBreach::Aliases { .. } | BudgetBreach::Anchors { .. },
155            ..
156        } => "YAML config must not contain anchors or aliases",
157        _ => return yaml_error(name, error.to_string()),
158    };
159    match error.location() {
160        Some(location) => yaml_error(
161            name,
162            format!(
163                "{message} at line {}, column {}",
164                location.line(),
165                location.column()
166            ),
167        ),
168        None => yaml_error(name, message),
169    }
170}
171
172fn validate_yaml_profile(name: &str, content: &str) -> Result<(), ConfigParseError> {
173    let parser_options = serde_saphyr::granit_parser::options! {
174        emit_comments: false,
175    };
176    let mut document_count = 0;
177    for parsed in Parser::new_from_str_with_options(content, parser_options) {
178        let (event, span) = parsed.map_err(|error| yaml_error(name, error.to_string()))?;
179        if matches!(event, Event::DocumentStart(..)) {
180            document_count += 1;
181            if document_count > 1 {
182                return Err(yaml_profile_error(
183                    name,
184                    "YAML config must contain exactly one document",
185                    span.start.line(),
186                    span.start.col(),
187                ));
188            }
189        }
190        if event.anchor_id().is_some() || event.alias_id().is_some() {
191            return Err(yaml_profile_error(
192                name,
193                "YAML config must not contain anchors or aliases",
194                span.start.line(),
195                span.start.col(),
196            ));
197        }
198        if event.tag().is_some() {
199            let marker = span.tag_start.unwrap_or(span.start);
200            return Err(yaml_profile_error(
201                name,
202                "YAML config must not contain custom tags",
203                marker.line(),
204                marker.col(),
205            ));
206        }
207        if event.scalar().is_some_and(|(value, style)| {
208            style == ScalarStyle::Plain && looks_like_timestamp(value)
209        }) {
210            return Err(yaml_profile_error(
211                name,
212                "YAML config must quote timestamp-like scalars",
213                span.start.line(),
214                span.start.col(),
215            ));
216        }
217    }
218    Ok(())
219}
220
221fn validate_yaml_value(name: &str, value: &Value, at_root: bool) -> Result<(), ConfigParseError> {
222    match value {
223        Value::Null => Err(yaml_error(name, "YAML config must not contain null values")),
224        Value::Object(mapping) => mapping
225            .values()
226            .try_for_each(|value| validate_yaml_value(name, value, false)),
227        Value::Array(values) if at_root => {
228            let _ = values;
229            Err(yaml_error(name, "YAML config root must be a mapping"))
230        }
231        Value::Array(values) => values
232            .iter()
233            .try_for_each(|value| validate_yaml_value(name, value, false)),
234        _ if at_root => Err(yaml_error(name, "YAML config root must be a mapping")),
235        _ => Ok(()),
236    }
237}
238
239fn yaml_error(name: &str, message: impl Into<String>) -> ConfigParseError {
240    ConfigParseError::Yaml {
241        name: name.to_owned(),
242        message: message.into(),
243    }
244}
245
246fn yaml_profile_error(name: &str, message: &str, line: usize, column: usize) -> ConfigParseError {
247    yaml_error(
248        name,
249        format!("{message} at line {line}, column {}", column + 1),
250    )
251}
252
253fn looks_like_timestamp(token: &str) -> bool {
254    let bytes = token.as_bytes();
255    bytes.len() == 10
256        && bytes[0..4].iter().all(u8::is_ascii_digit)
257        && bytes[4] == b'-'
258        && bytes[5..7].iter().all(u8::is_ascii_digit)
259        && bytes[7] == b'-'
260        && bytes[8..10].iter().all(u8::is_ascii_digit)
261}
262
263#[cfg(test)]
264mod tests {
265    use super::*;
266
267    #[test]
268    fn parses_toml_yaml_and_legacy_json_without_a_filesystem() {
269        let toml =
270            parse_config("morphir.toml", "[project]\nname='acme/orders'\nversion='1'").unwrap();
271        let yaml = parse_config(
272            "morphir.yaml",
273            "project:\n  name: acme/orders\n  version: '1'\n",
274        )
275        .unwrap();
276        let legacy = parse_config(
277            "morphir.json",
278            r#"{"name":"acme/orders","sourceDirectory":"src","exposedModules":[]}"#,
279        )
280        .unwrap();
281
282        assert_eq!(toml["project"]["name"], "acme/orders");
283        assert_eq!(yaml["project"]["name"], "acme/orders");
284        assert_eq!(legacy["project"]["version"], "0.1.0");
285    }
286
287    #[test]
288    fn rejects_yaml_aliases_tags_duplicate_keys_nulls_and_multiple_documents() {
289        for source in [
290            "defaults: &d { language: elm }\nfrontend: *d\n",
291            "frontend: !custom elm\n",
292            "project: {}\nproject: {}\n",
293            "project: null\n",
294            "project: {}\n---\nproject: {}\n",
295        ] {
296            assert!(
297                parse_config("morphir.yaml", source).is_err(),
298                "accepted {source:?}"
299            );
300        }
301    }
302
303    #[test]
304    fn normalized_yaml_errors_preserve_parser_locations() {
305        for (source, expected_reason) in [
306            (
307                "project:\n  name: first\n  name: second\n",
308                "YAML config must not contain duplicate keys",
309            ),
310            (
311                "project:\n  name: orders\n  <<: {version: '1'}\n",
312                "YAML config must not use merge keys",
313            ),
314        ] {
315            let error = parse_config("located.yaml", source).expect_err("invalid YAML");
316            let diagnostic = error.to_string();
317
318            assert!(diagnostic.contains(expected_reason), "{diagnostic}");
319            assert!(
320                diagnostic.contains("line 3, column 3"),
321                "missing parser location in {diagnostic}"
322            );
323        }
324    }
325
326    #[test]
327    fn rejects_actual_yaml_properties_and_ambiguous_complete_scalars() {
328        for source in [
329            "value: !custom text\n",
330            "value: !!str text\n",
331            "value: &anchor text\n",
332            "value: *alias\n",
333            "value:\n  <<: {nested: true}\n",
334            "value: .inf\n",
335            "value: 2026-08-30\n",
336        ] {
337            assert!(
338                parse_config("strict.yaml", source).is_err(),
339                "accepted {source:?}"
340            );
341        }
342    }
343
344    #[test]
345    fn allows_forbidden_token_text_in_quoted_and_block_scalars() {
346        let config = parse_config(
347            "quoted.yaml",
348            "project:\n  name: 'acme/!*&'\n  version: '2026-08-30'\n  description: |\n    Literal * alias, ! tag, & anchor, and 2026-08-30 text.\n",
349        )
350        .unwrap();
351
352        assert_eq!(config["project"]["name"], "acme/!*&");
353    }
354
355    #[test]
356    fn allows_forbidden_tokens_after_doubled_single_quote_escapes() {
357        for (source, expected) in [
358            ("description: 'it''s ! text'\n", "it's ! text"),
359            ("description: 'it''s * text'\n", "it's * text"),
360        ] {
361            let config = parse_config("quoted.yaml", source).unwrap();
362            assert_eq!(config["description"], expected);
363        }
364    }
365
366    #[test]
367    fn allows_yaml_indicator_characters_and_dates_inside_plain_scalars() {
368        for (source, expected) in [
369            ("description: hello ! world\n", "hello ! world"),
370            ("description: rock & roll\n", "rock & roll"),
371            ("description: a * character\n", "a * character"),
372            (
373                "description: released 2026-08-30 successfully\n",
374                "released 2026-08-30 successfully",
375            ),
376        ] {
377            let config = parse_config("plain.yaml", source).unwrap();
378            assert_eq!(config["description"], expected);
379        }
380    }
381
382    #[test]
383    fn parses_a_long_plain_scalar_without_line_sized_scanner_collections() {
384        let description = format!(
385            "{} ! & * released 2026-08-30 successfully",
386            "a".repeat(1024 * 1024)
387        );
388        let source = format!("description: {description}\n");
389
390        let config = parse_config("long-line.yaml", &source).unwrap();
391
392        assert_eq!(
393            config["description"].as_str().map(str::len),
394            Some(description.len())
395        );
396        assert!(
397            config["description"]
398                .as_str()
399                .is_some_and(|value| value.ends_with("2026-08-30 successfully"))
400        );
401    }
402
403    #[test]
404    fn every_parse_diagnostic_names_its_input() {
405        for (name, source) in [
406            ("broken.toml", "[project"),
407            ("broken.yaml", "project: null\n"),
408            ("broken.json", "{"),
409            ("broken.txt", "project = {}"),
410        ] {
411            let error = parse_config(name, source).expect_err("invalid configuration");
412            assert!(error.to_string().contains(name), "{error}");
413        }
414    }
415}