balena_cdsl/dsl/schema/compiler/
mod.rs

1//! Compile `serde_yaml`'s representation into a compiled schema that then can be used in `Generator`, using `compile` function.
2use yaml_merge_keys::merge_keys_serde;
3
4use crate::dsl::schema::deserialization::deserialize_root;
5use crate::dsl::schema::DocumentRoot;
6
7pub fn compile(schema: serde_yaml::Value) -> Result<CompiledSchema, CompilationError> {
8    let schema = merge_keys_serde(schema)?;
9    let schema = deserialize_root(&schema)?;
10    Ok(CompiledSchema::with(schema))
11}
12
13pub struct CompiledSchema {
14    schema: DocumentRoot,
15}
16
17#[derive(Debug)]
18pub struct CompilationError {
19    message: String,
20}
21
22impl CompilationError {
23    pub fn with_message(message: &str) -> CompilationError {
24        CompilationError {
25            message: message.to_string(),
26        }
27    }
28}
29
30impl From<serde_yaml::Error> for CompilationError {
31    fn from(source: serde_yaml::Error) -> Self {
32        CompilationError {
33            message: source.to_string(),
34        }
35    }
36}
37
38impl From<yaml_merge_keys::Error> for CompilationError {
39    fn from(source: yaml_merge_keys::Error) -> Self {
40        CompilationError {
41            message: source.to_string(),
42        }
43    }
44}
45
46impl CompiledSchema {
47    pub fn with(schema: DocumentRoot) -> CompiledSchema {
48        CompiledSchema { schema }
49    }
50
51    pub fn compiled(self) -> DocumentRoot {
52        self.schema
53    }
54}