-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsegment.rs
More file actions
56 lines (50 loc) · 1.57 KB
/
segment.rs
File metadata and controls
56 lines (50 loc) · 1.57 KB
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
use super::ParameterError;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::BufReader;
use std::path::Path;
/// Parameters describing an individual segment of a molecule.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SegmentRecord<M> {
pub identifier: String,
pub molarweight: f64,
pub model_record: M,
}
impl<M> SegmentRecord<M> {
/// Creates a new `SegmentRecord`.
pub fn new(identifier: String, molarweight: f64, model_record: M) -> Self {
Self {
identifier,
molarweight,
model_record,
}
}
/// Read a list of `SegmentRecord`s from a JSON file.
pub fn from_json<P: AsRef<Path>>(file: P) -> Result<Vec<Self>, ParameterError>
where
M: DeserializeOwned,
{
Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
}
}
impl<M> Hash for SegmentRecord<M> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.identifier.hash(state);
}
}
impl<M> PartialEq for SegmentRecord<M> {
fn eq(&self, other: &Self) -> bool {
self.identifier == other.identifier
}
}
impl<M> Eq for SegmentRecord<M> {}
impl<M: std::fmt::Display> std::fmt::Display for SegmentRecord<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SegmentRecord(\n\tidentifier={}", self.identifier)?;
write!(f, "\n\tmolarweight={}", self.molarweight)?;
write!(f, "\n\tmodel_record={}", self.model_record)?;
write!(f, "\n)")
}
}