Updates TODO and Adds a lot of progress on rework
This commit is contained in:
parent
d2cb3f4295
commit
8937ce7fd4
5
TODO.md
5
TODO.md
|
|
@ -8,8 +8,9 @@
|
|||
[ ]
|
||||
|
||||
## TODO
|
||||
[ ] Make a lib for dynamic_format
|
||||
[ ] Make a lib for dynamic_format and snippets (impl make a trait for have .id() and other future t)
|
||||
[ ] Make custom errors
|
||||
[ ] Transform all panic in errors
|
||||
[ ] Make functions into methods
|
||||
[ ] Have identical attributes with {0} {1} {0} (^^^^^)
|
||||
[-] Have identical attributes with {0} {1} {0} (^^^^^)
|
||||
[ ] Update tests
|
||||
90
src/main.rs
90
src/main.rs
|
|
@ -86,12 +86,13 @@ macro_rules! push_if_some {
|
|||
}
|
||||
/// Create a token list from the snippet given and return it
|
||||
pub fn create_tokens_from_string(snippet: &str) {
|
||||
let re = Regex::new(r"(\{(\d*)\})").unwrap();
|
||||
let re = Regex::new(r"\{\d*\}").unwrap();
|
||||
let matches = re.find_iter(snippet);
|
||||
let mut readhead = 0; // Starting to read at zero
|
||||
let mut token_list = Vec::new();
|
||||
for r in matches {
|
||||
// Assume we matched text from read to here
|
||||
println!("{:?}", r);
|
||||
push_if_some!(
|
||||
token_list,
|
||||
create_token_from_pos(snippet, Status::Text, readhead, Some(r.start()))
|
||||
|
|
@ -128,9 +129,6 @@ fn create_token_from_pos(
|
|||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
// let origin = "Fill the blank bébé";
|
||||
// let edited = edit::edit(origin).unwrap();
|
||||
// println!("{}", edited);
|
||||
match cli.create {
|
||||
true => create_snippet(cli),
|
||||
false => print_snippet(&cli),
|
||||
|
|
@ -142,14 +140,6 @@ fn main() {
|
|||
/// * Create a snippet with the given data
|
||||
/// * Write the snippet
|
||||
fn create_snippet(args: Cli) {
|
||||
// Find if the snippet exists
|
||||
let mut save_path = match config_folder() {
|
||||
Ok(path) => path,
|
||||
Err(e) => panic!(
|
||||
"The OS couldn't locate a place to save the snippets.\n {}",
|
||||
e
|
||||
),
|
||||
};
|
||||
// if args.verbose println!("{}",save_path);
|
||||
let string = if args.edit {
|
||||
edit::edit("").unwrap()
|
||||
|
|
@ -159,31 +149,39 @@ fn create_snippet(args: Cli) {
|
|||
|
||||
// Create a snippet with the given data
|
||||
let snp = Snippet::new(args.snipplet.clone(), args.args, string);
|
||||
let (file, save_path) = open_path(&snp);
|
||||
if args.locate {
|
||||
println!("Saving snippet to {:?}", save_path);
|
||||
}
|
||||
|
||||
let mut file = match create_dir_all(&save_path) {
|
||||
// Write the snippet
|
||||
snp.save(file)
|
||||
.unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path));
|
||||
}
|
||||
|
||||
/// Open the path linked to a snippet
|
||||
fn open_path(snp: &Snippet) -> (File, PathBuf) {
|
||||
// Find if the snippet exists
|
||||
let mut save_path = match config_folder() {
|
||||
Ok(path) => path,
|
||||
Err(e) => panic!(
|
||||
"The OS couldn't locate a place to save the snippets.\n {}",
|
||||
e
|
||||
),
|
||||
};
|
||||
|
||||
let file = match create_dir_all(&save_path) {
|
||||
Ok(()) => {
|
||||
save_path.push(format!("{}.yaml", args.snipplet));
|
||||
if args.locate {
|
||||
println!("Saving snippet to {:?}", save_path);
|
||||
}
|
||||
save_path.push(format!("{}.yaml", snp.name));
|
||||
File::create(&save_path).unwrap_or_else(|e| {
|
||||
panic!("Couldn't create snippet file at {:?} {:}", save_path, e)
|
||||
})
|
||||
}
|
||||
Err(e) => panic!("Couldn't write to path {:?}. {}", save_path, e),
|
||||
};
|
||||
// snp.save()
|
||||
// Write the snippet
|
||||
write!(
|
||||
file,
|
||||
"{}",
|
||||
serde_yaml::to_string(&snp).expect("Couldn't convert the given arguments into a yaml")
|
||||
)
|
||||
.unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path));
|
||||
(file, save_path)
|
||||
}
|
||||
|
||||
// fn open_path()
|
||||
|
||||
/// Find a configuration folder/name for the given snippet name
|
||||
fn config_folder() -> io::Result<PathBuf> {
|
||||
let exe = env::current_exe()?;
|
||||
|
|
@ -203,7 +201,7 @@ 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) {
|
||||
let final_args = match complete_vec(&args.args, &snp.default_values) {
|
||||
Err(_) => panic!("Too many arguments given to the snippet, aborting"),
|
||||
Ok(argg) => argg,
|
||||
};
|
||||
|
|
@ -222,7 +220,6 @@ fn print_snippet(args: &Cli) {
|
|||
(false, true) => edit_snippet(&snp),
|
||||
(false, false) => (),
|
||||
}
|
||||
create_tokens_from_string(&f_str);
|
||||
println!("{}", f_str);
|
||||
}
|
||||
|
||||
|
|
@ -234,9 +231,11 @@ fn print_path(path: &Path) {
|
|||
/// Edit the snippet and write it again
|
||||
fn edit_snippet(snp: &Snippet) {
|
||||
let test = edit::edit(&snp.content).expect("Couldn't edit the snippet with the editor");
|
||||
let snp = Snippet::new(snp.name.clone(), snp.arguments.clone(), test);
|
||||
let snp = Snippet::new(snp.name.clone(), snp.default_values.clone(), test);
|
||||
// TODO: Write snippet . Destroy original ? Return new ?
|
||||
// snp.save();
|
||||
let (file, path) = open_path(&snp);
|
||||
snp.save(file)
|
||||
.unwrap_or_else(|e| println!("Coudln't save to file {:?} {}", path, e));
|
||||
}
|
||||
|
||||
/// A snippet ready to be Serialized/Deserialized
|
||||
|
|
@ -246,7 +245,7 @@ pub struct Snippet {
|
|||
name: String,
|
||||
|
||||
/// The default value of all args
|
||||
arguments: Vec<String>,
|
||||
default_values: Vec<String>,
|
||||
|
||||
/// The base string to format
|
||||
content: String,
|
||||
|
|
@ -254,18 +253,22 @@ pub struct Snippet {
|
|||
|
||||
impl Snippet {
|
||||
/// Create a new snippet
|
||||
pub fn new(name: String, arguments: Vec<String>, content: String) -> Self {
|
||||
pub fn new(name: String, default_values: Vec<String>, content: String) -> Self {
|
||||
Snippet {
|
||||
name,
|
||||
arguments,
|
||||
default_values,
|
||||
content,
|
||||
}
|
||||
}
|
||||
|
||||
// /// 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, mut file: File) -> Result<(), std::io::Error> {
|
||||
write!(
|
||||
file,
|
||||
"{}",
|
||||
serde_yaml::to_string(self).expect("Couldn't convert the given arguments into a yaml")
|
||||
)
|
||||
}
|
||||
|
||||
/// Tries to load a path and return a snippet
|
||||
// pub fn load(path: String) -> Result<Self, ()> {
|
||||
|
|
@ -278,10 +281,6 @@ impl Snippet {
|
|||
}
|
||||
}
|
||||
|
||||
// pub struct AccessError{
|
||||
|
||||
// }
|
||||
|
||||
/// A dynamic formatter, formatting a string with any number of
|
||||
/// {} with element of a Vec, by default the search doen't need to
|
||||
/// be exhaustive and wont fail if either the array or format string don't
|
||||
|
|
@ -290,8 +289,11 @@ pub fn dynamic_format<T>(format_string: &str, vector: Vec<T>) -> Result<String,
|
|||
where
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
let collected_format_string: Vec<&str> = format_string.split/*_inclusive*/("{}").collect();
|
||||
// println!("{:?}", collected_format_string);
|
||||
// TODO: RESTART FROM HERE, do the permutation
|
||||
// Implement a to_str or a collect for vec<Formatter>
|
||||
create_tokens_from_string(format_string);
|
||||
// println!("{}",);
|
||||
let collected_format_string: Vec<&str> = format_string.split("{}").collect();
|
||||
let size_str = collected_format_string.len();
|
||||
let size_vec = vector.len();
|
||||
|
||||
|
|
|
|||
Reference in New Issue