nakadi_types/
misc.rs

1//! Types that are shared throughout the model
2use std::fmt;
3use std::str::FromStr;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Error;
8
9new_type! {
10    #[doc=r#"Indicator of the application owning this EventType.
11
12See also [Nakadi Manual](https://nakadi.io/manual.html#definition_EventType*owning_application)"#]
13    #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
14    pub struct OwningApplication(String, env="OWNING_APPLICATION");
15}
16
17#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
18pub struct AuthorizationAttributes(Vec<AuthorizationAttribute>);
19
20impl AuthorizationAttributes {
21    pub fn new<T: Into<Vec<AuthorizationAttribute>>>(v: T) -> Self {
22        Self(v.into())
23    }
24
25    pub fn att<T: Into<AuthorizationAttribute>>(mut self, v: T) -> Self {
26        self.0.push(v.into());
27        self
28    }
29
30    pub fn push<T: Into<AuthorizationAttribute>>(&mut self, v: T) {
31        self.0.push(v.into());
32    }
33
34    pub fn len(&self) -> usize {
35        self.0.len()
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.0.is_empty()
40    }
41
42    pub fn iter(&self) -> impl Iterator<Item = &AuthorizationAttribute> {
43        self.0.iter()
44    }
45}
46
47impl IntoIterator for AuthorizationAttributes {
48    type Item = AuthorizationAttribute;
49    type IntoIter = std::vec::IntoIter<AuthorizationAttribute>;
50
51    fn into_iter(self) -> Self::IntoIter {
52        self.0.into_iter()
53    }
54}
55
56impl<A> From<A> for AuthorizationAttributes
57where
58    A: Into<AuthorizationAttribute>,
59{
60    fn from(k: A) -> Self {
61        Self::new(vec![k.into()])
62    }
63}
64
65impl<A, B, C> From<(A, B, C)> for AuthorizationAttributes
66where
67    A: Into<AuthorizationAttribute>,
68    B: Into<AuthorizationAttribute>,
69    C: Into<AuthorizationAttribute>,
70{
71    fn from((a, b, c): (A, B, C)) -> Self {
72        Self::new(vec![a.into(), b.into(), c.into()])
73    }
74}
75/// An attribute for authorization.
76///
77/// This object includes a data type, which represents the type of the
78/// attribute attribute (which data types are allowed depends on which authorization
79/// plugin is deployed, and how it is configured), and a value.
80/// A wildcard can be represented with data type and value. It means that
81/// all authenticated users are allowed to perform an operation.
82///
83/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_AuthorizationAttribute)
84#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
85pub struct AuthorizationAttribute {
86    /// The type of attribute (e.g., ‘team’, or ‘permission’, depending on the Nakadi configuration)
87    pub data_type: AuthAttDataType,
88    /// The value of the attribute
89    pub value: AuthAttValue,
90}
91
92impl AuthorizationAttribute {
93    pub fn new<D: Into<AuthAttDataType>, V: Into<AuthAttValue>>(data_type: D, value: V) -> Self {
94        Self {
95            data_type: data_type.into(),
96            value: value.into(),
97        }
98    }
99}
100
101impl FromStr for AuthorizationAttribute {
102    type Err = Error;
103    fn from_str(s: &str) -> Result<Self, Self::Err> {
104        let parts: Vec<&str> = s.split(':').map(|s| s.trim()).collect();
105
106        if parts.len() != 2 {
107            return Err(Error::new(format!(
108                "{} is not a valid AuthorizationAttribute",
109                s
110            )));
111        }
112
113        Ok(Self::new(parts[0], parts[1]))
114    }
115}
116
117impl<U, V> From<(U, V)> for AuthorizationAttribute
118where
119    U: Into<AuthAttDataType>,
120    V: Into<AuthAttValue>,
121{
122    fn from((u, v): (U, V)) -> Self {
123        AuthorizationAttribute::new(u, v)
124    }
125}
126
127impl fmt::Display for AuthorizationAttribute {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        write!(f, "{}:{}", self.data_type, self.value)?;
130        Ok(())
131    }
132}
133
134new_type! {
135    #[doc=r#"Data type of `AuthorizationAttribute`.
136
137See also [Nakadi Manual](https://nakadi.io/manual.html#definition_AuthorizationAttribute)"#]
138    #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
139    pub struct AuthAttDataType(String);
140}
141
142impl AuthAttDataType {
143    /// Creates an `AuthorizationAttribute` with the this data type and the given value
144    pub fn with_value<V: Into<AuthAttValue>>(self, value: V) -> AuthorizationAttribute {
145        AuthorizationAttribute::new(self.0, value)
146    }
147}
148
149new_type! {
150    #[doc=r#"Value type of `AuthorizationAttribute`.
151
152See also [Nakadi Manual](https://nakadi.io/manual.html#definition_AuthorizationAttribute)"#]
153    #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
154    pub struct AuthAttValue(String);
155}