sdf_parser_core/
lib.rs

1pub mod config;
2pub use parser::*;
3
4pub mod parser {
5
6    use schemars::{schema::Schema, JsonSchema};
7    use serde::{Deserialize, Serialize};
8
9    #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
10    #[serde(untagged)]
11    pub enum MaybeValid<U> {
12        Valid(U),
13        Invalid(serde_yaml::Value),
14    }
15
16    impl<U> MaybeValid<U> {
17        pub fn is_invalid(&self) -> bool {
18            matches!(self, Self::Invalid(_))
19        }
20
21        pub fn valid_data(&self) -> Option<&U> {
22            match self {
23                Self::Valid(data) => Some(data),
24                Self::Invalid(_) => None,
25            }
26        }
27    }
28
29    // we implement this way to avoid json schema generated a schema for the invalid case
30    impl<U: JsonSchema> JsonSchema for MaybeValid<U> {
31        fn schema_name() -> String {
32            U::schema_name()
33        }
34
35        fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> Schema {
36            U::json_schema(generator)
37        }
38    }
39}