rust_sudoku/
cli.rs

1use bpaf::{Bpaf, FromUtf8};
2use std::{path::PathBuf, str::FromStr};
3
4#[derive(Debug, Clone, Bpaf)]
5/// Solve Sudoku problems *blazingly fast*.
6///
7/// Accepts input from a text file to fill in the puzzle. Each number (1-9) is interpreted
8/// as that number in the puzzle. A zero or any other letter is considered a blank space.
9/// Any whitespace is ignored.
10#[bpaf(options, version)]
11pub struct Config {
12    /// Location of puzzle to read or stdin by default.
13    #[bpaf(short, long, argument("FILE"))]
14    pub input: Option<PathBuf>,
15    /// Output file to write solution to or stdout by default.
16    #[bpaf(short, long, argument("FILE"))]
17    pub output: Option<PathBuf>,
18    /// Print puzzle with nice borders, options include `simple`, `multiline` and `bordered`.
19    #[bpaf(long, argument::<FromUtf8<OutputStyle>>("STYLE"), fallback(OutputStyle::Bordered))]
20    pub style: OutputStyle,
21    /// Print each partial solution to the console as the program runs.
22    #[bpaf(short, long)]
23    pub print_partials: bool,
24    /// Add delay between each iteration in ms (useful when using `--print-partials`).
25    #[bpaf(short, long, argument("DELAY"))]
26    pub delay: Option<u64>,
27}
28
29#[derive(Debug, Clone, Copy)]
30/// How the puzzle is printed.
31pub enum OutputStyle {
32    /// The entire puzzle is printed in one line.
33    Simple,
34    /// The puzzle is printed with 9 numbers on each line.
35    MultiLine,
36    /// The puzzle is printed with a border around the puzzle and around each 3x3 square.
37    Bordered,
38}
39
40impl FromStr for OutputStyle {
41    type Err = &'static str;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        match s {
45            "simple" => Ok(Self::Simple),
46            "multiline" => Ok(Self::MultiLine),
47            "bordered" => Ok(Self::Bordered),
48            _ => Err("Invalid output style, expected simple|multiline|bordered"),
49        }
50    }
51}