Skip to main content

agent_first_mail/config/
archive.rs

1use super::defaults::default_archive_message_index_fields;
2use crate::error::{AppError, Result};
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
6#[serde(deny_unknown_fields)]
7pub struct ArchiveSection {
8    #[serde(default)]
9    pub message_index: ArchiveMessageIndexSection,
10}
11
12impl ArchiveSection {
13    pub(super) fn validate(&self) -> Result<()> {
14        if self.message_index.item_fields.is_empty() {
15            return Err(AppError::new(
16                "config_invalid",
17                "archive.message_index.item_fields must contain at least one field",
18            ));
19        }
20        Ok(())
21    }
22}
23
24#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
25#[serde(deny_unknown_fields)]
26pub struct ArchiveMessageIndexSection {
27    #[serde(default = "default_archive_message_index_fields")]
28    pub item_fields: Vec<ArchiveMessageIndexField>,
29    #[serde(default)]
30    pub sort: ArchiveMessageIndexSort,
31}
32
33impl Default for ArchiveMessageIndexSection {
34    fn default() -> Self {
35        Self {
36            item_fields: default_archive_message_index_fields(),
37            sort: ArchiveMessageIndexSort::default(),
38        }
39    }
40}
41
42#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
43#[serde(rename_all = "snake_case")]
44pub enum ArchiveMessageIndexField {
45    Time,
46    From,
47    To,
48    Subject,
49    Summary,
50    MessageId,
51    ArchiveTime,
52    Link,
53}
54
55impl ArchiveMessageIndexField {
56    pub fn as_str(self) -> &'static str {
57        match self {
58            Self::Time => "time",
59            Self::From => "from",
60            Self::To => "to",
61            Self::Subject => "subject",
62            Self::Summary => "summary",
63            Self::MessageId => "message_id",
64            Self::ArchiveTime => "archive_time",
65            Self::Link => "link",
66        }
67    }
68
69    pub(super) fn parse(value: &str) -> Result<Self> {
70        match value {
71            "time" => Ok(Self::Time),
72            "from" => Ok(Self::From),
73            "to" => Ok(Self::To),
74            "subject" => Ok(Self::Subject),
75            "summary" => Ok(Self::Summary),
76            "message_id" => Ok(Self::MessageId),
77            "archive_time" => Ok(Self::ArchiveTime),
78            "link" => Ok(Self::Link),
79            _ => Err(AppError::new(
80                "invalid_request",
81                format!("archive.message_index.item_fields contains unsupported field: {value}"),
82            )),
83        }
84    }
85}
86
87#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
88#[serde(rename_all = "snake_case")]
89pub enum ArchiveMessageIndexSort {
90    #[default]
91    DateDesc,
92}
93
94impl ArchiveMessageIndexSort {
95    pub fn as_str(self) -> &'static str {
96        match self {
97            Self::DateDesc => "date_desc",
98        }
99    }
100
101    pub(super) fn parse(value: &str) -> Result<Self> {
102        match value {
103            "date_desc" => Ok(Self::DateDesc),
104            _ => Err(AppError::new(
105                "invalid_request",
106                "archive.message_index.sort expects date_desc",
107            )),
108        }
109    }
110}