Compare commits

...

2 Commits

Author SHA1 Message Date
cc393ce2c0 feat: add basic errors 2025-07-24 11:58:49 +02:00
7a8d6e0e74 feat: add uuid to logs 2025-07-24 11:57:19 +02:00
4 changed files with 26 additions and 6 deletions

View File

@ -7,3 +7,4 @@ edition = "2024"
chrono = { version = "0.4.41", features = ["serde"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.141"
uuid = { version = "1.17.0", features = ["serde", "v4"] }

View File

@ -19,9 +19,14 @@ impl App {
Self::default()
}
pub fn add(&mut self, item: Item) {
pub fn add(&mut self, item: Item) -> Result<()> {
self.logs.push(item);
let _ = self.save();
self.save()
}
pub fn remove<T: AsRef<str>>(&mut self, id: T) -> Result<()> {
self.logs.retain(|i| i.id() != id.as_ref());
self.save()
}
pub fn save(&self) -> Result<()> {

View File

@ -1,8 +1,10 @@
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Item {
id: Uuid,
content: String,
created: DateTime<Local>,
modified: DateTime<Local>,
@ -17,12 +19,17 @@ impl Item {
self.content = content;
self.modified = Local::now();
}
pub fn id(&self) -> String {
self.id.to_string()
}
}
impl Default for Item {
fn default() -> Self {
let now = Local::now();
Self {
id: Uuid::new_v4(),
content: String::new(),
created: now,
modified: now,

View File

@ -1,8 +1,15 @@
use lw::App;
use std::error::Error;
fn main() {
use lw::{App, log::Item};
fn main() -> Result<(), Box<dyn Error>> {
let mut app = App::default();
app.add("hello_world".into());
app.save().unwrap();
let item = Item::from("hello_world");
let id = item.id();
app.add(item)?;
println!("{app:?}");
app.remove(id)?;
app.save()?;
println!("{app:?}");
Ok(())
}