Skip to content

Commit

Permalink
fixed uid bug
Browse files Browse the repository at this point in the history
  • Loading branch information
paulchambaz committed Jan 14, 2025
1 parent b67867e commit 192f453
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
];

devPkgs = with pkgs; [
cargo
rustc
just
cargo-tarpaulin
vhs
Expand Down
15 changes: 7 additions & 8 deletions src/calendar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::{anyhow, Result};
use chrono::NaiveDateTime;
use std::path::PathBuf;
use uuid::Uuid;

use crate::storage;

Expand All @@ -13,7 +12,7 @@ pub struct Calendar {

#[derive(Debug, Clone)]
pub struct Event {
pub id: Uuid,
pub id: String,
pub name: String,
pub start: NaiveDateTime,
pub end: NaiveDateTime,
Expand Down Expand Up @@ -45,9 +44,9 @@ impl Calendar {
Ok(())
}

pub fn remove_event(&mut self, event_id: Uuid) -> Result<()> {
pub fn remove_event(&mut self, event_id: String) -> Result<()> {
let _ = self
.get_event(event_id)
.get_event(event_id.clone())
.ok_or_else(|| anyhow!("Could not find event with this uuid"))?;

storage::delete_event(&self.path, event_id)?;
Expand All @@ -57,7 +56,7 @@ impl Calendar {

pub fn edit_event(
&mut self,
id: Uuid,
id: String,
name: Option<String>,
start: Option<NaiveDateTime>,
end: Option<NaiveDateTime>,
Expand Down Expand Up @@ -90,11 +89,11 @@ impl Calendar {
Ok(())
}

pub fn get_event(&self, id: Uuid) -> Option<&Event> {
pub fn get_event(&self, id: String) -> Option<&Event> {
self.events.iter().find(|e| e.id == id)
}

fn get_event_mut(&mut self, id: Uuid) -> Option<&mut Event> {
fn get_event_mut(&mut self, id: String) -> Option<&mut Event> {
self.events.iter_mut().find(|e| e.id == id)
}
}
Expand All @@ -108,7 +107,7 @@ impl Event {
description: Option<String>,
) -> Self {
Event {
id: Uuid::new_v4(),
id: String::new(),
name,
start,
end,
Expand Down
17 changes: 6 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use anyhow::{anyhow, Result};
use chrono::{Duration, NaiveDate, NaiveDateTime};
use clap::{Parser, Subcommand};
use std::str::FromStr;
use uuid::Uuid;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -64,7 +63,7 @@ pub struct CalendarAddArgs {

#[derive(Debug)]
pub struct CalendarEditArgs {
pub event_id: Uuid,
pub event_id: String,
pub calendar: String,
pub name: Option<String>,
pub start: Option<NaiveDateTime>,
Expand All @@ -75,14 +74,14 @@ pub struct CalendarEditArgs {

#[derive(Debug)]
pub struct CalendarDeleteArgs {
pub event_id: Uuid,
pub event_id: String,
pub calendar: String,
pub force: bool,
}

#[derive(Debug)]
pub struct CalendarShowArgs {
pub event_id: Uuid,
pub event_id: String,
pub calendar: String,
}

Expand Down Expand Up @@ -295,10 +294,6 @@ fn parse_datetime(datetime_str: &str) -> Result<NaiveDateTime> {
Ok(CalendarDateTime::parse(datetime_str)?.inner())
}

fn parse_uuid(uuid_str: &str) -> Result<Uuid> {
Uuid::parse_str(uuid_str).map_err(|e| anyhow!("Invalid UUID: {}", e))
}

impl ListArgs {
pub fn validate(self) -> Result<CalendarListArgs> {
let query: Option<String> = Some(self.query.join(" "))
Expand Down Expand Up @@ -391,7 +386,7 @@ impl EditArgs {
pub fn validate(self) -> Result<CalendarEditArgs> {
let calendar = self.calendar.unwrap_or_else(|| "personal".to_string());

let event_id = parse_uuid(&self.event_id)?;
let event_id = self.event_id;
let start = self.at.map(|w| parse_datetime(&w)).transpose()?;
let end = self.to.map(|t| parse_datetime(&t)).transpose()?;

Expand All @@ -416,7 +411,7 @@ impl EditArgs {
impl DeleteArgs {
pub fn validate(self) -> Result<CalendarDeleteArgs> {
let calendar = self.calendar.unwrap_or_else(|| "personal".to_string());
let event_id = parse_uuid(&self.event_id)?;
let event_id = self.event_id;
Ok(CalendarDeleteArgs {
event_id,
calendar,
Expand Down Expand Up @@ -448,7 +443,7 @@ impl ViewArgs {
impl ShowArgs {
pub fn validate(self) -> Result<CalendarShowArgs> {
let calendar = self.calendar.unwrap_or_else(|| "personal".to_string());
let event_id = parse_uuid(&self.event_id)?;
let event_id = self.event_id;
Ok(CalendarShowArgs { event_id, calendar })
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub fn delete(cmd: cli::CalendarDeleteArgs) -> Result<()> {

if !cmd.force {
let event = calendar
.get_event(cmd.event_id)
.get_event(cmd.event_id.clone())
.ok_or_else(|| anyhow!("Could not find event with this uuid"))?;
print!(
"You are about to delete '{}', are you sure? (y/N) ",
Expand Down
6 changes: 3 additions & 3 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn read_event(path: &Path) -> Result<Event> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut event = Event {
id: Uuid::new_v4(),
id: String::new(),
name: String::new(),
start: Utc::now().naive_utc(),
end: Utc::now().naive_utc(),
Expand All @@ -132,7 +132,7 @@ fn read_event(path: &Path) -> Result<Event> {
let main_key = key_parts[0];

match main_key {
"UID" => event.id = Uuid::parse_str(value)?,
"UID" => event.id = value.to_string(),
"SUMMARY" => event.name = value.to_string(),
"LOCATION" => event.location = Some(value.to_string()),
"DESCRIPTION" => event.description = Some(value.to_string()),
Expand Down Expand Up @@ -217,7 +217,7 @@ pub fn write_event(calendar_path: &Path, event: &Event) -> Result<()> {
Ok(())
}

pub fn delete_event(calendar_path: &Path, event_id: Uuid) -> Result<()> {
pub fn delete_event(calendar_path: &Path, event_id: String) -> Result<()> {
let filename = format!("{}.ics", event_id);
let file_path = calendar_path.join(filename);

Expand Down

0 comments on commit 192f453

Please sign in to comment.