feos_core/parameter/
segment.rs

1use super::ParameterError;
2use serde::de::DeserializeOwned;
3use serde::{Deserialize, Serialize};
4use std::fs::File;
5use std::hash::{Hash, Hasher};
6use std::io::BufReader;
7use std::path::Path;
8
9/// Parameters describing an individual segment of a molecule.
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct SegmentRecord<M> {
12    pub identifier: String,
13    pub molarweight: f64,
14    pub model_record: M,
15}
16
17impl<M> SegmentRecord<M> {
18    /// Creates a new `SegmentRecord`.
19    pub fn new(identifier: String, molarweight: f64, model_record: M) -> Self {
20        Self {
21            identifier,
22            molarweight,
23            model_record,
24        }
25    }
26
27    /// Read a list of `SegmentRecord`s from a JSON file.
28    pub fn from_json<P: AsRef<Path>>(file: P) -> Result<Vec<Self>, ParameterError>
29    where
30        M: DeserializeOwned,
31    {
32        Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
33    }
34}
35
36impl<M> Hash for SegmentRecord<M> {
37    fn hash<H: Hasher>(&self, state: &mut H) {
38        self.identifier.hash(state);
39    }
40}
41
42impl<M> PartialEq for SegmentRecord<M> {
43    fn eq(&self, other: &Self) -> bool {
44        self.identifier == other.identifier
45    }
46}
47impl<M> Eq for SegmentRecord<M> {}
48
49impl<M: std::fmt::Display> std::fmt::Display for SegmentRecord<M> {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "SegmentRecord(\n\tidentifier={}", self.identifier)?;
52        write!(f, "\n\tmolarweight={}", self.molarweight)?;
53        write!(f, "\n\tmodel_record={}", self.model_record)?;
54        write!(f, "\n)")
55    }
56}