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
14macro_rules! from_str {
15 ($a:ty) => {
16 impl<'a> Parse<'a> for $a {
17 fn parse(i: Arg) -> Result<Self, ErrVal> {
18 <$a>::from_str(i).map_err(|e| ErrVal::Err {
19 input: i.into(),
20 typename: Self::desc().into(),
21 error: format!("{e}"),
22 })
23 }
24
25 fn desc() -> &'static str {
26 stringify!($a)
27 }
28 }
29 };
30}
31
32from_str!(i8);
33from_str!(i16);
34from_str!(i32);
35from_str!(i64);
36from_str!(i128);
37from_str!(u8);
38from_str!(u16);
39from_str!(u32);
40from_str!(u64);
41from_str!(u128);
42from_str!(f32);
43from_str!(f64);
44from_str!(String);
45from_str!(bool);
46from_str!(char);
47from_str!(usize);
48from_str!(isize);
49
50fn file_exist(i: &str) -> Result<PathBuf, String> {
51 let p = PathBuf::from_str(i).map_err(|e| e.to_string())?;
52 if !p.exists() {
53 return Err(format!("Does not exist"));
54 };
55 if !p.is_file() {
56 return Err(format!("Not a file"));
57 };
58 Ok(p)
59}
60
61impl<'a> Parse<'a> for FileExist {
62 fn parse(i: Arg) -> Result<Self, ErrVal> {
63 match file_exist(i) {
64 Ok(p) => Ok(FileExist { p, s: i.to_owned() }),
65 Err(e) => Err(ErrVal::Err {
66 input: i.into(),
67 typename: Self::desc().into(),
68 error: e,
69 }),
70 }
71 }
72
73 fn desc() -> &'static str {
74 stringify!(FileExist)
75 }
76}
77
78fn dir_exist(i: &str) -> Result<PathBuf, String> {
79 let p = PathBuf::from_str(i).map_err(|e| e.to_string())?;
80 if !p.exists() {
81 return Err(format!("Does not exist"));
82 };
83 if !p.is_dir() {
84 return Err(format!("Not a dir"));
85 };
86 Ok(p)
87}
88
89impl<'a> Parse<'a> for DirExist {
90 fn parse(i: Arg) -> Result<Self, ErrVal> {
91 match dir_exist(i) {
92 Ok(p) => Ok(DirExist { p, s: i.to_owned() }),
93 Err(e) => Err(ErrVal::Err {
94 input: i.into(),
95 typename: Self::desc().into(),
96 error: e,
97 }),
98 }
99 }
100
101 fn desc() -> &'static str {
102 stringify!(DirExist)
103 }
104}