geode/
model.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#![allow(unused)]
//! The `model` module contains the core definitions for geoscience physics modeling.
//!
//! - `Physics`: Represents different types of physical models.
//! - `CompositionalPhysics`: A specific model for compositional systems.

pub mod domain;
pub mod material;
pub use material::Material;
use schemars::JsonSchema;

use std::{collections::HashMap, str::FromStr};

use self::domain::Domain;
use compositional::CompositionalPhysics;
use serde::{Deserialize, Serialize};

mod compositional {
    use crate::parse::quantity::*;
    use std::collections::HashMap;

    use schemars::JsonSchema;
    use serde::{Deserialize, Serialize};

    /// Represents a component in a compositional physics model, such as a
    /// molecule or chemical species.
    #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, JsonSchema)]
    pub struct Component {
        pub mass: MolarMass,
    }

    /// # Compositional Physics
    ///
    /// Describe compositional physics YEAH
    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
    pub struct CompositionalPhysics {
        pub components: HashMap<String, Component>,
        pub phases: Vec<String>,
    }
}

/// Hello Lyss, you're such a bg.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
pub struct Physics {
    compositional: CompositionalPhysics,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
pub struct GeoscienceModel {
    pub physics: Physics,
    pub domain: Domain,
    #[serde(default)]
    pub materials: HashMap<String, Option<Material>>,
}

impl FromStr for GeoscienceModel {
    type Err = serde_yaml::Error;

    /// Converts a YAML formatted string into a `GeoscienceModel`.
    /// This allows the use of the `FromStr` trait to parse YAML strings
    /// directly into the model, with graceful error handling.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_yaml(s)
    }
}

impl GeoscienceModel {
    /// Constructs a `GeoscienceModel` from a YAML string input using Serde.
    /// It attempts to deserialize the input and returns a Result with either
    /// a `GeoscienceModel` instance on success, or a `serde_yaml::Error` on failure.
    fn from_yaml(s: &str) -> Result<Self, serde_yaml::Error> {
        Ok(serde_yaml::from_str(s)?)
    }
}