1mod data;
4mod errors;
5mod generators;
6mod map_generation;
7mod parser;
8
9pub use crate::data::{DomainData, DomainInfo};
10use crate::parser::{generated_domain::RootElementType, get_root, parse_domain};
11pub use cmtool_data::PhaseCM; pub use data::{FeedFlow, ParsedFeeds};
13pub use errors::CMError;
14pub use generators::GenerateContract;
15use map_generation::generate_flowmap;
16pub type ConnectionType = [cmtool_data::RawDataFlux; 2];
17
18pub struct Parser(RootElementType);
21
22impl Parser {
23 pub fn start_parsing(reactor_content: &str) -> Result<(String, Self), CMError> {
26 let root = get_root(reactor_content)?;
27 Ok((root.run_id.clone(), Self(root)))
28 }
29
30 fn continue_parsing(
33 p: Parser,
34 root_dir: impl AsRef<std::path::Path>,
35 ) -> Result<(DomainData, Option<GenerateContract>), CMError> {
36 let path = root_dir.as_ref().join(&p.0.run_id);
37 Self::continue_parsing_with_path(p, path)
38 }
39
40 pub fn continue_parsing_with_path(
43 Parser(root): Parser,
44 root_dir: impl AsRef<std::path::Path>,
45 ) -> Result<(DomainData, Option<GenerateContract>), CMError> {
46 let (mut domain, mb, connections) = parse_domain(&root)?;
47
48 let gc = generate_flowmap(None, &root.reactors, &mb, connections)?;
51 let path = root_dir.as_ref().to_owned();
52
53 domain.case_path = path.as_os_str().to_string_lossy().to_string();
54 Ok((domain, gc))
55 }
56}
57
58pub fn generate_domain(
61 root_dir: impl AsRef<std::path::Path>,
62 reactor_content: &str,
63) -> Result<(DomainData, Option<GenerateContract>), CMError> {
64 let (_id, parser) = Parser::start_parsing(reactor_content)?;
65
66 Parser::continue_parsing(parser, root_dir)
67}
68
69pub fn generate_and_write_domain(
72 root_dir: impl AsRef<std::path::Path>,
73 reactor_content: &str,
74) -> Result<DomainData, CMError> {
75 let (domain, gc) = generate_domain(&root_dir, reactor_content)?;
76 if let Some(contract) = gc {
77 contract.write(root_dir)?;
78 }
79 Ok(domain)
80}
81
82pub fn headless_generate(
85 reactor_input_file_name: &str,
86 path: impl AsRef<std::path::Path>,
87) -> Result<(), CMError> {
88 let contents = std::fs::read_to_string(reactor_input_file_name)?;
89 let root_dir = path.as_ref().to_str().expect("path str").to_owned();
92 std::fs::create_dir_all(&root_dir)?;
93 generate_domain(&root_dir, &contents)?;
94 Ok(())
95}