replace string settings with typed enums
- preserve local wall-time handling and import completion timestamps - validate invalid properties without overwriting items
This commit is contained in:
127
src/model.rs
127
src/model.rs
@@ -1,3 +1,5 @@
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Item {
|
||||
pub id: i64,
|
||||
@@ -30,7 +32,7 @@ pub struct Category {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub parent_id: Option<i64>,
|
||||
pub kind: String,
|
||||
pub kind: CategoryKind,
|
||||
pub match_text: String,
|
||||
pub exclusive: bool,
|
||||
}
|
||||
@@ -39,9 +41,9 @@ pub struct Category {
|
||||
pub struct ViewDef {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub kind: String,
|
||||
pub kind: ViewKind,
|
||||
pub filter_value: String,
|
||||
pub sort_key: String,
|
||||
pub sort_key: SortKey,
|
||||
pub show_done: bool,
|
||||
pub filter_expr: String,
|
||||
pub columns: Vec<ViewColumn>,
|
||||
@@ -53,7 +55,7 @@ pub struct ViewColumn {
|
||||
pub field: String,
|
||||
pub heading: String,
|
||||
pub width: u16,
|
||||
pub aggregate: String,
|
||||
pub aggregate: Aggregate,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -93,7 +95,7 @@ pub struct CategoryRule {
|
||||
pub id: i64,
|
||||
pub category_id: i64,
|
||||
pub condition_expr: String,
|
||||
pub action_kind: String,
|
||||
pub action_kind: RuleActionKind,
|
||||
pub action_value: String,
|
||||
pub enabled: bool,
|
||||
}
|
||||
@@ -121,11 +123,11 @@ pub struct ItemChanges {
|
||||
pub struct DocumentSettings {
|
||||
pub description: String,
|
||||
pub backup_on_open: bool,
|
||||
pub trash_policy: String,
|
||||
pub done_policy: String,
|
||||
pub trash_policy: TrashPolicy,
|
||||
pub done_policy: DonePolicy,
|
||||
pub automatic_filing: bool,
|
||||
pub date_order: String,
|
||||
pub week_start: String,
|
||||
pub date_order: DateOrder,
|
||||
pub week_start: WeekStart,
|
||||
pub default_time: String,
|
||||
pub morning_time: String,
|
||||
pub afternoon_time: String,
|
||||
@@ -138,11 +140,11 @@ impl Default for DocumentSettings {
|
||||
Self {
|
||||
description: String::new(),
|
||||
backup_on_open: false,
|
||||
trash_policy: "on-demand".into(),
|
||||
done_policy: "keep".into(),
|
||||
trash_policy: TrashPolicy::OnDemand,
|
||||
done_policy: DonePolicy::Keep,
|
||||
automatic_filing: true,
|
||||
date_order: "ymd".into(),
|
||||
week_start: "monday".into(),
|
||||
date_order: DateOrder::YearMonthDay,
|
||||
week_start: WeekStart::Monday,
|
||||
default_time: "09:00".into(),
|
||||
morning_time: "09:00".into(),
|
||||
afternoon_time: "13:00".into(),
|
||||
@@ -151,3 +153,102 @@ impl Default for DocumentSettings {
|
||||
}
|
||||
}
|
||||
}
|
||||
macro_rules! string_enum {
|
||||
($name:ident { $($variant:ident => $value:literal),+ $(,)? }) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum $name {
|
||||
$($variant),+
|
||||
}
|
||||
|
||||
impl $name {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
$(Self::$variant => $value),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for $name {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
match value {
|
||||
$($value => Ok(Self::$variant),)+
|
||||
_ => Err(format!("unsupported {} {value:?}", stringify!($name))),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
string_enum!(CategoryKind {
|
||||
Standard => "standard",
|
||||
Date => "date",
|
||||
Numeric => "numeric",
|
||||
});
|
||||
|
||||
string_enum!(ViewKind {
|
||||
List => "list",
|
||||
Category => "category",
|
||||
Upcoming => "upcoming",
|
||||
Done => "done",
|
||||
Datebook => "datebook",
|
||||
Trash => "trash",
|
||||
});
|
||||
|
||||
string_enum!(SortKey {
|
||||
Manual => "manual",
|
||||
When => "when",
|
||||
Done => "done",
|
||||
Priority => "priority",
|
||||
Updated => "updated",
|
||||
});
|
||||
|
||||
string_enum!(Aggregate {
|
||||
None => "none",
|
||||
Sum => "sum",
|
||||
Average => "avg",
|
||||
Count => "count",
|
||||
Minimum => "min",
|
||||
Maximum => "max",
|
||||
});
|
||||
|
||||
string_enum!(RuleActionKind {
|
||||
Assign => "assign",
|
||||
Exclude => "exclude",
|
||||
Priority => "priority",
|
||||
Value => "value",
|
||||
When => "when",
|
||||
Alarm => "alarm",
|
||||
Repeat => "repeat",
|
||||
Done => "done",
|
||||
});
|
||||
|
||||
string_enum!(TrashPolicy {
|
||||
OnDemand => "on-demand",
|
||||
OnClose => "on-close",
|
||||
EndOfDay => "end-of-day",
|
||||
Immediate => "immediate",
|
||||
});
|
||||
|
||||
string_enum!(DonePolicy {
|
||||
Keep => "keep",
|
||||
Trash => "trash",
|
||||
});
|
||||
|
||||
string_enum!(DateOrder {
|
||||
YearMonthDay => "ymd",
|
||||
MonthDayYear => "mdy",
|
||||
DayMonthYear => "dmy",
|
||||
});
|
||||
|
||||
string_enum!(WeekStart {
|
||||
Monday => "monday",
|
||||
Sunday => "sunday",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user