1use crate::com::*;
2
3#[derive(Clone, Debug, Default)]
4pub struct FileExist {
5 pub p: PathBuf,
6 pub s: String,
7}
8#[derive(Clone, Debug, Default)]
9pub struct DirExist {
10 pub p: PathBuf,
11 pub s: String,
12}
13
14impl<'a> Parse<'a> for i32 {
15 fn parse(i: Arg) -> Result<Self, ParseErr> {
16 i32::from_str(i).map_err(|e| ParseErr {
17 i,
18 ty: Self::desc(),
19 e: format!("{e}"),
20 })
21 }
22
23 fn desc() -> &'static str {
24 stringify!(i32)
25 }
26}
27impl<'a> Parse<'a> for i64 {
28 fn parse(i: Arg) -> Result<Self, ParseErr> {
29 i64::from_str(i).map_err(|e| ParseErr {
30 i,
31 ty: Self::desc(),
32 e: format!("{e}"),
33 })
34 }
35
36 fn desc() -> &'static str {
37 stringify!(i64)
38 }
39}
40impl<'a> Parse<'a> for String {
41 fn parse(i: Arg) -> Result<Self, ParseErr> {
42 String::from_str(i).map_err(|e| ParseErr {
43 i,
44 ty: Self::desc(),
45 e: format!("{e}"),
46 })
47 }
48
49 fn desc() -> &'static str {
50 stringify!(String)
51 }
52}
53
54fn file_exist(i: &str) -> Result<PathBuf, String> {
55 let p = PathBuf::from_str(i).map_err(|e| e.to_string())?;
56 if !p.exists() {
57 return Err(format!("Does not exist"));
58 };
59 if !p.is_file() {
60 return Err(format!("Not a file"));
61 };
62 Ok(p)
63}
64
65impl<'a> Parse<'a> for FileExist {
66 fn parse(i: Arg) -> Result<Self, ParseErr> {
67 match file_exist(i) {
68 Ok(p) => Ok(FileExist { p, s: i.to_owned() }),
69 Err(e) => Err(ParseErr {
70 i,
71 ty: Self::desc(),
72 e,
73 }),
74 }
75 }
76
77 fn desc() -> &'static str {
78 stringify!(FileExist)
79 }
80}
81
82fn dir_exist(i: &str) -> Result<PathBuf, String> {
83 let p = PathBuf::from_str(i).map_err(|e| e.to_string())?;
84 if !p.exists() {
85 return Err(format!("Does not exist"));
86 };
87 if !p.is_dir() {
88 return Err(format!("Not a dir"));
89 };
90 Ok(p)
91}
92
93impl<'a> Parse<'a> for DirExist {
94 fn parse(i: Arg) -> Result<Self, ParseErr> {
95 match dir_exist(i) {
96 Ok(p) => Ok(DirExist { p, s: i.to_owned() }),
97 Err(e) => Err(ParseErr {
98 i,
99 ty: Self::desc(),
100 e,
101 }),
102 }
103 }
104
105 fn desc() -> &'static str {
106 stringify!(DirExist)
107 }
108}