Skip to main content

syncweb_core/folder/
sync_mode.rs

1use std::{fmt, str::FromStr};
2
3use crate::error::{Result, SyncwebError};
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
7#[non_exhaustive]
8pub enum SyncMode {
9    #[default]
10    SendReceive,
11    SendOnly,
12    ReceiveOnly,
13    ReceiveEncrypted,
14    PublicReadOnly,
15}
16
17impl SyncMode {
18    #[must_use]
19    pub const fn can_write(self) -> bool {
20        matches!(self, Self::SendReceive | Self::SendOnly)
21    }
22
23    #[must_use]
24    pub const fn can_receive(self) -> bool {
25        !matches!(self, Self::SendOnly)
26    }
27
28    #[must_use]
29    pub const fn is_public(self) -> bool {
30        matches!(self, Self::PublicReadOnly)
31    }
32}
33
34impl fmt::Display for SyncMode {
35    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36        formatter.write_str(match self {
37            Self::SendReceive => "sendreceive",
38            Self::SendOnly => "sendonly",
39            Self::ReceiveOnly => "receiveonly",
40            Self::ReceiveEncrypted => "receiveencrypted",
41            Self::PublicReadOnly => "publicreadonly",
42        })
43    }
44}
45
46impl FromStr for SyncMode {
47    type Err = SyncwebError;
48
49    fn from_str(value: &str) -> Result<Self> {
50        match value.to_ascii_lowercase().replace(['-', '_'], "").as_str() {
51            "sendreceive" => Ok(Self::SendReceive),
52            "sendonly" => Ok(Self::SendOnly),
53            "receiveonly" => Ok(Self::ReceiveOnly),
54            "receiveencrypted" => Ok(Self::ReceiveEncrypted),
55            "publicreadonly" => Ok(Self::PublicReadOnly),
56            _ => Err(SyncwebError::InvalidSyncMode(value.to_owned())),
57        }
58    }
59}