Skip to content

Commit

Permalink
Clean up and remove mockall. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pumken authored Mar 18, 2023
1 parent a0bbefe commit c2251f7
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 421 deletions.
146 changes: 1 addition & 145 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chemcreator"
version = "1.1.0"
version = "0.2.0"
description = "A text-based tool for identifying organic molecules."
authors = ["Gavin Tran"]
readme = "README.md"
Expand All @@ -13,6 +13,3 @@ repository = "https://github.com/pumken/chemcreator"
[dependencies]
ruscii = "0.3.2"
thiserror = "1.0.38"

[dev-dependencies]
mockall = "0.11.3"
13 changes: 4 additions & 9 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ use crate::pointer::Pointer;
use crate::spatial::GridState;
use ruscii::spatial::Vec2;

pub(crate) fn debug_chain(graph: &GridState) -> Fallible<Vec<Atom>> {
let all_chains = get_all_chains(graph)?;
longest_chain(all_chains)
}

/// Gets the longest of the given [`Vec`] of chains, assuming that it is non-empty.
///
/// ## Errors
Expand Down Expand Up @@ -122,7 +117,7 @@ fn create_branches(
/// If one of the bonds to the current cell is found to be dangling, an
/// [`IncompleteBond`] will be returned.
fn next_carbons(pos: Vec2, previous_pos: Option<Vec2>, graph: &GridState) -> Fallible<Vec<Atom>> {
let ptr = Pointer { graph, pos };
let ptr = Pointer::new(graph, pos);
let mut out = ptr.bonded_carbons()?;

if let Some(it) = previous_pos {
Expand All @@ -146,7 +141,7 @@ pub(crate) fn endpoint_carbons(graph: &GridState) -> Fallible<Vec<&Cell>> {
let mut out = vec![];

for carbon in all_carbons {
let ptr = Pointer::new(carbon, graph);
let ptr = Pointer::new(graph, carbon.pos());
if ptr.bonded_carbon_count()? <= 1 {
out.push(carbon);
}
Expand All @@ -168,7 +163,7 @@ pub(crate) fn endpoint_carbons(graph: &GridState) -> Fallible<Vec<&Cell>> {
pub(crate) fn get_connected_cells(pos: Vec2, graph: &GridState) -> Fallible<Vec<Vec<bool>>> {
if let Cell::None(_) = graph
.get(pos)
.expect("pos should be a valid point on the graph.")
.expect("pos should be a valid point on the graph")
{
panic!(
"Passed empty cell ({}, {}) to get_connected_cells",
Expand All @@ -185,7 +180,7 @@ pub(crate) fn get_connected_cells(pos: Vec2, graph: &GridState) -> Fallible<Vec<
graph: &GridState,
) -> Fallible<()> {
accumulator[pos.x as usize][pos.y as usize] = true;
let ptr = Pointer { graph, pos };
let ptr = Pointer::new(graph, pos);
for cell in ptr.connected() {
if let Some(it) = previous_pos {
if cell.pos() == it {
Expand Down
52 changes: 27 additions & 25 deletions src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::spatial::{FromVec2, GridState};
use ruscii::spatial::{Direction, Vec2};
use thiserror::Error;

/// Generates a [`Branch`] from the given `chain` containing all functional groups attached
/// to it.
pub(crate) fn link_groups(graph: &GridState, chain: Vec<Atom>) -> Fallible<Branch> {
let mut branch = Branch::new(chain);

Expand Down Expand Up @@ -50,11 +52,10 @@ pub(crate) fn debug_branches(graph: &GridState) -> Fallible<Branch> {
///
/// If a given [`GroupNode`] is not recognized, [`UnrecognizedGroup`] will be returned.
fn convert_nodes(group_nodes: Vec<GroupNode>) -> Fallible<Vec<Substituent>> {
let mut groups = vec![];

for node in group_nodes {
groups.push(identify_single_bond_group(node)?)
}
let groups = group_nodes
.into_iter()
.map(identify_single_bond_group)
.collect::<Fallible<Vec<Group>>>()?;

let new_groups = group_patterns(groups);

Expand All @@ -68,13 +69,12 @@ fn convert_nodes(group_nodes: Vec<GroupNode>) -> Fallible<Vec<Substituent>> {
/// Returns [`UnrecognizedGroup`] if the structure is not valid.
fn identify_single_bond_group(node: GroupNode) -> Fallible<Group> {
let string = node.to_string();
let id = string.as_str();

let out = match id {
let out = match string.as_str() {
"1O(1H)" => Hydroxyl,
"2O" => Carbonyl,
_ => return Err(UnrecognizedGroup),
};

Ok(out)
}

Expand All @@ -90,6 +90,7 @@ fn group_patterns(mut groups: Vec<Group>) -> Vec<Substituent> {
}
break;
}

let mut rest = groups
.into_iter()
.map(Substituent::Group)
Expand All @@ -115,7 +116,7 @@ pub(crate) fn group_node_tree(
pos: Vec2,
direction: Direction,
) -> Fallible<GroupNode> {
let ptr = Pointer { graph, pos };
let ptr = Pointer::new(graph, pos);
let bond = ptr.bond_order(direction).unwrap();
let atom = ptr.traverse_bond(direction)?;
let mut next = vec![];
Expand All @@ -140,18 +141,22 @@ pub(crate) fn group_node_tree(
/// points to a valid [`Cell::Atom`], and that there are no dangling bonds. If any of these
/// contracts are broken, this function will panic.
fn next_directions(graph: &GridState, pos: Vec2, previous_pos: Vec2) -> Fallible<Vec<Direction>> {
let ptr = Pointer { graph, pos };
let bonded = ptr.bonded()?;
let mut out = vec![];

for atom in bonded {
let result = match Direction::from_points(pos, atom.pos) {
Ok(it) => it,
Err(_) => return Err(Other("An unexpected error occurred (G151).")),
};
out.push(result)
}
out.retain(|&dir| dir != Direction::from_points(pos, previous_pos).unwrap());
let ptr = Pointer::new(graph, pos);
let out = ptr
.bonded()?
.into_iter()
.map(|atom| {
Direction::from_points(pos, atom.pos)
.map_err(|_| Other("An unexpected error occurred (groups/next_directions)."))
})
.filter(|dir| {
if let Ok(it) = dir {
it != &Direction::from_points(pos, previous_pos).unwrap()
} else {
true
}
})
.collect::<Fallible<Vec<Direction>>>()?;

Ok(out)
}
Expand All @@ -168,10 +173,7 @@ fn group_directions(
accumulator: &Branch,
index: usize,
) -> Fallible<Vec<Direction>> {
let ptr = Pointer {
graph,
pos: accumulator.chain[index].pos,
};
let ptr = Pointer::new(graph, accumulator.chain[index].pos);
let directions = ptr
.connected_directions()
.into_iter()
Expand Down
Loading

0 comments on commit c2251f7

Please sign in to comment.