cml/types/
right.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_io as fio;
6
7pub use cm_types::{
8    Availability, BorrowedName, BoundedName, DeliveryType, DependencyType, HandleType, Name,
9    OnTerminate, ParseError, Path, RelativePath, StartupMode, StorageId, Url,
10};
11use cml_macro::CheckedVec;
12use serde::{Deserialize, Serialize};
13
14use std::fmt;
15
16/// A right or bundle of rights to apply to a directory.
17#[derive(Deserialize, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
18#[serde(rename_all = "snake_case")]
19pub enum Right {
20    // Individual
21    Connect,
22    Enumerate,
23    Execute,
24    GetAttributes,
25    ModifyDirectory,
26    ReadBytes,
27    Traverse,
28    UpdateAttributes,
29    WriteBytes,
30
31    // Aliass
32    #[serde(rename = "r*")]
33    ReadAlias,
34    #[serde(rename = "w*")]
35    WriteAlias,
36    #[serde(rename = "x*")]
37    ExecuteAlias,
38    #[serde(rename = "rw*")]
39    ReadWriteAlias,
40    #[serde(rename = "rx*")]
41    ReadExecuteAlias,
42}
43
44impl fmt::Display for Right {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        let s = match self {
47            Self::Connect => "connect",
48            Self::Enumerate => "enumerate",
49            Self::Execute => "execute",
50            Self::GetAttributes => "get_attributes",
51            Self::ModifyDirectory => "modify_directory",
52            Self::ReadBytes => "read_bytes",
53            Self::Traverse => "traverse",
54            Self::UpdateAttributes => "update_attributes",
55            Self::WriteBytes => "write_bytes",
56            Self::ReadAlias => "r*",
57            Self::WriteAlias => "w*",
58            Self::ExecuteAlias => "x*",
59            Self::ReadWriteAlias => "rw*",
60            Self::ReadExecuteAlias => "rx*",
61        };
62        write!(f, "{}", s)
63    }
64}
65
66impl Right {
67    /// Expands this right or bundle or rights into a list of `fio::Operations`.
68    pub fn expand(&self) -> Vec<fio::Operations> {
69        match self {
70            Self::Connect => vec![fio::Operations::CONNECT],
71            Self::Enumerate => vec![fio::Operations::ENUMERATE],
72            Self::Execute => vec![fio::Operations::EXECUTE],
73            Self::GetAttributes => vec![fio::Operations::GET_ATTRIBUTES],
74            Self::ModifyDirectory => vec![fio::Operations::MODIFY_DIRECTORY],
75            Self::ReadBytes => vec![fio::Operations::READ_BYTES],
76            Self::Traverse => vec![fio::Operations::TRAVERSE],
77            Self::UpdateAttributes => vec![fio::Operations::UPDATE_ATTRIBUTES],
78            Self::WriteBytes => vec![fio::Operations::WRITE_BYTES],
79            Self::ReadAlias => vec![
80                fio::Operations::CONNECT,
81                fio::Operations::ENUMERATE,
82                fio::Operations::TRAVERSE,
83                fio::Operations::READ_BYTES,
84                fio::Operations::GET_ATTRIBUTES,
85            ],
86            Self::WriteAlias => vec![
87                fio::Operations::CONNECT,
88                fio::Operations::ENUMERATE,
89                fio::Operations::TRAVERSE,
90                fio::Operations::WRITE_BYTES,
91                fio::Operations::MODIFY_DIRECTORY,
92                fio::Operations::UPDATE_ATTRIBUTES,
93            ],
94            Self::ExecuteAlias => vec![
95                fio::Operations::CONNECT,
96                fio::Operations::ENUMERATE,
97                fio::Operations::TRAVERSE,
98                fio::Operations::EXECUTE,
99            ],
100            Self::ReadWriteAlias => vec![
101                fio::Operations::CONNECT,
102                fio::Operations::ENUMERATE,
103                fio::Operations::TRAVERSE,
104                fio::Operations::READ_BYTES,
105                fio::Operations::WRITE_BYTES,
106                fio::Operations::MODIFY_DIRECTORY,
107                fio::Operations::GET_ATTRIBUTES,
108                fio::Operations::UPDATE_ATTRIBUTES,
109            ],
110            Self::ReadExecuteAlias => vec![
111                fio::Operations::CONNECT,
112                fio::Operations::ENUMERATE,
113                fio::Operations::TRAVERSE,
114                fio::Operations::READ_BYTES,
115                fio::Operations::GET_ATTRIBUTES,
116                fio::Operations::EXECUTE,
117            ],
118        }
119    }
120}
121
122/// A list of rights.
123#[derive(CheckedVec, Debug, PartialEq, Clone)]
124#[checked_vec(
125    expected = "a nonempty array of rights, with unique elements",
126    min_length = 1,
127    unique_items = true
128)]
129pub struct Rights(pub Vec<Right>);
130
131pub trait RightsClause {
132    fn rights(&self) -> Option<&Rights>;
133}