Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
str::FromStr,
};

use anyhow::{Result, anyhow};
use anyhow::{Result, ensure};
use itertools::{Itertools, Position, iproduct};

use crate::{
Expand Down Expand Up @@ -349,13 +349,12 @@ impl FromStr for Board {
let lines = s.trim().lines().collect::<Vec<_>>();
let size = lines.len();
for (line_num, &line) in lines.iter().enumerate() {
if line.len() != size {
let row_num = line_num + 1;
let row_len = line.len();
return Err(anyhow!(
"Invalid board: row {row_num} has {row_len} entries but the board is {size} rows long."
));
}
ensure!(
line.len() == size,
"Invalid board: row {row_num} has {row_len} entries but the board is {size} rows long.",
row_num = line_num + 1,
row_len = line.len()
);
}
let colors = lines
.into_iter()
Expand Down
30 changes: 14 additions & 16 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt::Display, str::FromStr};

use anyhow::{Context, Result, anyhow};
use anyhow::{Context, Result, ensure};
use image::ImageReader;

use crate::{
Expand Down Expand Up @@ -57,13 +57,12 @@ impl FromStr for InputSquares {
let lines = s.trim().lines().collect::<Vec<_>>();
let size = lines.len();
for (line_num, &line) in lines.iter().enumerate() {
if line.len() != size {
let row_num = line_num + 1;
let row_len = line.len();
return Err(anyhow!(
"Invalid solve state squares: row {row_num} has {row_len} entries but the board is {size} rows long."
));
}
ensure!(
line.len() == size,
"Invalid solve state squares: row {row_num} has {row_len} entries but the board is {size} rows long.",
row_num = line_num + 1,
row_len = line.len()
);
}
let solve_state_squares = lines
.into_iter()
Expand Down Expand Up @@ -115,16 +114,15 @@ impl FromStr for QueensFile {
fn from_str(s: &str) -> Result<Self> {
let lines = s.trim().lines().collect::<Vec<_>>();
let lines_len = lines.len();
if lines_len == 0 {
return Err(anyhow!("Invalid solve state: no lines found."));
}
ensure!(lines_len != 0, "Invalid solve state: no lines found.");

let size = lines[0].len();
let is_squares_formatted = lines_len == (1 + size * 2) && lines[size].is_empty();
if lines_len != size && !is_squares_formatted {
return Err(anyhow!(
"Invalid solve state: {lines_len} lines for size {size}."
));
}
ensure!(
lines_len == size || is_squares_formatted,
"Invalid solve state: {lines_len} lines for size {size}."
);

let board = Board::from_str(&lines[0..size].join("\n"))?;
let squares = if is_squares_formatted {
Some(InputSquares::from_str(
Expand Down
4 changes: 2 additions & 2 deletions src/solvestate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter, Write};

use anyhow::{Result, anyhow};
use anyhow::{Result, bail};
use clap::ValueEnum;
use itertools::{Itertools, Position};
use log::trace;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl SquareVal {
' ' => Ok(None),
'.' => Ok(None),
'_' => Ok(None),
_ => Err(anyhow!("Blank")),
_ => bail!("Blank"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/squarecolor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter};

use anyhow::{Result, anyhow};
use anyhow::{Result, bail};
use owo_colors::AnsiColors;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -94,7 +94,7 @@ impl TryFrom<char> for SquareColor {
'M' => Ok(SquareColor::BrightMagenta),
'C' => Ok(SquareColor::BrightCyan),
'W' => Ok(SquareColor::BrightWhite),
_ => Err(anyhow!("Unknown color char: {}", value)),
_ => bail!("Unknown color char: {}", value),
}
}
}
Expand Down