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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::{cmp::Ordering, fmt::Display, marker::PhantomData};
use nalgebra::Point3;
use crate::{builder_typestate::No, ModelInfo};
use super::AtomCollection;
pub trait BuildState {}
pub struct Ready {}
impl BuildState for Ready {}
impl BuildState for No {}
pub struct AtomCollectionBuilder<T, S>
where
T: ModelInfo,
S: BuildState,
{
element_symbols: Option<Vec<String>>,
atomic_nums: Option<Vec<u8>>,
xyz_coords: Option<Vec<Point3<f64>>>,
fractional_xyz: Option<Vec<Option<Point3<f64>>>>,
atom_ids: Option<Vec<u32>>,
size: usize,
format_type: T,
state: PhantomData<S>,
}
#[derive(Debug)]
pub enum AtomCollectionBuildingError {
InconsistentSize { curr: usize, expected: usize },
MissingField { missed: String },
}
impl Display for AtomCollectionBuildingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AtomCollectionBuildingError::InconsistentSize { curr, expected } => write!(
f,
"InconsistentSize: current: {} vs expected: {}",
curr, expected
),
AtomCollectionBuildingError::MissingField { missed: miss } => {
write!(f, "MissingField: {} value is `None`.", miss)
}
}
}
}
impl<T: ModelInfo, S: BuildState> AtomCollectionBuilder<T, S> {
pub fn new(size: usize) -> AtomCollectionBuilder<T, No> {
AtomCollectionBuilder {
element_symbols: None,
atomic_nums: None,
xyz_coords: None,
fractional_xyz: None,
atom_ids: None,
size,
format_type: T::default(),
state: PhantomData,
}
}
pub fn with_element_symbols(
mut self,
element_symbols: &[String],
) -> Result<Self, AtomCollectionBuildingError> {
match element_symbols.len().cmp(&self.size) {
Ordering::Equal => {
self.element_symbols = Some(element_symbols.to_vec());
Ok(self)
}
_ => Err(AtomCollectionBuildingError::InconsistentSize {
curr: element_symbols.len(),
expected: self.size,
}),
}
}
pub fn with_atomic_nums(
mut self,
atomic_nums: &[u8],
) -> Result<Self, AtomCollectionBuildingError> {
match atomic_nums.len().cmp(&self.size) {
Ordering::Equal => {
self.atomic_nums = Some(atomic_nums.to_vec());
Ok(self)
}
_ => Err(AtomCollectionBuildingError::InconsistentSize {
curr: atomic_nums.len(),
expected: self.size,
}),
}
}
pub fn with_xyz_coords(
mut self,
xyz_coords: &[Point3<f64>],
) -> Result<Self, AtomCollectionBuildingError> {
match xyz_coords.len().cmp(&self.size) {
Ordering::Equal => {
self.xyz_coords = Some(xyz_coords.to_vec());
Ok(self)
}
_ => Err(AtomCollectionBuildingError::InconsistentSize {
curr: xyz_coords.len(),
expected: self.size,
}),
}
}
pub fn with_fractional_xyz(
mut self,
fractional_xyz: &[Option<Point3<f64>>],
) -> Result<Self, AtomCollectionBuildingError> {
match fractional_xyz.len().cmp(&self.size) {
Ordering::Equal => {
self.fractional_xyz = Some(fractional_xyz.to_vec());
Ok(self)
}
_ => Err(AtomCollectionBuildingError::InconsistentSize {
curr: fractional_xyz.len(),
expected: self.size,
}),
}
}
pub fn with_atom_ids(mut self, atom_ids: &[u32]) -> Result<Self, AtomCollectionBuildingError> {
match atom_ids.len().cmp(&self.size) {
Ordering::Equal => {
self.atom_ids = Some(atom_ids.to_vec());
Ok(self)
}
_ => Err(AtomCollectionBuildingError::InconsistentSize {
curr: atom_ids.len(),
expected: self.size,
}),
}
}
pub fn finish(self) -> Result<AtomCollectionBuilder<T, Ready>, AtomCollectionBuildingError> {
if self.atomic_nums.is_none() {
return Err(AtomCollectionBuildingError::MissingField {
missed: "atomic_nums".into(),
});
}
if self.element_symbols.is_none() {
return Err(AtomCollectionBuildingError::MissingField {
missed: "element_symbols".into(),
});
}
if self.xyz_coords.is_none() {
return Err(AtomCollectionBuildingError::MissingField {
missed: "xyz_coords".into(),
});
}
if self.fractional_xyz.is_none() {
return Err(AtomCollectionBuildingError::MissingField {
missed: "fractional_xyz".into(),
});
}
if self.atom_ids.is_none() {
return Err(AtomCollectionBuildingError::MissingField {
missed: "atom_ids".into(),
});
}
let Self {
element_symbols,
atomic_nums,
xyz_coords,
fractional_xyz,
atom_ids,
size,
format_type,
state: _,
} = self;
Ok(AtomCollectionBuilder {
element_symbols,
atomic_nums,
xyz_coords,
fractional_xyz,
atom_ids,
size,
format_type,
state: PhantomData,
})
}
}
impl<T: ModelInfo> AtomCollectionBuilder<T, Ready> {
pub fn build(self) -> AtomCollection<T> {
AtomCollection {
element_symbols: self.element_symbols.unwrap(),
atomic_nums: self.atomic_nums.unwrap(),
xyz_coords: self.xyz_coords.unwrap(),
fractional_xyz: self.fractional_xyz.unwrap(),
atom_ids: self.atom_ids.unwrap(),
size: self.size,
format_type: T::default(),
}
}
}