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
11pub const STATIC_SALT: &str = "This Project is Dedicated to Tamanna.";
13
14pub const ENV_SALT_VARIABLE: &str = "SHAVEE_SALT";
16
17pub const RANDOM_SALT_LEN: usize = 32;
20
21use clap;
22#[cfg(feature = "trace")]
23use env_logger;
24#[cfg(feature = "trace")]
25use log;
26
27pub 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 let size = match file_size_argument.len() {
36 number_of_entries if number_of_entries == 1 => None,
38 number_of_entries if number_of_entries == 2 => {
40 let second_entry = file_size_argument[1];
42 match second_entry.parse::<u64>() {
44 Ok(size_arg) => Some(size_arg),
46
47 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 _ => {
62 return Err(clap::Error::raw(
63 clap::error::ErrorKind::InvalidValue,
64 UNREACHABLE_CODE,
65 ));
66 }
67 };
68
69 Ok((file, size))
71}
72
73pub 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 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 assert_eq!(
115 parse_file_size_arguments(vec![&"./shavee".to_string()]).unwrap(),
116 (Some(String::from("./shavee")), None)
117 );
118
119 assert_eq!(
121 parse_file_size_arguments(vec![&"".to_string()]).unwrap(),
122 (Some(String::from("")), None)
123 );
124
125 parse_file_size_arguments(vec![&"./shavee".to_string(), &"ten".to_string()]).unwrap_err();
127
128 parse_file_size_arguments(vec![
130 &"./shavee".to_string(),
131 &"2048".to_string(),
132 &"ten".to_string(),
133 ])
134 .unwrap_err();
135 }
136}