shavee_core/
lib.rs

1pub mod filehash;
2pub mod logic;
3pub mod password;
4pub mod structs;
5pub mod yubikey;
6pub mod zfs;
7
8pub const UNREACHABLE_CODE: &str =
9    "Panic! Something unexpected happened! Please help by reporting it as a bug.";
10
11/// Static salt for backward compatibility
12pub const STATIC_SALT: &str = "This Project is Dedicated to Tamanna.";
13
14/// Name of Shell Environment variable for storing salt
15pub const ENV_SALT_VARIABLE: &str = "SHAVEE_SALT";
16
17/// Len of the random salt (in Bytes)
18/// It must be bigger than 16 bytes
19pub const RANDOM_SALT_LEN: usize = 32;
20
21use clap;
22#[cfg(feature = "trace")]
23use env_logger;
24#[cfg(feature = "trace")]
25use log;
26
27/// first [0] value is the file name
28/// it is a required field for "--file" and its existence already checked by clap
29pub fn parse_file_size_arguments(
30    file_size_argument: Vec<&String>,
31) -> Result<(Option<String>, Option<u64>), clap::Error> {
32    let file = Some(file_size_argument[0].to_string());
33
34    // If user entered SIZE arg value, it will be wrapped with Some(), otherwise None will be returned
35    let size = match file_size_argument.len() {
36        // if there is only 1 entry then it is file name and size is set to None
37        number_of_entries if number_of_entries == 1 => None,
38        // if there are 2 entries, then 2nd entry is size
39        number_of_entries if number_of_entries == 2 => {
40            // if "--file" has two entries, then 2nd [1] is size
41            let second_entry = file_size_argument[1];
42            // however the size entry needs to be validated and return error if it is not a u64 value
43            match second_entry.parse::<u64>() {
44                // wrap the parsed entry with Some()
45                Ok(size_arg) => Some(size_arg),
46
47                // on error return invalid value kind
48                Err(_) => {
49                    let error_message =
50                        format!(r#""{}" is not valid for SIZE argument."#, second_entry);
51
52                    return Err(clap::Error::raw(
53                        clap::error::ErrorKind::InvalidValue,
54                        &error_message[..],
55                    ));
56                }
57            }
58        }
59
60        // clap checks against number of entries must not allow any other value than 1 and 2 entries
61        _ => {
62            return Err(clap::Error::raw(
63                clap::error::ErrorKind::InvalidValue,
64                UNREACHABLE_CODE,
65            ));
66        }
67    };
68
69    // Function Return values wrap with OK indicating no error
70    Ok((file, size))
71}
72
73/// Generates debug logs if "trace" feature is enabled in `Cargo.toml`
74/// RUST_LOG environment variable needs to be set to "debug" or "trace"
75pub fn trace_init(_test: bool) -> () {
76    #[cfg(feature = "trace")]
77    if _test {
78        env_logger::init();
79    } else {
80        let _ = env_logger::builder().is_test(true).try_init();
81    }
82    #[cfg(not(feature = "trace"))]
83    ();
84}
85
86pub fn trace(_message: &str) -> () {
87    #[cfg(feature = "trace")]
88    log::trace!("{}", _message);
89    #[cfg(not(feature = "trace"))]
90    ();
91}
92
93pub fn error(_message: &str) -> () {
94    #[cfg(feature = "trace")]
95    log::error!("{}", _message);
96    #[cfg(not(feature = "trace"))]
97    ();
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn parse_file_size_arguments_test() {
106        crate::trace_init(false);
107        // test for when both file and size is provided
108        assert_eq!(
109            parse_file_size_arguments(vec![&"./shavee".to_string(), &"2048".to_string()]).unwrap(),
110            (Some(String::from("./shavee")), Some(2048 as u64))
111        );
112
113        // test for when only file is provided
114        assert_eq!(
115            parse_file_size_arguments(vec![&"./shavee".to_string()]).unwrap(),
116            (Some(String::from("./shavee")), None)
117        );
118
119        // test for when there is an empty input
120        assert_eq!(
121            parse_file_size_arguments(vec![&"".to_string()]).unwrap(),
122            (Some(String::from("")), None)
123        );
124
125        // test for reporting error when size is invalid
126        parse_file_size_arguments(vec![&"./shavee".to_string(), &"ten".to_string()]).unwrap_err();
127
128        // test for reporting error when more than 2 entries are provided
129        parse_file_size_arguments(vec![
130            &"./shavee".to_string(),
131            &"2048".to_string(),
132            &"ten".to_string(),
133        ])
134        .unwrap_err();
135    }
136}