From 6e81d1b4e6d7e9e95c3085f545140064b33bdddb Mon Sep 17 00:00:00 2001 From: xenua Date: Fri, 6 Jan 2023 23:32:31 +0100 Subject: [PATCH] more work --- src/data.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/src/data.rs b/src/data.rs index 61fe1b5..c6930ca 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - fs::{read_to_string, DirBuilder, File}, + fs::{read_to_string, remove_file, DirBuilder, File}, io::{self, BufRead}, path::PathBuf, }; @@ -30,6 +30,8 @@ pub enum Error { ShitMissing(String), #[error("filesystem error")] FS, + #[error("id collision wtf")] + IDCollision, } fn meta_str_to_map(from: &str) -> Result, Error> { @@ -65,6 +67,15 @@ impl TextMeta { } } +impl std::fmt::Display for TextMeta { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let buf = "".to_string(); + self.language + .map(|lang| buf += format!("{}\n", lang).as_str()); + write!(f, "{}---\n", buf) + } +} + struct FileMeta { file_name: String, } @@ -128,6 +139,73 @@ impl DataStorage { Ok(()) } + pub fn create_text(&mut self, id: String, text: String, meta: TextMeta) -> Result<(), Error> { + if self.text.contains_key(&id) { + return Err(Error::IDCollision); + } + self.text.insert(id, TextPaste { text, meta }); + Ok(()) + } + + pub fn edit_text( + &mut self, + id: String, + new_text: Option, + new_meta: Option, + ) -> Result<(), Error> { + let mut paste = self.text.get(&id).ok_or(Error::ShitMissing(id))?; + new_text.map(|nt| paste.text = nt); + new_meta.map(|nm| paste.meta = nm); + + Ok(()) + } + + pub fn delete_text(&mut self, id: String) -> Result { + let old_text = self + .text + .remove(&id) + .ok_or(Error::ShitMissing(id)) + .map(|(_, paste)| paste.text)?; + + remove_file(self.base_dir.join(*TEXT_DIR).join(id))?; + + Ok(old_text) + } + + pub fn create_file(&mut self, id: String, file_meta: FileMeta) -> Result<(), Error> { + todo!() + } + + pub fn delete_file(&mut self, id: String) -> Result<(), Error> { + let file_meta = self.files.remove(&id).ok_or(Error::ShitMissing(id))?.1; + + remove_file(self.base_dir.join(*FILE_DIR).join(id))?; + remove_file(self.base_dir.join(*FILE_DIR).join(id + ".meta"))?; + + Ok(()) + } + + pub fn create_redirect(&mut self, id: String, target: String) -> Result<(), Error> { + todo!() + } + + pub fn edit_redirect(&mut self, id: String, new_target: String) -> Result<(), Error> { + if !self.redirects.contains_key(&id) { + return Err(Error::ShitMissing(id)); + } + self.redirects.insert(id, new_target); + + self.write_redirects()?; + Ok(()) + } + + pub fn delete_redirect(&mut self, id: String) -> Result<(), Error> { + self.redirects.remove(&id).ok_or(Error::ShitMissing(id))?; + + self.write_redirects()?; + Ok(()) + } + fn ensure_folder_structure(&self) -> Result<(), Error> { let dir_builder = DirBuilder::new().recursive(true); if cfg!(target_family = "unix") { @@ -220,4 +298,25 @@ impl DataStorage { } Ok(()) } + + fn write_redirects(&self) -> Result<(), Error> { + let fuckery: String = self + .redirects + .into_iter() + .map(|(id, target)| format!("{}: {}\n", id, target)) + .collect(); + std::fs::write(self.base_dir.join(*REDIRECTS_FILE), 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(); + fuckery += text_paste.text.as_str(); + + std::fs::write(self.base_dir.join(*TEXT_DIR).join(id), fuckery)?; + + Ok(()) + } }