diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..70033a5 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,164 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" + +[[package]] +name = "libc" +version = "0.2.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "mvw" +version = "0.1.0" +dependencies = [ + "tempfile", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "rustix" +version = "0.38.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "tempfile" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 8a3f4fd..7ed2320 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +tempfile = "3.14.0" diff --git a/src/main.rs b/src/main.rs index e7a11a9..300fdd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,101 @@ -fn main() { - println!("Hello, world!"); +use std::fs; +use std::fs::File; +use std::path::PathBuf; +use std::path::Path; +use std::io::ErrorKind; +use std::io::Write; +use tempfile::NamedTempFile; + +const LOCK_FILE: &str = ".mvwrap"; + +fn read_current_dir() -> Vec { + let paths : Vec = match fs::read_dir(".") { + Err(e) if e.kind() == ErrorKind::NotFound => Vec::new(), + Err(e) => panic!("Unexpected Error! {:?}", e), + Ok(entries) => entries.filter_map(|e| e.ok()) + .map(|e| e.path()) + .collect() + }; + + let mut path_list : Vec = vec![]; + for path in paths { + let path_string = path.display().to_string(); + if path_string != format!("./{}", LOCK_FILE) { + path_list.push(path_string[2..].to_string()); + } + } + path_list.sort(); + path_list +} + +fn lock_dir() { + if Path::new(LOCK_FILE).is_file() { + panic!("Lock file \"{}\" exists!", LOCK_FILE); + } + let _file = match File::create(LOCK_FILE) { + Ok(file) => file, + Err(_) => panic!("Could not create lock file: {}", LOCK_FILE), + }; +} + +fn unlock_dir() { + if Path::new(LOCK_FILE).is_file() { + let _result = match fs::remove_file(LOCK_FILE){ + Ok(result) => result, + Err(_) => panic!("Could not remove lock file: {}", LOCK_FILE), + }; + } +} + +fn create_temp_file(paths: Vec) -> String { + let mut tmpfile = match NamedTempFile::new(){ + Ok(file) => file, + Err(e) => panic!("Could not create tempfile: {}", e), + }; + + for path in paths { + let _ = writeln!(tmpfile, "{}", path); + } + + let filepath = tmpfile.path().display().to_string(); + + let _ = match tmpfile.keep(){ + Ok(res) => res, + Err(e) => panic!("Can't keep tempfile: {}", e), + }; + + filepath +} + +fn remove_temp_file(tmpfile: String) { + if Path::new(&tmpfile).is_file() { + let _result = match fs::remove_file(&tmpfile){ + Ok(result) => result, + Err(_) => panic!("Could not remove tempfile: {}", &tmpfile), + }; + } +} + +fn main() { + lock_dir(); + + let paths = read_current_dir(); + + // create named tempfile and fill with paths + let temp_file = create_temp_file(paths); + + // edit tempfile + + // read and process tempfile + println!("In file: {}", temp_file); + + let contents = fs::read_to_string(temp_file.clone()) + .expect("Should have been able to read the file"); + + println!("With text:\n{contents}"); + println!("----- END ----"); + + remove_temp_file(temp_file); + + unlock_dir(); }