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
2 changes: 1 addition & 1 deletion src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ mod tests {
let board = board_result.unwrap();
assert_eq!(board.size(), 4);
assert_eq!(board.square_count(), 16);
assert_eq!(format!("{}", board), board_str);
assert_eq!(format!("{board}"), board_str);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/datastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ mod tests {
assert!(!sqs.is_empty());
assert!(sqs.contains(&SquareColor::Black));
assert!(!sqs.contains(&SquareColor::Red));
assert_eq!(format!("{}", sqs), "[Black, Blue, White]");
assert_eq!(format!("{sqs}"), "[Black, Blue, White]");
}

#[test]
Expand All @@ -422,7 +422,7 @@ mod tests {
assert!(ls.contains(&0));
assert!(!ls.contains(&1));
assert_eq!(ls.iter().collect::<Vec<_>>(), vec![0, 2, 5]);
assert_eq!(format!("{}", ls), "[0, 2, 5]");
assert_eq!(format!("{ls}"), "[0, 2, 5]");
}

#[test]
Expand All @@ -433,7 +433,7 @@ mod tests {
assert!(cs.contains(&(0, 0)));
assert!(!cs.contains(&(5, 5)));
assert_eq!(cs.iter().collect::<Vec<_>>(), vec![(0, 0), (1, 1), (2, 4)]);
assert_eq!(format!("{}", cs), "[(0, 0), (1, 1), (2, 4)]");
assert_eq!(format!("{cs}"), "[(0, 0), (1, 1), (2, 4)]");
cs.extend([(5, 5)]);
assert!(cs.contains(&(5, 5)));
}
Expand Down
8 changes: 4 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ impl QueensFile {
/// a QueensFile from it.
pub fn try_from_text_file(path: &std::path::PathBuf) -> Result<Self> {
let content = std::fs::read_to_string(path)
.with_context(|| format!("Could not read file `{:?}`", path))?;
.with_context(|| format!("Could not read file `{path:?}`"))?;

QueensFile::from_str(&content)
.with_context(|| format!("Failed to create board from text file at {:?}", path))
.with_context(|| format!("Failed to create board from text file at {path:?}"))
}

/// This reads the given path as an image file and attempts to return
Expand All @@ -105,7 +105,7 @@ impl QueensFile {
let rgb_image = ImageReader::open(path)?.decode()?.to_rgb8();

analyze_grid_image(&rgb_image)
.with_context(|| format!("Failed to create board from image at {:?}", path))
.with_context(|| format!("Failed to create board from image at {path:?}"))
}
}

Expand Down Expand Up @@ -154,7 +154,7 @@ mod tests {
fn input_squares_display() -> Result<()> {
let input_str = "Qxxx\nxx..\nx...\nx...";
let input_squares = InputSquares::from_str(input_str)?;
assert_eq!(format!("{}", input_squares), input_str.replace(".", " "));
assert_eq!(format!("{input_squares}"), input_str.replace(".", " "));
Ok(())
}

Expand Down
47 changes: 22 additions & 25 deletions src/heuristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ pub fn next_heuristic<'h>(
heuristics: &'h [Box<dyn Heuristic>],
) -> Option<&'h dyn Heuristic> {
debug!(
"Generating next heuristic with {:?} strategy",
solve_strategy
"Generating next heuristic with {solve_strategy:?} strategy"
);
let h = match solve_strategy {
SolveStrategy::Short => heuristics
Expand Down Expand Up @@ -127,7 +126,7 @@ pub fn all_heuristics(board: &Board) -> Vec<Box<dyn Heuristic>> {
v.extend(board.all_colors().iter().map(|color| {
Box::new(LastSquareAvailable {
coords: board.coords_for_color(color),
desc: format!("'{:?}' Color", color),
desc: format!("'{color:?}' Color"),
}) as _
}));
v.extend((0..board.size()).map(|r| {
Expand Down Expand Up @@ -157,7 +156,7 @@ pub fn all_heuristics(board: &Board) -> Vec<Box<dyn Heuristic>> {
v.extend(board.all_colors().into_iter().map(|color| {
Box::new(AllPossibilitiesEliminateSquare {
coords: board.coords_for_color(color),
desc: format!("'{:?}' Color", color),
desc: format!("'{color:?}' Color"),
}) as _
}));

Expand Down Expand Up @@ -186,7 +185,7 @@ pub fn all_heuristics(board: &Board) -> Vec<Box<dyn Heuristic>> {
.filter(|cc| !cc.is_empty())
.map(|cc| {
Box::new(NColorsOnlyAppearInNLines {
color_desc: format!("{:?}", cc),
color_desc: format!("{cc:?}"),
colors: SquareColorSet::from_iter(cc.into_iter().copied()),
liner: |coord| coord.0,
liner_desc: "rows".to_string(),
Expand All @@ -201,7 +200,7 @@ pub fn all_heuristics(board: &Board) -> Vec<Box<dyn Heuristic>> {
.filter(|cc| !cc.is_empty())
.map(|cc| {
Box::new(NColorsOnlyAppearInNLines {
color_desc: format!("{:?}", cc),
color_desc: format!("{cc:?}"),
colors: SquareColorSet::from_iter(cc.into_iter().copied()),
liner: |coord| coord.1,
liner_desc: "cols".to_string(),
Expand All @@ -223,22 +222,22 @@ impl Heuristic for LastSquareAvailable {
self.coords
}
fn changes(&self, solve_state: &SolveState) -> Option<Changes> {
trace!("Heuristic Start: LastSquareAvailable {:?}", self);
trace!("Heuristic Start: LastSquareAvailable {self:?}");
let last_empty_coord = self
.coords
.iter()
.filter(|&c| solve_state.square(&c).is_none())
.exactly_one()
.ok();
let queen = last_empty_coord?;
trace!("Heuristic Success: LastSquareAvailable {:?}", self);
trace!("Heuristic Success: LastSquareAvailable {self:?}");
let x = solve_state
.board
.queen_borders(&queen)
.iter()
.filter(|&coord| solve_state.square(&coord).is_none())
.collect::<CoordSet>();
trace!("Heuristic Return: LastSquareAvailable {:?}", self);
trace!("Heuristic Return: LastSquareAvailable {self:?}");
Some(Changes::AddQueen { queen, x })
}

Expand All @@ -265,8 +264,7 @@ impl Heuristic for AllPossibilitiesEliminateSquare {
}
fn changes(&self, solve_state: &SolveState) -> Option<Changes> {
trace!(
"Heuristic Start: AllPossibilitiesEliminateSquare {:?}",
self
"Heuristic Start: AllPossibilitiesEliminateSquare {self:?}"
);
let x = self
.coords
Expand All @@ -282,8 +280,7 @@ impl Heuristic for AllPossibilitiesEliminateSquare {
None
} else {
trace!(
"Heuristic Success/Return: AllPossibilitiesEliminateSquare {:?}",
self
"Heuristic Success/Return: AllPossibilitiesEliminateSquare {self:?}"
);
Some(Changes::AddX { x })
}
Expand Down Expand Up @@ -312,14 +309,14 @@ impl Heuristic for NLinesContainOnlyNColors {
.collect()
}
fn changes(&self, solve_state: &SolveState) -> Option<Changes> {
trace!("Heuristic Start: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Start: NLinesContainOnlyNColors {self:?}");
if self
.lines
.iter()
.flatten()
.any(|coord| solve_state.square(&coord) == Some(SquareVal::Queen))
{
trace!("Heuristic Invalid: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Invalid: NLinesContainOnlyNColors {self:?}");
return None;
}
let coords = CoordSet::from_iter(
Expand All @@ -331,10 +328,10 @@ impl Heuristic for NLinesContainOnlyNColors {
let colors_set =
SquareColorSet::from_iter(coords.iter().map(|coord| solve_state.board.color(&coord)));
if colors_set.len() > self.lines.len() {
trace!("Heuristic Invalid: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Invalid: NLinesContainOnlyNColors {self:?}");
return None;
}
trace!("Heuristic Success: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Success: NLinesContainOnlyNColors {self:?}");
let x = solve_state
.board
.all_coords()
Expand All @@ -344,10 +341,10 @@ impl Heuristic for NLinesContainOnlyNColors {
.filter(|coord| solve_state.square(coord).is_none())
.collect::<CoordSet>();
if x.is_empty() {
trace!("Heuristic No-op: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic No-op: NLinesContainOnlyNColors {self:?}");
None
} else {
trace!("Heuristic Return: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Return: NLinesContainOnlyNColors {self:?}");
Some(Changes::AddX { x })
}
}
Expand Down Expand Up @@ -380,15 +377,15 @@ impl Heuristic for NColorsOnlyAppearInNLines {
.collect()
}
fn changes(&self, solve_state: &SolveState) -> Option<Changes> {
trace!("Heuristic Start: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Start: NLinesContainOnlyNColors {self:?}");
if solve_state
.board
.all_coords()
.iter()
.filter(|coord| self.colors.contains(&solve_state.board.color(coord)))
.any(|coord| solve_state.square(&coord) == Some(SquareVal::Queen))
{
trace!("Heuristic Invalid: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Invalid: NLinesContainOnlyNColors {self:?}");
return None;
}
let coords = solve_state.board.all_coords();
Expand All @@ -399,10 +396,10 @@ impl Heuristic for NColorsOnlyAppearInNLines {
.map(self.liner);
let lines_set = LineSet::from_iter(lines);
if lines_set.len() > self.colors.len() {
trace!("Heuristic Invalid: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Invalid: NLinesContainOnlyNColors {self:?}");
return None;
}
trace!("Heuristic Success: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Success: NLinesContainOnlyNColors {self:?}");
let x = solve_state
.board
.all_coords()
Expand All @@ -412,10 +409,10 @@ impl Heuristic for NColorsOnlyAppearInNLines {
.filter(|coord| solve_state.square(coord).is_none())
.collect::<CoordSet>();
if x.is_empty() {
trace!("Heuristic No-op: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic No-op: NLinesContainOnlyNColors {self:?}");
None
} else {
trace!("Heuristic Return: NLinesContainOnlyNColors {:?}", self);
trace!("Heuristic Return: NLinesContainOnlyNColors {self:?}");
Some(Changes::AddX { x })
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const X_OTHER_RATIO: f32 = 0.01;
/// # }
/// ```
pub fn analyze_grid_image(img: &RgbImage) -> Result<QueensFile> {
trace!("Analyze grid image start: {:?}", img);
trace!("Analyze grid image start: {img:?}");
let width_ranges = find_grid_ranges(img, 0..img.width(), true);
ensure!(
width_ranges.len() >= 4,
Expand All @@ -110,8 +110,7 @@ pub fn analyze_grid_image(img: &RgbImage) -> Result<QueensFile> {
let mut square_values = Vec::with_capacity(board_size * board_size);

trace!(
"Analyze grid image ranges found: {:?} {:?}",
width_ranges, height_ranges
"Analyze grid image ranges found: {width_ranges:?} {height_ranges:?}"
);
for (height_range, width_range) in iproduct!(height_ranges, width_ranges) {
let view = img.view(
Expand All @@ -128,8 +127,7 @@ pub fn analyze_grid_image(img: &RgbImage) -> Result<QueensFile> {
)
})?;
trace!(
"Analyze grid image color: for height {:?} width {:?} got dominant color {:?}",
height_range, width_range, rgb_color
"Analyze grid image color: for height {height_range:?} width {width_range:?} got dominant color {rgb_color:?}"
);
all_rgb_colors.push(rgb_color);

Expand All @@ -140,8 +138,7 @@ pub fn analyze_grid_image(img: &RgbImage) -> Result<QueensFile> {
_ => None,
};
trace!(
"Analyze grid image ratio: For height {:?} width {:?} got ratio {:?} and value {:?}",
height_range, width_range, other_ratio, square_val
"Analyze grid image ratio: For height {height_range:?} width {width_range:?} got ratio {other_ratio:?} and value {square_val:?}"
);
square_values.push(square_val);
}
Expand Down Expand Up @@ -175,8 +172,8 @@ pub fn analyze_grid_image(img: &RgbImage) -> Result<QueensFile> {
let board = Board::new(board_size, colors);
let squares = InputSquares::from(square_values);
trace!("Analyze grid image done.");
trace!("Board:\n{}", board);
trace!("Squares:\n{}", squares);
trace!("Board:\n{board}");
trace!("Squares:\n{squares}");
Ok(QueensFile {
board,
squares: Some(squares),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn profile(path_args: &PathCli, solve_args: &SolveCli, iterations: &usize) -> Re
solve_iter(solve_state, solve_args.strategy, &heuristics).for_each(drop);
}
let elapsed = start_time.elapsed();
println!("{} iterations completed in {:?}", iterations, elapsed);
println!("{iterations} iterations completed in {elapsed:?}");
Ok(())
}

Expand Down
5 changes: 2 additions & 3 deletions src/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ pub fn generate_share_content(
.collect::<Vec<_>>();

let puzzle_name = if puzzle_name.chars().all(char::is_numeric) {
format!("#{}", puzzle_name)
format!("#{puzzle_name}")
} else {
puzzle_name.to_string()
};

let mut output = String::new();
output.push_str(&format!(
"QSolve {} | {:?} and flawless\n",
puzzle_name, elapsed
"QSolve {puzzle_name} | {elapsed:?} and flawless\n"
));
output.push_str(&format!(
"First \u{1f451}s: {}\n",
Expand Down
12 changes: 6 additions & 6 deletions src/solvestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub enum SolveStrategy {

impl Display for SolveStrategy {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ impl<'a> From<&'a QueensFile> for SolveState<'a> {
solve_state.apply_changes(&Changes::AddQueen { queen, x });
}

trace!("From<QueensFile> for SolveState done:\n{}", solve_state);
trace!("From<QueensFile> for SolveState done:\n{solve_state}");

solve_state
}
Expand Down Expand Up @@ -384,13 +384,13 @@ mod tests {
fn solvestate_from_queens_file() {
let board_str = "wwww\nkkkk\nrrrr\nbbbb";
let squares_str = "Qxxx\nxx..\nx...\nx. _";
let qf_str = format!("{}\n\n{}", board_str, squares_str);
let qf_str = format!("{board_str}\n\n{squares_str}");
let qf = QueensFile::from_str(&qf_str).unwrap();
let ss = SolveState::from(&qf);
assert!(ss.is_valid());

assert_eq!(
format!("{}", ss),
format!("{ss}"),
qf_str.replace(".", " ").replace("_", " ")
);
}
Expand All @@ -399,7 +399,7 @@ mod tests {
fn solvestate_ansi_string() {
let board_str = "wwww\nkkkk\nrrrr\nbbbb";
let squares_str = "Qxxx\nxx..\nx...\nx. _";
let qf_str = format!("{}\n\n{}", board_str, squares_str);
let qf_str = format!("{board_str}\n\n{squares_str}");
let qf = QueensFile::from_str(&qf_str).unwrap();
let ss = SolveState::from(&qf);
assert!(ss.is_valid());
Expand All @@ -417,7 +417,7 @@ mod tests {
fn solvestate_ansi_string_highlighted() {
let board_str = "wwww\nkkkk\nrrrr\nbbbb";
let squares_str = "Qxxx\nxx..\nx...\nx. _";
let qf_str = format!("{}\n\n{}", board_str, squares_str);
let qf_str = format!("{board_str}\n\n{squares_str}");
let qf = QueensFile::from_str(&qf_str).unwrap();
let ss = SolveState::from(&qf);
assert!(ss.is_valid());
Expand Down
4 changes: 2 additions & 2 deletions src/squarecolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Display for SquareColor {
SquareColor::BrightCyan => 'C',
SquareColor::BrightWhite => 'W',
};
write!(f, "{}", c)
write!(f, "{c}")
}
}

Expand Down Expand Up @@ -188,7 +188,7 @@ mod tests {
#[test]
fn squarecolor_char_roundtrip() {
for sc in ALL_SQUARE_COLORS {
let c = format!("{}", sc).chars().next().unwrap();
let c = format!("{sc}").chars().next().unwrap();
assert_eq!(SquareColor::try_from(c).unwrap(), sc)
}
}
Expand Down