tokio_listener/
unix_chmod.rs

1use std::{fmt::Display, str::FromStr};
2
3#[cfg_attr(docsrs_alt, doc(cfg(feature = "unix_path_tools")))]
4#[cfg(feature = "unix_path_tools")]
5/// Value of `--unix-listen-chmod` option which allows changing DAC file access mode for UNIX path socket
6#[non_exhaustive]
7#[cfg_attr(
8    feature = "serde",
9    derive(serde_with::DeserializeFromStr, serde_with::SerializeDisplay)
10)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum UnixChmodVariant {
13    /// Set filesystem mode of the UNIX socket to `u+rw`, allowing access only to one uid
14    Owner,
15    /// Set filesystem mode of the UNIX socket to `ug+rw`, allowing access to owner uid and a group
16    Group,
17    /// Set filesystem mode of the UNIX socket to `a+rw`, allowing global access to the socket
18    Everybody,
19}
20
21#[cfg(feature = "unix_path_tools")]
22#[cfg_attr(docsrs_alt, doc(cfg(feature = "unix_path_tools")))]
23impl Display for UnixChmodVariant {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            UnixChmodVariant::Owner => "owner".fmt(f),
27            UnixChmodVariant::Group => "group".fmt(f),
28            UnixChmodVariant::Everybody => "everybody".fmt(f),
29        }
30    }
31}
32
33#[cfg(feature = "unix_path_tools")]
34#[cfg_attr(docsrs_alt, doc(cfg(feature = "unix_path_tools")))]
35impl FromStr for UnixChmodVariant {
36    type Err = &'static str;
37
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        if s.eq_ignore_ascii_case("owner") {
40            Ok(UnixChmodVariant::Owner)
41        } else if s.eq_ignore_ascii_case("group") {
42            Ok(UnixChmodVariant::Group)
43        } else if s.eq_ignore_ascii_case("everybody") {
44            Ok(UnixChmodVariant::Everybody)
45        } else {
46            Err("Unknown chmod variant. Expected `owner`, `group` or `everybody`.")
47        }
48    }
49}