Fix rustfmt and nixfmt
This commit is contained in:
@@ -56,7 +56,7 @@
|
|||||||
wayland
|
wayland
|
||||||
libxkbcommon
|
libxkbcommon
|
||||||
];
|
];
|
||||||
|
|
||||||
env = [
|
env = [
|
||||||
{
|
{
|
||||||
name = "LD_LIBRARY_PATH";
|
name = "LD_LIBRARY_PATH";
|
||||||
|
|||||||
+27
-25
@@ -1,7 +1,7 @@
|
|||||||
use crate::cell::{Cell, CellValue};
|
use crate::cell::{Cell, CellValue};
|
||||||
|
use ndarray::{prelude::*, ViewRepr};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use ndarray::{prelude::*, ViewRepr};
|
|
||||||
|
|
||||||
const REGION_SIZE: usize = 9;
|
const REGION_SIZE: usize = 9;
|
||||||
const SUB_REGION_SIZE: usize = 3;
|
const SUB_REGION_SIZE: usize = 3;
|
||||||
@@ -30,9 +30,7 @@ impl fmt::Display for Board {
|
|||||||
impl Board {
|
impl Board {
|
||||||
pub fn new() -> Board {
|
pub fn new() -> Board {
|
||||||
let cells = Array2::zeros((REGION_SIZE, REGION_SIZE));
|
let cells = Array2::zeros((REGION_SIZE, REGION_SIZE));
|
||||||
let board = Board {
|
let board = Board { cells };
|
||||||
cells,
|
|
||||||
};
|
|
||||||
board
|
board
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +42,7 @@ impl Board {
|
|||||||
self.cells[point.coordinates()].set_value(value);
|
self.cells[point.coordinates()].set_value(value);
|
||||||
self.update_related(point, value);
|
self.update_related(point, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_related(&mut self, point: &Point, value: CellValue) {
|
pub fn update_related(&mut self, point: &Point, value: CellValue) {
|
||||||
self.update_column(point, value);
|
self.update_column(point, value);
|
||||||
self.update_row(point, value);
|
self.update_row(point, value);
|
||||||
@@ -82,8 +80,11 @@ impl Board {
|
|||||||
pub fn col_range(&self) -> std::ops::Range<usize> {
|
pub fn col_range(&self) -> std::ops::Range<usize> {
|
||||||
0..REGION_SIZE
|
0..REGION_SIZE
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn square_iter(&self, point: &Point) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 2]>> {
|
pub fn square_iter(
|
||||||
|
&self,
|
||||||
|
point: &Point,
|
||||||
|
) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 2]>> {
|
||||||
let row_bounds = point.row_bounds();
|
let row_bounds = point.row_bounds();
|
||||||
let col_bounds = point.col_bounds();
|
let col_bounds = point.col_bounds();
|
||||||
self.cells.slice(s![row_bounds, col_bounds])
|
self.cells.slice(s![row_bounds, col_bounds])
|
||||||
@@ -92,7 +93,7 @@ impl Board {
|
|||||||
pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
|
pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
|
||||||
self.cells.slice(s![row, ..])
|
self.cells.slice(s![row, ..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn col_iter(&self, col: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
|
pub fn col_iter(&self, col: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
|
||||||
self.cells.slice(s![.., col])
|
self.cells.slice(s![.., col])
|
||||||
}
|
}
|
||||||
@@ -101,30 +102,31 @@ impl Board {
|
|||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Point {
|
pub struct Point {
|
||||||
row: usize,
|
row: usize,
|
||||||
col: usize
|
col: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point {
|
impl Point {
|
||||||
pub fn new(row: usize, col: usize) -> Point {
|
pub fn new(row: usize, col: usize) -> Point {
|
||||||
Point {
|
Point { row, col }
|
||||||
row, col
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn coordinates(&self) -> [usize; 2] {
|
fn coordinates(&self) -> [usize; 2] {
|
||||||
[self.row, self.col]
|
[self.row, self.col]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _bounds(val: usize) -> std::ops::Range<usize> {
|
fn _bounds(val: usize) -> std::ops::Range<usize> {
|
||||||
let mut start = val / (SUB_REGION_SIZE as usize);
|
let mut start = val / (SUB_REGION_SIZE as usize);
|
||||||
start *= SUB_REGION_SIZE;
|
start *= SUB_REGION_SIZE;
|
||||||
std::ops::Range {start, end: start + SUB_REGION_SIZE}
|
std::ops::Range {
|
||||||
|
start,
|
||||||
|
end: start + SUB_REGION_SIZE,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn row_bounds(&self) -> std::ops::Range<usize> {
|
fn row_bounds(&self) -> std::ops::Range<usize> {
|
||||||
Self::_bounds(self.row)
|
Self::_bounds(self.row)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn col_bounds(&self) -> std::ops::Range<usize> {
|
fn col_bounds(&self) -> std::ops::Range<usize> {
|
||||||
Self::_bounds(self.col)
|
Self::_bounds(self.col)
|
||||||
}
|
}
|
||||||
@@ -138,13 +140,13 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bounds_are_correct() {
|
fn bounds_are_correct() {
|
||||||
let origin = Point{ row: 0, col: 0};
|
let origin = Point { row: 0, col: 0 };
|
||||||
assert_eq!(origin.row_bounds(), 0..3);
|
assert_eq!(origin.row_bounds(), 0..3);
|
||||||
assert_eq!(origin.col_bounds(), 0..3);
|
assert_eq!(origin.col_bounds(), 0..3);
|
||||||
let one = Point{ row: 1, col: 1};
|
let one = Point { row: 1, col: 1 };
|
||||||
assert_eq!(one.row_bounds(), 0..3);
|
assert_eq!(one.row_bounds(), 0..3);
|
||||||
assert_eq!(one.col_bounds(), 0..3);
|
assert_eq!(one.col_bounds(), 0..3);
|
||||||
let five = Point{ row: 5, col: 7};
|
let five = Point { row: 5, col: 7 };
|
||||||
assert_eq!(five.row_bounds(), 3..6);
|
assert_eq!(five.row_bounds(), 3..6);
|
||||||
assert_eq!(five.col_bounds(), 6..9);
|
assert_eq!(five.col_bounds(), 6..9);
|
||||||
}
|
}
|
||||||
@@ -152,24 +154,24 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn has_none_values() {
|
fn has_none_values() {
|
||||||
let board = Board::new();
|
let board = Board::new();
|
||||||
assert!(board.value(&Point{row: 0, col: 0}).is_none());
|
assert!(board.value(&Point { row: 0, col: 0 }).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn set_value_updates_cell() {
|
fn set_value_updates_cell() {
|
||||||
let mut board = Board::new();
|
let mut board = Board::new();
|
||||||
board.set_value(&Point{row: 0, col: 0}, CellValue::One);
|
board.set_value(&Point { row: 0, col: 0 }, CellValue::One);
|
||||||
let val = board.value(&Point{row: 0, col: 0}).unwrap();
|
let val = board.value(&Point { row: 0, col: 0 }).unwrap();
|
||||||
assert_eq!(val, CellValue::One);
|
assert_eq!(val, CellValue::One);
|
||||||
|
|
||||||
// Same column
|
// Same column
|
||||||
assert_not_contains!(board.candidates(&Point{row: 1, col: 0}), &CellValue::One);
|
assert_not_contains!(board.candidates(&Point { row: 1, col: 0 }), &CellValue::One);
|
||||||
// Same row
|
// Same row
|
||||||
assert_not_contains!(board.candidates(&Point{row: 0, col: 1}), &CellValue::One);
|
assert_not_contains!(board.candidates(&Point { row: 0, col: 1 }), &CellValue::One);
|
||||||
// Same sub-structure
|
// Same sub-structure
|
||||||
assert_not_contains!(board.candidates(&Point{row: 1, col: 1}), &CellValue::One);
|
assert_not_contains!(board.candidates(&Point { row: 1, col: 1 }), &CellValue::One);
|
||||||
|
|
||||||
// Different row
|
// Different row
|
||||||
assert_contains!(board.candidates(&Point{row: 8, col: 8}), &CellValue::One);
|
assert_contains!(board.candidates(&Point { row: 8, col: 8 }), &CellValue::One);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use num_traits::identities::Zero;
|
use num_traits::identities::Zero;
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ impl ToString for CellValue {
|
|||||||
CellValue::Six => String::from("6"),
|
CellValue::Six => String::from("6"),
|
||||||
CellValue::Seven => String::from("7"),
|
CellValue::Seven => String::from("7"),
|
||||||
CellValue::Eight => String::from("8"),
|
CellValue::Eight => String::from("8"),
|
||||||
CellValue::Nine => String::from("9")
|
CellValue::Nine => String::from("9"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@ impl Zero for Cell {
|
|||||||
candidates,
|
candidates,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_zero(&self) -> bool {
|
fn is_zero(&self) -> bool {
|
||||||
self.value.is_none()
|
self.value.is_none()
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-12
@@ -1,6 +1,10 @@
|
|||||||
use iced::{border::Radius, widget::{button, text}, Background, Border, Color, Shadow, Theme};
|
|
||||||
use super::super::cell::Cell;
|
use super::super::cell::Cell;
|
||||||
use super::Message;
|
use super::Message;
|
||||||
|
use iced::{
|
||||||
|
border::Radius,
|
||||||
|
widget::{button, text},
|
||||||
|
Background, Border, Color, Shadow, Theme,
|
||||||
|
};
|
||||||
|
|
||||||
const LIGHT_GRAY: Color = Color {
|
const LIGHT_GRAY: Color = Color {
|
||||||
r: 0.5,
|
r: 0.5,
|
||||||
@@ -19,22 +23,20 @@ pub fn widget(cell: &Cell) -> iced::Element<Message> {
|
|||||||
.style(|theme: &Theme, status| {
|
.style(|theme: &Theme, status| {
|
||||||
// Get the current theme's palette
|
// Get the current theme's palette
|
||||||
let palette = theme.palette();
|
let palette = theme.palette();
|
||||||
|
|
||||||
// Create a default border, since we need to set off the cells
|
// Create a default border, since we need to set off the cells
|
||||||
// TODO: Figure out how to get this out of the theme?
|
// TODO: Figure out how to get this out of the theme?
|
||||||
let mut border = Border::default().color(LIGHT_GRAY);
|
let mut border = Border::default().color(LIGHT_GRAY);
|
||||||
border.width = 1.0;
|
border.width = 1.0;
|
||||||
|
|
||||||
// Return a style based on the status
|
// Return a style based on the status
|
||||||
match status {
|
match status {
|
||||||
button::Status::Active => {
|
button::Status::Active => button::Style {
|
||||||
button::Style{
|
background: Some(Background::from(palette.background)),
|
||||||
background: Some(Background::from(palette.background)),
|
text_color: palette.text,
|
||||||
text_color: palette.text,
|
border,
|
||||||
border,
|
shadow: Shadow::default(),
|
||||||
shadow: Shadow::default(),
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
let mut style = button::primary(theme, status);
|
let mut style = button::primary(theme, status);
|
||||||
style.border.radius = Radius::new(0);
|
style.border.radius = Radius::new(0);
|
||||||
@@ -46,4 +48,4 @@ pub fn widget(cell: &Cell) -> iced::Element<Message> {
|
|||||||
.width(50)
|
.width(50)
|
||||||
.on_press(Message::Pressed)
|
.on_press(Message::Pressed)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -3,4 +3,4 @@ pub enum Message {
|
|||||||
Pressed,
|
Pressed,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod cell;
|
pub mod cell;
|
||||||
|
|||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
mod cell;
|
|
||||||
mod board;
|
mod board;
|
||||||
|
mod cell;
|
||||||
pub mod display;
|
pub mod display;
|
||||||
|
|
||||||
pub use board::Board;
|
pub use board::Board;
|
||||||
pub use board::Point;
|
pub use board::Point;
|
||||||
pub use cell::Cell;
|
pub use cell::Cell;
|
||||||
pub use cell::CellValue;
|
pub use cell::CellValue;
|
||||||
|
|||||||
+12
-16
@@ -1,9 +1,8 @@
|
|||||||
use rustdoku::Board;
|
|
||||||
use rustdoku::display;
|
use rustdoku::display;
|
||||||
|
use rustdoku::Board;
|
||||||
|
|
||||||
use iced;
|
use iced;
|
||||||
|
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
board: Board,
|
board: Board,
|
||||||
message: String,
|
message: String,
|
||||||
@@ -25,22 +24,19 @@ fn update(state: &mut State, _message: display::Message) {
|
|||||||
fn display(state: &State) -> iced::Element<display::Message> {
|
fn display(state: &State) -> iced::Element<display::Message> {
|
||||||
let mut column: Vec<iced::Element<display::Message>> = Vec::with_capacity(9);
|
let mut column: Vec<iced::Element<display::Message>> = Vec::with_capacity(9);
|
||||||
for row_idx in state.board.row_range() {
|
for row_idx in state.board.row_range() {
|
||||||
let row: Vec<iced::Element<display::Message>> = state.board
|
let row: Vec<iced::Element<display::Message>> = state
|
||||||
.row_iter(row_idx)
|
.board
|
||||||
.into_iter()
|
.row_iter(row_idx)
|
||||||
.map(|c| display::cell::widget(c))
|
.into_iter()
|
||||||
.into_iter()
|
.map(|c| display::cell::widget(c))
|
||||||
.collect();
|
.into_iter()
|
||||||
column.push(
|
.collect();
|
||||||
iced::widget::Row::with_children(row)
|
column.push(iced::widget::Row::with_children(row).into());
|
||||||
.into()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iced::widget::Column::with_children(column)
|
iced::widget::Column::with_children(column).into()
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
iced::run("SuDoKu", update, display)
|
iced::run("SuDoKu", update, display)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user