diff --git a/src/data.rs b/src/data.rs index 6787af6..35f40d1 100644 --- a/src/data.rs +++ b/src/data.rs @@ -5,7 +5,6 @@ use std::{ path::PathBuf, }; -#[cfg(target_family = "unix")] use std::os::unix::fs::DirBuilderExt; use argon2::{ @@ -79,7 +78,7 @@ struct TextMeta { impl TextMeta { pub fn parse(from: &str) -> Result { - let map = meta_str_to_map(from)?; + let mut map = meta_str_to_map(from)?; Ok(Self { language: map.remove("language"), }) @@ -88,8 +87,9 @@ impl TextMeta { impl std::fmt::Display for TextMeta { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let buf = "".to_string(); + let mut buf = "".to_string(); self.language + .as_ref() .map(|lang| buf += format!("{}\n", lang).as_str()); write!(f, "{}---\n", buf) } @@ -101,7 +101,7 @@ struct FileMeta { impl FileMeta { pub fn parse(from: &str) -> Result { - let map = meta_str_to_map(from)?; + let mut map = meta_str_to_map(from)?; let file_name = map.remove("file_name").ok_or(Error::Metadata)?; Ok(Self { file_name }) @@ -210,7 +210,10 @@ impl DataStorage { new_text: Option, new_meta: Option, ) -> Result<(), Error> { - let mut paste = self.text.get(&id).ok_or(Error::ShitMissing(id))?; + let mut paste = self + .text + .get_mut(&id) + .ok_or(Error::ShitMissing(id.clone()))?; new_text.map(|nt| paste.text = nt); new_meta.map(|nm| paste.meta = nm); @@ -221,10 +224,10 @@ impl DataStorage { let old_text = self .text .remove(&id) - .ok_or(Error::ShitMissing(id)) + .ok_or(Error::ShitMissing(id.clone())) .map(|(_, paste)| paste.text)?; - remove_file(self.base_dir.join(*TEXT_DIR).join(id))?; + remove_file(self.base_dir.join(TEXT_DIR.as_str()).join(id))?; Ok(old_text) } @@ -234,17 +237,21 @@ impl DataStorage { return Err(Error::IDCollision); } - self.files.insert(id, file_meta); + self.files.insert(id.clone(), file_meta); self.write_file_meta(id); Ok(()) } pub fn delete_file(&mut self, id: String) -> Result<(), Error> { - let file_meta = self.files.remove(&id).ok_or(Error::ShitMissing(id))?.1; + let file_meta = self + .files + .remove(&id) + .ok_or(Error::ShitMissing(id.clone()))? + .1; - remove_file(self.base_dir.join(*FILE_DIR).join(id))?; - remove_file(self.base_dir.join(*FILE_DIR).join(id + ".meta"))?; + remove_file(self.base_dir.join(FILE_DIR.as_str()).join(id.clone()))?; + remove_file(self.base_dir.join(FILE_DIR.as_str()).join(id + ".meta"))?; Ok(()) } @@ -261,7 +268,7 @@ impl DataStorage { pub fn edit_redirect(&mut self, id: String, new_target: String) -> Result<(), Error> { if !self.redirects.contains_key(&id) { - return Err(Error::ShitMissing(id)); + return Err(Error::ShitMissing(id.clone())); } self.redirects.insert(id, new_target); @@ -270,7 +277,9 @@ impl DataStorage { } pub fn delete_redirect(&mut self, id: String) -> Result<(), Error> { - self.redirects.remove(&id).ok_or(Error::ShitMissing(id))?; + self.redirects + .remove(&id) + .ok_or(Error::ShitMissing(id.clone()))?; self.write_redirects()?; Ok(()) @@ -286,8 +295,10 @@ impl DataStorage { return Err(Error::Exists); } - self.users - .insert(username, UserData::new(username, password, is_admin)?); + self.users.insert( + username.clone(), + UserData::new(username, password, is_admin)?, + ); self.write_users()?; Ok(()) @@ -299,7 +310,7 @@ impl DataStorage { old_password: String, new_password: String, ) -> Result<(), Error> { - let mut user = *self + let mut user = self .users .get_mut(&username) .ok_or(Error::ShitMissing(username))?; @@ -318,14 +329,12 @@ impl DataStorage { } fn ensure_folder_structure(&self) -> Result<(), Error> { - let dir_builder = DirBuilder::new().recursive(true); - if cfg!(target_family = "unix") { - dir_builder = dir_builder.mode(0o775); - } + let mut dir_builder = DirBuilder::new(); + dir_builder.recursive(true).mode(0o775); - dir_builder.create(self.base_dir)?; - dir_builder.create(self.base_dir.join(*FILE_DIR))?; - dir_builder.create(self.base_dir.join(*TEXT_DIR))?; + dir_builder.create(self.base_dir.as_path())?; + dir_builder.create(self.base_dir.join(FILE_DIR.as_str()))?; + dir_builder.create(self.base_dir.join(TEXT_DIR.as_str()))?; Ok(()) } @@ -334,11 +343,13 @@ impl DataStorage { let f = File::options() .create(true) .read(true) - .open(self.base_dir.join(*REDIRECTS_FILE))?; + .open(self.base_dir.join(REDIRECTS_FILE.as_str()))?; for line in io::BufReader::new(f).lines() { - line?; - let (id, url) = line?.split_once(": ").ok_or(Error::Metadata)?; + let (id, url) = line? + .split_once(": ") + .map(|(id, url)| (id.to_string(), url.to_string())) + .ok_or(Error::Metadata)?; self.redirects .insert(id.trim().to_string(), url.trim().to_string()); } @@ -347,13 +358,14 @@ impl DataStorage { } fn load_text(&mut self) -> Result<(), Error> { - for thing in std::fs::read_dir(self.base_dir.join(*TEXT_DIR)).expect("fs error") { + for thing in std::fs::read_dir(self.base_dir.join(TEXT_DIR.as_str())).expect("fs error") { let dir_entry = thing?; // i'm so good at naming things let id = dir_entry.file_name().into_string().or(Err(Error::FS))?; let (raw_metadata, text) = read_to_string(dir_entry.path())? .split_once("---") + .map(|(meta, text)| (meta.to_string(), text.to_string())) .expect("invalid text format"); - let meta = TextMeta::parse(raw_metadata)?; + let meta = TextMeta::parse(raw_metadata.as_str())?; self.text.insert( id, TextPaste { @@ -366,7 +378,7 @@ impl DataStorage { } fn load_files(&mut self) -> Result<(), Error> { - for thing in std::fs::read_dir(self.base_dir.join(*FILE_DIR)).expect("fs error") { + for thing in std::fs::read_dir(self.base_dir.join(FILE_DIR.as_str())).expect("fs error") { let dir_entry = thing?; // i'm so good at naming things let filename = dir_entry .file_name() @@ -384,70 +396,70 @@ impl DataStorage { } fn assert_all_files_present(&self) -> Result<(), Error> { - let files_folder = self.base_dir.join(*FILE_DIR); - for (id, _) in self.files.into_iter() { + let files_folder = self.base_dir.join(FILE_DIR.as_str()); + for meow in self.files.iter() { files_folder - .join(id) + .join(meow.key()) .try_exists() - .or(Err(Error::ShitMissing(id)))?; + .or(Err(Error::ShitMissing(meow.key().to_string())))?; } Ok(()) } fn load_users(&mut self) -> Result<(), Error> { - let is_admin = true; - for line in read_to_string(self.base_dir.join(*USERS_FILE))?.lines() { + let mut is_admin = true; + for line in read_to_string(self.base_dir.join(USERS_FILE.as_str()))?.lines() { if line.starts_with("---") { is_admin = false; break; } let user = UserData::parse(line, is_admin)?; - self.users.insert(user.username, user); + self.users.insert(user.username.clone(), user); } Ok(()) } fn write_redirects(&self) -> Result<(), Error> { - let fuckery: String = self + let mut fuckery: String = self .redirects - .into_iter() - .map(|(id, target)| format!("{}: {}\n", id, target)) + .iter() + .map(|meow| format!("{}: {}\n", meow.key(), meow.value())) .collect(); - std::fs::write(self.base_dir.join(*REDIRECTS_FILE), fuckery)?; + std::fs::write(self.base_dir.join(REDIRECTS_FILE.as_str()), fuckery)?; Ok(()) } fn write_users(&self) -> Result<(), Error> { - let fuckery = String::from("---"); - self.users.into_iter().map(|(_, user)| { - if user.is_admin { - fuckery = format!("{}\n{}", user.to_string(), fuckery) + let mut fuckery = String::from("---"); + self.users.iter().map(|item| { + if item.value().is_admin { + fuckery = format!("{}\n{}", item.value().to_string(), fuckery) } else { - fuckery += format!("\n{}", user.to_string()).as_str() + fuckery += format!("\n{}", item.value().to_string()).as_str() } }); - std::fs::write(self.base_dir.join(*USERS_FILE), fuckery)?; + std::fs::write(self.base_dir.join(USERS_FILE.as_str()), fuckery)?; Ok(()) } fn write_text(&self, id: String) -> Result<(), Error> { - let text_paste = self.text.get(&id).ok_or(Error::ShitMissing(id))?; - let fuckery = text_paste.meta.to_string(); + let text_paste = self.text.get(&id).ok_or(Error::ShitMissing(id.clone()))?; + let mut fuckery = text_paste.meta.to_string(); fuckery += text_paste.text.as_str(); - std::fs::write(self.base_dir.join(*TEXT_DIR).join(id), fuckery)?; + std::fs::write(self.base_dir.join(TEXT_DIR.as_str()).join(id), fuckery)?; Ok(()) } fn write_file_meta(&self, id: String) -> Result<(), Error> { - let file_meta = *self.files.get(&id).ok_or(Error::ShitMissing(id))?; + let file_meta = self.files.get(&id).ok_or(Error::ShitMissing(id.clone()))?; std::fs::write( - self.base_dir.join(*FILE_DIR).join(id), + self.base_dir.join(FILE_DIR.as_str()).join(id), file_meta.to_string(), )?;