From 0b586ecef0ed9b66980256a2b73049dce00044db Mon Sep 17 00:00:00 2001 From: KeksNino Date: Thu, 3 Sep 2026 15:18:53 +0200 Subject: [PATCH] feat: add function to parse editor output in edit --- src/edit.rs | 30 ++++++++++++++++++------------ src/main.rs | 4 ++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/edit.rs b/src/edit.rs index c21fa93..2a5916d 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -1,31 +1,37 @@ use lofty::tag::Tag; +use std::collections::HashMap; use std::env::var; use std::io::Write; use std::path::PathBuf; use std::{fs::File, io::Read, process::Command}; -pub fn edit(file_path: PathBuf, tag: Tag) -> std::io::Result<()> { +pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> { + let tmp_file = PathBuf::from("/tmp/keks-sort-metadata"); let editor = var("EDITOR").unwrap(); - let mut file = File::create(&file_path)?; + let mut file = File::create(&tmp_file)?; for item in tag.items() { - writeln!( - file, - "{:?} = {:?}", - item.key(), - item.value().text().unwrap_or("") - )?; + writeln!(file, "{:?} = {}", item.key(), item.value().text().unwrap())?; } Command::new(editor) - .arg(&file_path) + .arg(&tmp_file) .status() .expect("Something went wrong"); let mut editable = String::new(); - let _ = File::open(file_path) + File::open(&tmp_file) .expect("Could not open file") - .read_to_string(&mut editable); + .read_to_string(&mut editable)?; - println!("File content:\n{}", editable); Ok(()) } + +fn parse(input: &str) -> HashMap { + input + .lines() + .filter_map(|line| { + let (key, value) = line.split_once('=')?; + Some((key.trim().to_string(), value.trim().to_string())) + }) + .collect() +} diff --git a/src/main.rs b/src/main.rs index 0e4d54c..1b9f0bb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -72,9 +72,8 @@ fn main() -> Result<(), Box> { } } Commands::Edit { file } => { - let path = PathBuf::from("/tmp/keks-sort-metadata"); let tag = get_metadata(file); - edit(path, tag)?; + edit(file.to_path_buf(), tag)?; } } @@ -91,5 +90,6 @@ fn get_metadata>(file_path: P) -> lofty::tag::Tag { Some(primary_tag) => primary_tag, None => tagged_file.first_tag().expect("ERROR: No tags found!"), }; + tag.clone() }