aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: 63573557200c9a12b302583c19afd70a3b53a5cf (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::{
    default::Default,
    fmt,
    path::{Path, PathBuf},
};

pub static HELP_TEXT: &str = "
Usage
-----

slate new <FILE> -d WIDTHxHEIGHT

Options
-------

    -h, --help      Prints help information
    -d,             Specify dimensions for new file (default: 200x200)
";

#[derive(Debug)]
pub enum CliError {
    NoDimensions,
    FileAlreadyExists,
    FileDoesNotExist,
    DimensionParseError,
    NewRequiresFile,
    InvalidSubCommand(String),
    SubCommandParseError,
}

impl fmt::Display for CliError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoDimensions => write!(f, "no dimensions provided"),
            Self::FileAlreadyExists => {
                write!(f, "cannot initialize project, file already exists")
            }
            Self::FileDoesNotExist => {
                write!(f, "cannot open existing project, file does not exist")
            }
            Self::DimensionParseError => write!(f, "invalid dimension string"),
            Self::NewRequiresFile => write!(f, "new subcommand requires file name"),
            Self::InvalidSubCommand(s) => write!(f, "invalid subcommand `{}`", s),
            Self::SubCommandParseError => write!(f, "error parsing subcommand"),
        }
    }
}

impl std::error::Error for CliError {}

#[derive(Debug)]
pub enum Config {
    NewProject {
        file_name: Option<PathBuf>,
        dimensions: (u32, u32),
    },
    ExistingProject {
        file_name: PathBuf,
    },
    Help,
}

impl Default for Config {
    fn default() -> Self {
        Self::NewProject {
            file_name: None,
            dimensions: (200, 200),
        }
    }
}

pub fn parse_args() -> Result<Config, CliError> {
    let mut args = pico_args::Arguments::from_env();

    if args.contains(["-h", "--help"]) {
        return Ok(Config::Help);
    }

    match args.subcommand() {
        Ok(cmd) => match cmd.as_deref() {
            Some("new") => new_project(&mut args),
            Some(s) => {
                if !Path::new(s).is_file() {
                    return Err(CliError::FileDoesNotExist);
                }
                Ok(Config::ExistingProject {
                    file_name: PathBuf::from(s),
                })
            }
            None => Ok(Config::Help),
        },
        _ => Err(CliError::SubCommandParseError),
    }
}

fn new_project(args: &mut pico_args::Arguments) -> Result<Config, CliError> {
    let file_name = args.free_from_str();
    if let Ok(f) = &file_name {
        if Path::new(&f).is_file() {
            return Err(CliError::FileAlreadyExists);
        }
    }
    let dimensions = args
        .opt_value_from_fn(["-d", "--dimensions"], parse_dimensions)
        .map_err(|_| CliError::DimensionParseError)?
        .unwrap_or((200, 200));
    Ok(Config::NewProject {
        file_name: file_name.ok(),
        dimensions,
    })
}

fn parse_dimensions(input: &str) -> Result<(u32, u32), CliError> {
    let dimensions: Vec<&str> = input.split('x').collect();
    match &dimensions[..] {
        [width, height] => Ok((
            width
                .parse::<u32>()
                .map_err(|_| CliError::DimensionParseError)?,
            height
                .parse::<u32>()
                .map_err(|_| CliError::DimensionParseError)?,
        )),
        _ => Err(CliError::DimensionParseError),
    }
}