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
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Single,
Double,
Triple,
Wedge,
WedgeBack,
Hash,
HashBack,
Hollow,
HollowBack,
}
impl Kind {
pub fn is_cram(&self) -> bool {
!matches!(self, Kind::Single | Kind::Double | Kind::Triple)
}
pub fn is_reversed(&self) -> bool {
matches!(self, Kind::WedgeBack | Kind::HashBack | Kind::HollowBack)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Angle {
Default,
Absolute(f64),
Relative(f64),
}
pub const DEFAULT_ANGLE: Angle = Angle::Default;
#[derive(Debug, Clone, PartialEq)]
pub struct Bond {
pub kind: Kind,
pub angle: Angle,
pub length: f64,
pub from: Option<usize>,
pub to: Option<usize>,
pub position: usize,
}
impl Bond {
pub fn new(kind: Kind, position: usize) -> Bond {
Bond {kind, angle: DEFAULT_ANGLE, length: 1.0, from: None, to: None, position}
}
pub fn is_placed(&self) -> bool {
self.angle != DEFAULT_ANGLE || self.length != 1.0
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Hook {
pub name: String,
pub kind: Option<Kind>,
pub position: usize,
}
pub const DEFAULT_HOOK: &str = "a";
#[derive(Debug, Clone, PartialEq)]
pub struct Atom {
pub source: String,
pub hook: Vec<Hook>,
pub position: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Ring {
pub size: usize,
pub circle: bool,
pub bond: Vec<Bond>,
pub member: Vec<Node>,
pub position: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
pub atom: Atom,
pub branch: Vec<(Bond, Node)>,
pub ring: Option<Ring>,
}
impl Node {
pub fn new(source: impl Into<String>, position: usize) -> Node {
Node {
atom: Atom {source: source.into(), hook: Vec::new(), position},
branch: Vec::new(),
ring: None,
}
}
}
pub fn count(node: &Node) -> usize {
let branch: usize = node.branch.iter().map(|(_, child)| count(child)).sum();
let ring: usize = match &node.ring {
Some(found) => found.member.iter().map(count).sum(),
None => 0,
};
1 + branch + ring
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_the_atoms_of_a_tree() {
let mut root = Node::new("C", 0);
root.branch.push((Bond::new(Kind::Single, 1), Node::new("H", 2)));
root.branch.push((Bond::new(Kind::Double, 3), Node::new("O", 4)));
assert_eq!(count(&root), 3);
}
#[test]
fn counts_the_atoms_of_a_ring() {
let mut root = Node::new("", 0);
root.ring = Some(Ring {
size: 6,
circle: false,
bond: (0..6).map(|at| Bond::new(Kind::Single, at)).collect(),
member: (1..6).map(|at| Node::new("", at)).collect(),
position: 0,
});
assert_eq!(count(&root), 6);
}
#[test]
fn a_bond_without_brackets_goes_east() {
let bond = Bond::new(Kind::Single, 0);
assert_eq!(bond.angle, Angle::Default);
assert_eq!(bond.length, 1.0);
assert_eq!(bond.from, None);
assert!(!bond.is_placed());
}
#[test]
fn the_cram_bonds_know_their_wide_side() {
assert!(Kind::Wedge.is_cram());
assert!(!Kind::Single.is_cram());
assert!(!Kind::Wedge.is_reversed());
assert!(Kind::WedgeBack.is_reversed());
assert!(Kind::HashBack.is_reversed());
assert!(!Kind::Hollow.is_reversed());
}
}