Skip to main content

crucible_wire/permissions/
target.rs

1use std::fmt;
2
3/// One bound identifier of a [`ResourceRef`](super::ResourceRef).
4#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
5pub enum Target {
6    One(String),
7    /// Every instance, including ones created later: `Inbox(*)`. Only a
8    /// resource's own (last) identifier is ever `All`.
9    All,
10}
11
12impl Target {
13    /// The identifier, unless it is `All`.
14    pub fn as_one(&self) -> Option<&str> {
15        match self {
16            Self::One(id) => Some(id),
17            Self::All => None,
18        }
19    }
20}
21
22impl From<String> for Target {
23    fn from(id: String) -> Self {
24        Self::One(id)
25    }
26}
27
28impl From<&String> for Target {
29    fn from(id: &String) -> Self {
30        Self::One(id.clone())
31    }
32}
33
34impl From<&str> for Target {
35    fn from(id: &str) -> Self {
36        Self::One(id.to_owned())
37    }
38}
39
40impl fmt::Display for Target {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::One(id) => f.write_str(id),
44            Self::All => f.write_str("*"),
45        }
46    }
47}