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
use std::str::FromStr; use std::string::ToString; use error::*; pub type Real = f32; #[derive(Debug, PartialEq, Clone)] pub struct Atom { pub element: String, pub x: Real, pub y: Real, pub z: Real, } impl Atom { pub fn new(element: &str, x: Real, y: Real, z: Real) -> Self { Atom { element: element.to_string(), x: x, y: y, z: z, } } } impl FromStr for Atom { type Err = Error; fn from_str(s: &str) -> Result<Self> { let splitted: Vec<&str> = s.split_whitespace().collect(); if splitted.len() != 4 { return Err(Error::IllegalState(String::from(""))); } Ok(Atom::new(splitted[0], splitted[1].parse::<Real>()?, splitted[2].parse::<Real>()?, splitted[3].parse::<Real>()?)) } } impl ToString for Atom { fn to_string(&self) -> String { let string_list = vec![ self.element.clone(), self.x.to_string(), self.y.to_string(), self.z.to_string(), ]; string_list.join(" ") } } pub struct Snapshot { pub comment: String, pub atoms: Vec<Atom> } impl Snapshot { pub fn size(&self) -> usize { self.atoms.len() } } #[cfg(test)] mod tests { use super::*; #[test] fn test_parse_atom() { let success = "C 10.0 11.0 12.0".parse::<Atom>(); assert!(success.is_ok()); assert_eq!( Atom::new("C", 10.0, 11.0, 12.0), success.unwrap()); let failure = "C 1.0 2.0 a".parse::<Atom>(); assert!(failure.is_err()); } #[test] fn test_atom_to_string() { let atom = Atom::new("C", 11.2, 8.5, 14.8); assert_eq!("C 11.2 8.5 14.8", atom.to_string()); } #[test] fn test_snapshot() { let snapshot = Snapshot { comment: "This is a comment".to_string(), atoms: vec![ Atom::new("C", 10.0, 11.0, 12.0), Atom::new("O", 8.4, 12.8, 5.0), Atom::new("H", 23.0, 9.0, 11.8), ] }; assert_eq!(3, snapshot.size()); assert_eq!("This is a comment", snapshot.comment); assert_eq!(Atom::new("C", 10.0, 11.0, 12.0), snapshot.atoms[0]); assert_eq!(Atom::new("O", 8.4, 12.8, 5.0), snapshot.atoms[1]); assert_eq!(Atom::new("H", 23.0, 9.0, 11.8), snapshot.atoms[2]); } }