summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 3732b5011cc964be3f4b0ec24ad8c273a3711637fff5bb6b86ad7adf0c04efa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
    hitomezashi-rs  Generates classical colored Hitomezashi stitch patterns
    Copyright (C) 2024  Nicholas Johnson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use clap::{value_parser, Parser};
use colored::Colorize;
use std::process::exit;

mod hitomezashi;

#[derive(Parser)]
#[command(version, about, long_about = None)] // Read from `Cargo.toml`
struct Cli {
    /// Pattern width
    #[arg(value_parser = value_parser!(usize))]
    width: usize,

    /// Pattern height
    #[arg(value_parser = value_parser!(usize))]
    height: usize,

    #[arg(short, long)]
    #[arg(default_value_t = 0.5)]
    #[arg(value_parser = value_parser!(f64))]
    /// Set skew
    skew: f64,
}

/* CLI for hitomezashi */
fn main() {
    let cli = Cli::parse();

    // validate bounds of width [1..]
    if cli.width < 1 {
        println!("{} invalid value '{}' for '{}': valid values are [1..]\n\nFor more information, try '{}'.",
			"error:".red().bold(),
			cli.width.to_string().yellow(),
			"<WIDTH>".bold(),
			"--help".bold());
        exit(2);
    }

    // validate bounds of height [1..]
    if cli.height < 1 {
        println!("{} invalid value '{}' for '{}': valid values are [1..]\n\nFor more information, try '{}'.",
			"error:".red().bold(),
			cli.height.to_string().yellow(),
			"<HEIGHT>".bold(),
			"--help".bold());
        exit(2);
    }

    // validate bounds of skew [0-1]
    if cli.skew < 0.0 || cli.skew > 1.0 {
        println!("{} invalid value '{}' for '{}': valid values are [0-1]\n\nFor more information, try '{}'.",
			"error:".red().bold(),
			cli.skew.to_string().yellow(),
			"--skew <SKEW>".bold(),
			"--help".bold());
        exit(2);
    }

    hitomezashi::hitomezashi(cli.width, cli.height, Some(cli.skew));
}