First working commit

This commit is contained in:
Dandaedre 2023-05-17 21:21:25 +02:00
parent 39e9466db2
commit 6374360f2f
2 changed files with 54 additions and 10 deletions

View File

@ -9,4 +9,6 @@
## TODO
- Make a lib for dynamic_format
- Make custom errors
- Make custom errors
- Transform all panic in errors
- Make functions into methods

View File

@ -97,8 +97,17 @@ fn print_snippet(args: &Cli) {
let snp: Snippet = serde_yaml::from_reader(f).expect("Couldn't parse values");
// Add the given arguments to the snippet
let final_args = match complete_vec(&args.args, &snp.arguments) {
Err(_) => panic!("Too many arguments given to the snippet, aborting"),
Ok(argg) => argg,
};
// Print the snippet
println!("{:?}", snp);
let f_str = match dynamic_format(&snp.content, final_args) {
Err(_) => panic!("Couldn't format the snippet !"),
Ok(sn) => sn,
};
println!("{}", f_str);
}
/// A snippet ready to be Serialized/Deserialized
@ -124,15 +133,15 @@ impl Snippet {
}
}
/// Save the snippet at the default location under it's name
pub fn save(&self) -> Result<(), ()> {
Ok(())
}
// /// Save the snippet at the default location under it's name
// pub fn save(&self) -> Result<(), ()> {
// Ok(())
// }
/// Tries to load a path and return a snippet
pub fn load(path: String) -> Result<Self, ()> {
Err(())
}
// pub fn load(path: String) -> Result<Self, ()> {
// Err(())
// }
/// Write to stdout the snipped with either default arguments or given arguments
pub fn print(&self) -> Result<(), ()> {
@ -153,7 +162,7 @@ where
T: std::fmt::Display,
{
let collected_format_string: Vec<&str> = format_string.split/*_inclusive*/("{}").collect();
println!("{:?}", collected_format_string);
// println!("{:?}", collected_format_string);
let size_str = collected_format_string.len();
let size_vec = vector.len();
@ -167,6 +176,26 @@ where
Ok(fmt_vec.join(""))
}
/// Fill the argument vector with a list of defaults values if needed
pub fn complete_vec<T>(to_fill: &Vec<T>, defaults: &Vec<T>) -> Result<Vec<T>, ()>
where
T: Clone,
{
let size_o = to_fill.len();
let size_d = defaults.len();
match size_o.cmp(&size_d) {
std::cmp::Ordering::Greater => Err(()),
std::cmp::Ordering::Equal => Ok(to_fill.clone()),
std::cmp::Ordering::Less => {
let mut output = to_fill.clone();
(size_o..size_d).for_each(|i| {
output.push(defaults[i].clone());
});
Ok(output)
}
}
}
#[cfg(test)]
mod test {
use super::*;
@ -178,4 +207,17 @@ mod test {
);
assert_eq!(res, Ok("big str aaa with many bbb formatters".to_string()));
}
#[test]
fn test_complete_vec() {
let smol = vec!["A"];
let big = vec!["a", "b", "c", "d", "e", "f"];
let eq = vec!["T", "U", "I", "O"];
let deff = vec!["AA", "BB", "CC", "DD"];
assert_eq!(
complete_vec(&smol, &deff).unwrap(),
vec!["A", "BB", "CC", "DD"]
);
assert_eq!(complete_vec(&eq, &deff).unwrap(), eq);
}
}