Skip to main content

workflowy_api/common/
parent_id.rs

1use std::borrow::Cow;
2
3use ParentId::*;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Debug)]
7#[serde(rename_all = "snake_case")]
8pub enum ParentId<'a> {
9    #[serde(rename = "None")]
10    Root,
11    Inbox,
12    Calendar,
13    Today,
14    Tomorrow,
15    NextWeek,
16    #[serde(untagged)]
17    Custom(Cow<'a, str>),
18}
19
20impl<'a> ParentId<'a> {
21    pub fn as_ref(&self) -> ParentId<'_> {
22        match self {
23            Root => ParentId::Root,
24            Inbox => ParentId::Inbox,
25            Calendar => ParentId::Calendar,
26            Today => ParentId::Today,
27            Tomorrow => ParentId::Tomorrow,
28            NextWeek => ParentId::NextWeek,
29            Custom(parent_id) => ParentId::Custom(Cow::Borrowed(parent_id.as_ref())),
30        }
31    }
32
33    pub fn into_owned(self) -> ParentId<'static> {
34        match self {
35            Root => ParentId::Root,
36            Inbox => ParentId::Inbox,
37            Calendar => ParentId::Calendar,
38            Today => ParentId::Today,
39            Tomorrow => ParentId::Tomorrow,
40            NextWeek => ParentId::NextWeek,
41            Custom(parent_id) => ParentId::Custom(Cow::Owned(parent_id.into_owned())),
42        }
43    }
44}
45
46impl From<String> for ParentId<'static> {
47    fn from(parent_id: String) -> Self {
48        Custom(Cow::Owned(parent_id))
49    }
50}
51
52impl<'a> From<&'a str> for ParentId<'a> {
53    fn from(parent_id: &'a str) -> Self {
54        Custom(Cow::Borrowed(parent_id))
55    }
56}