Skip to content

Commit f3474c7

Browse files
authored
Add more macros (#20)
1 parent ccd98a8 commit f3474c7

File tree

13 files changed

+392
-145
lines changed

13 files changed

+392
-145
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chemcreator"
3-
version = "1.2.0"
3+
version = "1.2.1"
44
description = "A text-based tool for identifying organic molecules."
55
authors = ["Gavin Tran"]
66
readme = "README.md"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ you try it out and find something named incorrectly, please open an issue!
3636
- [x] Alkenes
3737
- [x] Alkynes
3838
- [x] Alcohols
39-
- [ ] Aldehydes
39+
- [x] Aldehydes
40+
- [x] Amides
41+
- [x] Amines
4042
- [x] Carboxylic acids
4143
- [ ] Ethers
4244
- [x] Ketones
43-
- [ ] Halogenoalkanes
45+
- [x] Halogenoalkanes
4446
- [ ] Esters

src/chain.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ mod tests {
215215
use crate::graph_with;
216216
use crate::molecule::BondOrder::Single;
217217
use crate::molecule::Element::{C, H};
218-
use crate::test_utils::unwrap_atom;
219218
use crate::test_utils::GW::{A, B};
220219

221220
// Also checks accumulate_carbons
@@ -226,7 +225,7 @@ mod tests {
226225
[1, 0; A(C)],
227226
[2, 0; A(C)], [2, 1; A(C)], [2, 2; A(C)],
228227
[3, 0; A(C)],
229-
[4, 0; A(C)]
228+
[4, 0; A(C)],
230229
);
231230
let accumulator = endpoint_head_chains(
232231
Atom {
@@ -251,7 +250,7 @@ mod tests {
251250
[1, 0; A(C)],
252251
[2, 0; A(C)], [2, 1; A(C)], [2, 2; A(C)],
253252
[3, 0; A(C)],
254-
[4, 0; A(C)]
253+
[4, 0; A(C)],
255254
);
256255
let accumulator = endpoint_head_chains(
257256
Atom {
@@ -302,15 +301,15 @@ mod tests {
302301
[1, 0; A(H)],
303302
[1, 1; A(C)],
304303
[1, 2; A(C)],
305-
[2, 1; A(C)]
304+
[2, 1; A(C)],
306305
);
307306
let atoms = next_carbons(Vec2::xy(1, 1), None, &graph).unwrap();
308307
let expected = vec![
309308
graph.get(Vec2::xy(1, 2)).unwrap(),
310309
graph.get(Vec2::xy(2, 1)).unwrap(),
311310
]
312311
.iter()
313-
.map(|&cell| unwrap_atom(cell))
312+
.map(|&cell| cell.unwrap_atom())
314313
.collect::<Vec<Atom>>();
315314

316315
assert_eq!(atoms, expected);
@@ -323,10 +322,10 @@ mod tests {
323322
[1, 0; A(H)],
324323
[1, 1; A(C)],
325324
[1, 2; A(C)],
326-
[2, 1; A(C)]
325+
[2, 1; A(C)],
327326
);
328327
let atom = next_carbons(Vec2::xy(1, 1), Some(Vec2::xy(1, 2)), &graph).unwrap();
329-
let expected = vec![unwrap_atom(graph.get(Vec2::xy(2, 1)).unwrap())];
328+
let expected = vec![graph.get(Vec2::xy(2, 1)).unwrap().unwrap_atom()];
330329

331330
assert_eq!(atom, expected);
332331
}
@@ -340,7 +339,7 @@ mod tests {
340339
[3, 0; A(H)], [3, 1; A(C)], [3, 2; A(H)],
341340
[4, 1; B(Single)],
342341
[5, 0; A(H)], [5, 1; A(C)], [5, 2; A(H)],
343-
[6, 1; A(H)]
342+
[6, 1; A(H)],
344343
);
345344
let cells = endpoint_carbons(&graph).unwrap();
346345
let expected = vec![
@@ -356,7 +355,7 @@ mod tests {
356355
let graph = graph_with!(3, 3,
357356
[0, 1; A(H)],
358357
[1, 0; A(H)], [1, 1; A(C)], [1, 2; A(H)],
359-
[2, 1; A(H)]
358+
[2, 1; A(H)],
360359
);
361360
let cell = endpoint_carbons(&graph).unwrap();
362361
let expected = vec![graph.get(Vec2::xy(1, 1)).unwrap()];
@@ -370,7 +369,7 @@ mod tests {
370369
[0, 0; A(C)],
371370
[1, 0; A(C)],
372371
[2, 0; A(C)],
373-
[0, 2; A(C)]
372+
[0, 2; A(C)],
374373
);
375374
let network = get_connected_cells(Vec2::xy(0, 0), &graph).unwrap();
376375
let expected = vec![

src/groups.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::chain::{endpoint_head_chains, longest_chain};
77
use crate::compound;
88
use crate::groups::InvalidGraphError::{Other, UnrecognizedGroup};
99
use crate::molecule::Group::{
10-
AcidHalide, Aldehyde, Alkene, Alkyne, Amine, Bromo, Carbonyl, Carboxyl, Chloro, Fluoro,
10+
AcidHalide, Aldehyde, Alkene, Alkyne, Amide, Amine, Bromo, Carbonyl, Carboxyl, Chloro, Fluoro,
1111
Hydrogen, Hydroxyl, Iodo, Nitrile,
1212
};
1313
use crate::molecule::Halogen::{Bromine, Chlorine, Fluorine, Iodine};
@@ -131,7 +131,8 @@ fn group_patterns(mut groups: Vec<Group>) -> Vec<Substituent> {
131131
[Carbonyl, Chloro => AcidHalide(Chlorine)],
132132
[Carbonyl, Bromo => AcidHalide(Bromine)],
133133
[Carbonyl, Iodo => AcidHalide(Iodine)],
134-
[Carbonyl, Hydrogen => Aldehyde]
134+
[Carbonyl, Amine => Amide],
135+
[Carbonyl, Hydrogen => Aldehyde],
135136
);
136137
groups.retain(|it| it != &Hydrogen);
137138
break;
@@ -150,7 +151,7 @@ fn group_patterns(mut groups: Vec<Group>) -> Vec<Substituent> {
150151
/// groups to `out`.
151152
#[macro_export]
152153
macro_rules! compound {
153-
($groups:expr, $out:expr, $([$first:expr, $second:expr => $comp:expr]),*) => {
154+
($groups:expr, $out:expr, $([$first:expr, $second:expr => $comp:expr],)*) => {
154155
$(
155156
if $groups.contains(&$first) && $groups.contains(&$second) {
156157
$groups.retain(|it| it != &$first && it != &$second);
@@ -323,7 +324,7 @@ mod tests {
323324
[2, 1; B(Single)],
324325
[3, 0; A(H)], [3, 1; A(C)], [3, 2; A(H)],
325326
[4, 1; A(O)],
326-
[5, 1; A(H)]
327+
[5, 1; A(H)],
327328
);
328329
let chain = vec![
329330
Atom {
@@ -399,7 +400,7 @@ mod tests {
399400
[0, 1; B(Single)],
400401
[0, 2; A(O)],
401402
[0, 3; B(Single)],
402-
[0, 4; A(H)]
403+
[0, 4; A(H)],
403404
);
404405
let a = group_node_tree(&graph, Vec2::xy(0, 0), Direction::Up).unwrap();
405406
let b = GroupNode {
@@ -420,7 +421,7 @@ mod tests {
420421
let graph = graph_with!(1, 3,
421422
[0, 0; A(C)],
422423
[0, 1; A(O)],
423-
[0, 2; A(H)]
424+
[0, 2; A(H)],
424425
);
425426
let node = group_node_tree(&graph, Vec2::xy(0, 0), Direction::Up).unwrap();
426427
let expected = GroupNode {
@@ -441,7 +442,7 @@ mod tests {
441442
let graph = graph_with!(3, 3,
442443
[0, 0; A(H)], [0, 1; A(C)], [0, 2; A(H)],
443444
[1, 1; B(Double)],
444-
[2, 0; A(H)], [2, 1; A(C)], [2, 2; A(H)]
445+
[2, 0; A(H)], [2, 1; A(C)], [2, 2; A(H)],
445446
);
446447
let node = group_node_tree(&graph, Vec2::xy(0, 1), Direction::Right).unwrap();
447448
let expected = GroupNode {
@@ -458,7 +459,7 @@ mod tests {
458459
let graph = graph_with!(3, 3,
459460
[0, 1; A(C)],
460461
[1, 0; A(H)], [1, 1; A(C)],
461-
[2, 1; A(C)]
462+
[2, 1; A(C)],
462463
);
463464
let directions = next_directions(&graph, Vec2::xy(1, 1), Vec2::xy(0, 1)).unwrap();
464465
let expected = vec![Direction::Down, Direction::Right];
@@ -472,7 +473,7 @@ mod tests {
472473
[0, 1; A(C)],
473474
[1, 0; A(H)],
474475
[1, 1; A(C)],
475-
[2, 1; A(C)]
476+
[2, 1; A(C)],
476477
);
477478
let directions = next_directions(&graph, Vec2::xy(1, 1), Vec2::xy(0, 1)).unwrap();
478479
let expected = vec![Direction::Down, Direction::Right];
@@ -485,7 +486,7 @@ mod tests {
485486
let graph = graph_with!(5, 5,
486487
[0, 2; A(C)],
487488
[1, 2; B(Single)],
488-
[2, 0; A(C)], [2, 1; B(Double)], [2, 2; A(C)], [2, 3; A(O)], [2, 4; A(H)]
489+
[2, 0; A(C)], [2, 1; B(Double)], [2, 2; A(C)], [2, 3; A(O)], [2, 4; A(H)],
489490
);
490491
let branch = Branch {
491492
chain: vec![
@@ -514,7 +515,7 @@ mod tests {
514515
[1, 0; A(Br)],
515516
[1, 1; A(C)],
516517
[1, 2; A(I)],
517-
[2, 1; A(F)]
518+
[2, 1; A(F)],
518519
);
519520
let branch = Branch {
520521
chain: vec![Atom {

src/input.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,55 @@
55
use crate::groups::debug_branches;
66
use crate::macros::invoke_macro;
77
use crate::molecule::BondOrder::{Double, Single, Triple};
8-
use crate::molecule::Element;
9-
use crate::molecule::Element::{C, H, N, O};
8+
use crate::molecule::Element::{Cl, C, H, I, N, O};
9+
use crate::molecule::{ComponentType, Element};
1010
use crate::naming::name_molecule;
1111
use crate::spatial::GridState;
1212
use crate::{AppState, Mode};
1313
use ruscii::app::State;
1414
use ruscii::keyboard::{Key, KeyEvent};
1515
use ruscii::spatial::Direction;
16+
use Element::{Br, F};
1617

1718
pub(crate) fn input_insert_mode(app_state: &State, state: &mut AppState, graph: &mut GridState) {
1819
for key_event in app_state.keyboard().last_key_events() {
1920
match key_event {
2021
KeyEvent::Pressed(Key::B) => {
21-
graph.put_atom(Element::Br);
2222
state.key = "B";
23-
update(state, graph);
23+
update(state, graph, ComponentType::Element(Br));
2424
}
2525
KeyEvent::Pressed(Key::C) => {
26-
graph.put_atom(C);
2726
state.key = "C";
28-
update(state, graph);
27+
update(state, graph, ComponentType::Element(C));
2928
}
3029
KeyEvent::Pressed(Key::F) => {
31-
graph.put_atom(Element::F);
3230
state.key = "F";
33-
update(state, graph);
31+
update(state, graph, ComponentType::Element(F));
3432
}
3533
KeyEvent::Pressed(Key::H) => {
36-
graph.put_atom(H);
3734
state.key = "H";
38-
update(state, graph);
35+
update(state, graph, ComponentType::Element(H));
3936
}
4037
KeyEvent::Pressed(Key::I) => {
41-
graph.put_atom(Element::I);
4238
state.key = "I";
43-
update(state, graph);
39+
update(state, graph, ComponentType::Element(I));
4440
}
4541
KeyEvent::Pressed(Key::L) => {
46-
graph.put_atom(Element::Cl);
4742
state.key = "L";
48-
update(state, graph);
43+
update(state, graph, ComponentType::Element(Cl));
4944
}
5045
KeyEvent::Pressed(Key::N) => {
51-
graph.put_atom(N);
5246
state.key = "N";
53-
update(state, graph);
47+
update(state, graph, ComponentType::Element(N));
5448
}
5549
KeyEvent::Pressed(Key::O) => {
56-
graph.put_atom(O);
5750
state.key = "O";
58-
update(state, graph);
51+
update(state, graph, ComponentType::Element(O));
5952
}
6053
KeyEvent::Pressed(Key::F5) => {
6154
graph.clear_all();
6255
state.key = "F5";
63-
update(state, graph);
56+
update(state, graph, ComponentType::None);
6457
}
6558
KeyEvent::Pressed(Key::F7) => {
6659
state.macros_enabled = !state.macros_enabled;
@@ -74,24 +67,20 @@ pub(crate) fn input_insert_mode(app_state: &State, state: &mut AppState, graph:
7467
state.key = "F12";
7568
}
7669
KeyEvent::Pressed(Key::Num1) => {
77-
graph.put_bond(Single);
7870
state.key = "1";
79-
update(state, graph);
71+
update(state, graph, ComponentType::Order(Single));
8072
}
8173
KeyEvent::Pressed(Key::Num2) => {
82-
graph.put_bond(Double);
8374
state.key = "2";
84-
update(state, graph);
75+
update(state, graph, ComponentType::Order(Double));
8576
}
8677
KeyEvent::Pressed(Key::Num3) => {
87-
graph.put_bond(Triple);
8878
state.key = "3";
89-
update(state, graph);
79+
update(state, graph, ComponentType::Order(Triple));
9080
}
9181
KeyEvent::Pressed(Key::Backspace) => {
92-
graph.clear_cell();
9382
state.key = "Backspace";
94-
update(state, graph);
83+
update(state, graph, ComponentType::None);
9584
}
9685
KeyEvent::Pressed(Key::Right) => {
9786
graph.move_cursor(Direction::Right);
@@ -134,9 +123,20 @@ pub(crate) fn input_view_mode(app_state: &State, state: &mut AppState) {
134123
}
135124

136125
//noinspection RsBorrowChecker
137-
pub(crate) fn update(state: &mut AppState, graph: &mut GridState) {
126+
pub(crate) fn update(state: &mut AppState, graph: &mut GridState, comp: ComponentType) {
127+
let previous = graph
128+
.current_cell()
129+
.expect("cell should be within bounds")
130+
.comp();
131+
132+
match comp {
133+
ComponentType::Element(it) => graph.put_atom(it),
134+
ComponentType::Order(it) => graph.put_bond(it),
135+
ComponentType::None => graph.clear_cell(),
136+
}
137+
138138
if state.macros_enabled {
139-
invoke_macro(graph);
139+
invoke_macro(graph, comp, previous);
140140
}
141141

142142
(state.name, state.err) = match name_molecule(graph) {

0 commit comments

Comments
 (0)