From 3ab0da05d5c4ecb235270cfdd17a9d7f50fd66f5 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 10 Feb 2025 17:52:30 +0100 Subject: [PATCH] feat(bin): Add state (de)serialization module --- Cargo.lock | 7 ++++ Cargo.toml | 1 + bin/main.rs | 1 + bin/state.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 bin/state.rs diff --git a/Cargo.lock b/Cargo.lock index 9e954109..0bdb0feb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1299,6 +1299,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.7.4" @@ -2031,6 +2037,7 @@ dependencies = [ "lexopt", "libc", "log", + "md5", "nom", "predicates", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 17ce1fee..6410cab0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ radicle-cli = { version = "0.12.1" } radicle-surf = { version = "0.22.0" } radicle-signals = { version = "0.10.0" } ratatui = { version = "0.29.0", default-features = false, features = ["all-widgets", "termion", "serde"] } +md5 = { version = "0.7.0" } simple-logging = { version = "2.0.2" } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0" } diff --git a/bin/main.rs b/bin/main.rs index c58dcfb3..224a2fad 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -3,6 +3,7 @@ mod commands; mod git; mod log; mod settings; +mod state; mod terminal; #[cfg(test)] mod test; diff --git a/bin/state.rs b/bin/state.rs new file mode 100644 index 00000000..7991de8a --- /dev/null +++ b/bin/state.rs @@ -0,0 +1,98 @@ +use std::path::PathBuf; +use std::{fmt::Display, fs}; + +use anyhow::Result; + +use homedir::my_home; + +use serde::{Deserialize, Serialize}; + +use radicle::cob::ObjectId; +use radicle::identity::RepoId; + +const PATH: &str = ".radicle-tui/states"; + +/// Converts bytes to a deserializable type. +pub fn from_json<'a, D>(bytes: &'a [u8]) -> Result +where + D: Deserialize<'a>, +{ + Ok(serde_json::from_slice(bytes)?) +} + +/// Converts serializable type to bytes. +pub fn to_json(state: S) -> Result> +where + S: Serialize, +{ + Ok(serde_json::to_vec(&state)?) +} + +/// Trait for state readers. +pub trait ReadState { + fn read(&self) -> Result>; +} + +/// Trait for state writers. +pub trait WriteState: ReadState { + fn write(&self, bytes: &[u8]) -> Result<()>; +} + +/// A state storage that reads from and writes to a file. +pub struct FileStore { + path: PathBuf, +} + +impl FileStore { + pub fn new(filename: impl ToString) -> Result { + let folder = match my_home()? { + Some(home) => format!("{}/{}", home.to_string_lossy(), PATH), + _ => anyhow::bail!("Failed to read home directory"), + }; + let path = format!("{}/{}.json", folder, filename.to_string()); + + fs::create_dir_all(folder.clone())?; + + Ok(Self { + path: PathBuf::from(path), + }) + } +} + +impl ReadState for FileStore { + fn read(&self) -> Result> { + let path = PathBuf::from(&self.path); + + Ok(fs::read(path)?) + } +} + +impl WriteState for FileStore { + fn write(&self, contents: &[u8]) -> Result<()> { + let path = PathBuf::from(&self.path); + + Ok(fs::write(path, contents)?) + } +} + +pub struct FileIdentifier { + id: String, +} + +impl FileIdentifier { + pub fn new(command: &str, operation: &str, rid: &RepoId, id: Option<&ObjectId>) -> Self { + let id = match id { + Some(id) => format!("{}-{}-{}-{}", command, operation, rid, id), + _ => format!("{}-{}-{}", command, operation, rid), + }; + let id = format!("{:x}", md5::compute(id)); + + Self { id } + } +} + +impl Display for FileIdentifier { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.id) + } +}