powerio_tnd/formats/
ravens.rs1pub use powerio_core::cim::ravens::{
23 BALANCED_MARKERS, MULTICONDUCTOR_MARKERS, NAMESPACE, Profile, TOKEN, detect_profile,
24};
25use powerio_core::{Error, FormatId, PioModule, Source};
26use powerio_dist::MulticonductorNetwork;
27use powerio_tx::BalancedNetwork;
28
29#[derive(Debug)]
32pub enum RavensModule {
33 Balanced(PioModule<BalancedNetwork>),
35 Multiconductor(PioModule<MulticonductorNetwork>),
37}
38
39impl RavensModule {
40 #[must_use]
42 pub fn profile(&self) -> Profile {
43 match self {
44 Self::Balanced(_) => Profile::Balanced,
45 Self::Multiconductor(_) => Profile::Multiconductor,
46 }
47 }
48}
49
50pub fn profile_of(source: &Source) -> Result<Profile, Error> {
55 let buffer = source.primary_buffer()?;
56 let text = std::str::from_utf8(buffer.content_bytes()).map_err(|e| {
57 Error::new(
58 &powerio_tx::diagnostics::codes::PARSE_SOURCE_MALFORMED,
59 format!("MG-RAVENS JSON is not UTF-8 text: {e}"),
60 )
61 })?;
62 Ok(detect_profile(text))
63}
64
65pub fn parse(source: Source) -> Result<RavensModule, Error> {
75 let source = source.with_format(FormatId::new(TOKEN)?);
76 match profile_of(&source) {
77 Ok(Profile::Multiconductor) => {
78 powerio_dist::parse(source).map(RavensModule::Multiconductor)
79 }
80 Ok(Profile::Mixed) => Err(Error::new(
81 &crate::diagnostics::PARSE_RAVENS_MIXED_PROFILE,
82 "the document carries classes of both MG-RAVENS profiles (the balanced \
83 AlgorithmSettings, ProducerCostFunction, or PowerTransformerEnd beside per-phase \
84 or tank classes); no reader takes both, and reading the two parts into an \
85 IntegratedNetwork is not implemented",
86 )
87 .with_source(source)),
88 Ok(_) => powerio_tx::parse(source).map(RavensModule::Balanced),
89 Err(error) => Err(error.with_source(source)),
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 fn memory(text: &str) -> Source {
98 Source::from_memory("case.json", text.as_bytes().to_vec()).unwrap()
99 }
100
101 #[test]
102 fn a_mixed_document_is_refused_with_its_own_code() {
103 let text = r#"{
104 "ConnectivityNode": {"a": {"Ravens.cimObjectType": "ConnectivityNode"}},
105 "PerLengthLineParameter": {},
106 "MySettings": {"Ravens.cimObjectType": "AlgorithmSettings"}
107 }"#;
108 let error = parse(memory(text)).unwrap_err();
109 assert_eq!(
110 error.info().map(|info| info.code),
111 Some("PARSE.RAVENS.MIXED_PROFILE"),
112 "{error}"
113 );
114 }
115
116 #[test]
117 fn the_multiconductor_example_reads_as_a_multiconductor_module() {
118 let path = concat!(
119 env!("CARGO_MANIFEST_DIR"),
120 "/../tests/data/dist/ravens/case3_balanced.json"
121 );
122 let module = parse(Source::open(path).unwrap()).unwrap();
123 assert_eq!(module.profile(), Profile::Multiconductor);
124 let RavensModule::Multiconductor(module) = module else {
125 unreachable!("profile checked");
126 };
127 assert_eq!(module.value().buses().len(), 4);
129 assert_eq!(
130 module
131 .source()
132 .and_then(|s| s.format())
133 .map(FormatId::as_str),
134 Some(TOKEN)
135 );
136 }
137}