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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use super::ParameterError;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum IdentifierOption {
Cas,
Name,
IupacName,
Smiles,
Inchi,
Formula,
}
impl TryFrom<&str> for IdentifierOption {
type Error = ParameterError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let lower = s.to_lowercase();
match lower.as_str() {
"cas" => Ok(Self::Cas),
"name" => Ok(Self::Name),
"iupacname" => Ok(Self::IupacName),
"smiles" => Ok(Self::Smiles),
"inchi" => Ok(Self::Inchi),
"formula" => Ok(Self::Formula),
_ => Err(ParameterError::IdentifierNotFound(s.to_owned())),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Identifier {
pub cas: String,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub iupac_name: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub smiles: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub inchi: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub formula: Option<String>,
}
impl Identifier {
pub fn new(
cas: &str,
name: Option<&str>,
iupac_name: Option<&str>,
smiles: Option<&str>,
inchi: Option<&str>,
formula: Option<&str>,
) -> Identifier {
Identifier {
cas: cas.to_owned(),
name: name.map(Into::into),
iupac_name: iupac_name.map(Into::into),
smiles: smiles.map(Into::into),
inchi: inchi.map(Into::into),
formula: formula.map(Into::into),
}
}
pub fn as_string(&self, option: IdentifierOption) -> Option<String> {
match option {
IdentifierOption::Cas => Some(self.cas.clone()),
IdentifierOption::Name => self.name.clone(),
IdentifierOption::IupacName => self.iupac_name.clone(),
IdentifierOption::Smiles => self.smiles.clone(),
IdentifierOption::Inchi => self.inchi.clone(),
IdentifierOption::Formula => self.formula.clone(),
}
}
}
impl std::fmt::Display for Identifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Identifier(cas={}", self.cas)?;
if let Some(n) = &self.name {
write!(f, ", name={}", n)?;
}
if let Some(n) = &self.iupac_name {
write!(f, ", iupac_name={}", n)?;
}
if let Some(n) = &self.smiles {
write!(f, ", smiles={}", n)?;
}
if let Some(n) = &self.inchi {
write!(f, ", inchi={}", n)?;
}
if let Some(n) = &self.formula {
write!(f, ", formula={}", n)?;
}
write!(f, ")")
}
}
impl PartialEq for Identifier {
fn eq(&self, other: &Self) -> bool {
self.cas == other.cas
}
}
impl Eq for Identifier {}
impl Hash for Identifier {
fn hash<H: Hasher>(&self, state: &mut H) {
self.cas.hash(state);
}
}