Expand description
§MON AST to Serializable Value Conversion
This module provides the logic for converting a resolved MON Abstract Syntax Tree (AST)
into a generic, serializable data structure. This is the final step in the pipeline
before the MON data can be output as JSON, YAML, or any other format supported by serde.
§Architectural Overview
The process is straightforward:
- A fully resolved and validated
MonValuefrom the AST is passed to the internalto_valuefunction. - The function recursively traverses the
MonValue, converting it into a tree ofValueenums. - During this conversion, language-specific AST nodes that are not part of the data model—such
as
TypeDefinitionmembers—are discarded. OnlyPairmembers are included in the final object. - The resulting
Valueis designed to be directly serializable byserde. It uses aBTreeMapfor objects to ensure that the output has a deterministic order of keys, which is good practice for configuration and data files.
§Use Cases
This module is used internally by AnalysisResult to provide
the to_json() and to_yaml() methods. Direct interaction with this module is typically not
necessary for end-users, as the public API in the api module provides a more
convenient interface.
use mon_core::api::analyze;
let source = "{ b: 2, a: 1 }";
// The serialization module is used behind the scenes by `to_json`.
let result = analyze(source, "test.mon")?;
let json = result.to_json().unwrap();
// Note that the keys are sorted alphabetically because of the BTreeMap.
assert_eq!(json, "{\n \"a\": 1.0,\n \"b\": 2.0\n}");