Skip to content

Commit

Permalink
refactor(cli): additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Apr 9, 2024
1 parent 858cc74 commit b48664d
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions three-style-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ impl Cli {
depth,
}) = self.command
{
let allowed_moves = gen
.chars()
.map(|c| MoveKind::from_str(&c.to_string()))
.collect::<Result<Vec<_>, _>>()?;
let start = Instant::now();
let commutators = match (corners, edges) {
(Some(corners), None) => search_corner_commutators(corners, gen, depth)?,
(None, Some(edges)) => search_edge_commutators(edges, gen, depth)?,
(Some(corners), None) => search_corner_commutators(corners, allowed_moves, depth)?,
(None, Some(edges)) => search_edge_commutators(edges, allowed_moves, depth)?,
_ => unreachable!(),
};
let end = Instant::now();
Expand Down Expand Up @@ -74,37 +78,29 @@ enum Command {

fn search_corner_commutators(
corners: Vec<String>,
gen: String,
allowed_moves: Vec<MoveKind>,
depth: u8,
) -> Result<Vec<Commutator>, Error> {
let corners = corners
.into_iter()
.map(|c| Corner::from_str(&c))
.collect::<Result<Vec<_>, _>>()?;
let cycle = Cycle::new(corners[0], corners[1], corners[2]);
let allowed_moves = gen
.chars()
.map(|c| MoveKind::from_str(&c.to_string()))
.collect::<Result<Vec<_>, _>>()?;
let results = find_corner_commutators(cycle, &allowed_moves, depth);

Ok(results)
}

fn search_edge_commutators(
edges: Vec<String>,
gen: String,
allowed_moves: Vec<MoveKind>,
depth: u8,
) -> Result<Vec<Commutator>, Error> {
let edges = edges
.into_iter()
.map(|c| Edge::from_str(&c))
.collect::<Result<Vec<_>, _>>()?;
let cycle = Cycle::new(edges[0], edges[1], edges[2]);
let allowed_moves = gen
.chars()
.map(|c| MoveKind::from_str(&c.to_string()))
.collect::<Result<Vec<_>, _>>()?;
let results = find_edge_commutators(cycle, &allowed_moves, depth);

Ok(results)
Expand Down Expand Up @@ -137,7 +133,7 @@ fn print_commutators(commutators: Vec<Commutator>, duration: Duration) {
}
}

fn print_error(error: String) {
fn print_error(error: Error) {
let style = Style::new()
.bold()
.fg_color(Some(Color::Ansi(AnsiColor::Red)));
Expand All @@ -150,7 +146,7 @@ fn main() {
let result = cli.exec();

if let Err(error) = result {
print_error(error.to_string());
print_error(error);
process::exit(1);
}
}

0 comments on commit b48664d

Please sign in to comment.