vtx_sdk/
vtx_capabilities.rs

1//! Capability declaration builders.
2
3use crate::{Capabilities, HttpAllowRule};
4
5/// Builder for `Capabilities`.
6pub struct CapabilitiesBuilder {
7    subscriptions: Vec<String>,
8    permissions: Vec<String>,
9    http: Vec<HttpAllowRule>,
10}
11
12impl CapabilitiesBuilder {
13    pub fn new() -> Self {
14        Self {
15            subscriptions: Vec::new(),
16            permissions: Vec::new(),
17            http: Vec::new(),
18        }
19    }
20
21    pub fn subscription(mut self, topic: impl Into<String>) -> Self {
22        self.subscriptions.push(topic.into());
23        self
24    }
25
26    pub fn subscriptions<I, S>(mut self, topics: I) -> Self
27    where
28        I: IntoIterator<Item = S>,
29        S: Into<String>,
30    {
31        self.subscriptions.extend(topics.into_iter().map(Into::into));
32        self
33    }
34
35    pub fn permission(mut self, perm: impl Into<String>) -> Self {
36        self.permissions.push(perm.into());
37        self
38    }
39
40    pub fn permissions<I, S>(mut self, perms: I) -> Self
41    where
42        I: IntoIterator<Item = S>,
43        S: Into<String>,
44    {
45        self.permissions.extend(perms.into_iter().map(Into::into));
46        self
47    }
48
49    pub fn http_rule(mut self, rule: HttpAllowRule) -> Self {
50        self.http.push(rule);
51        self
52    }
53
54    pub fn http_rules<I>(mut self, rules: I) -> Self
55    where
56        I: IntoIterator<Item = HttpAllowRule>,
57    {
58        self.http.extend(rules);
59        self
60    }
61
62    pub fn build(self) -> Capabilities {
63        let http = if self.http.is_empty() {
64            None
65        } else {
66            Some(self.http)
67        };
68        Capabilities {
69            subscriptions: self.subscriptions,
70            permissions: self.permissions,
71            http,
72        }
73    }
74}
75
76impl Default for CapabilitiesBuilder {
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82/// Builder for `HttpAllowRule`.
83pub struct HttpAllowRuleBuilder {
84    scheme: String,
85    host: String,
86    port: Option<u16>,
87    path: Option<String>,
88    methods: Option<Vec<String>>,
89    allow_headers: Option<Vec<String>>,
90    max_request_bytes: Option<u64>,
91    max_response_bytes: Option<u64>,
92    follow_redirects: Option<bool>,
93    redirect_policy: Option<String>,
94}
95
96impl HttpAllowRuleBuilder {
97    pub fn new(scheme: impl Into<String>, host: impl Into<String>) -> Self {
98        Self {
99            scheme: scheme.into(),
100            host: host.into(),
101            port: None,
102            path: None,
103            methods: None,
104            allow_headers: None,
105            max_request_bytes: None,
106            max_response_bytes: None,
107            follow_redirects: None,
108            redirect_policy: None,
109        }
110    }
111
112    pub fn port(mut self, port: u16) -> Self {
113        self.port = Some(port);
114        self
115    }
116
117    pub fn path(mut self, path: impl Into<String>) -> Self {
118        self.path = Some(path.into());
119        self
120    }
121
122    pub fn methods<I, S>(mut self, methods: I) -> Self
123    where
124        I: IntoIterator<Item = S>,
125        S: Into<String>,
126    {
127        self.methods = Some(methods.into_iter().map(Into::into).collect());
128        self
129    }
130
131    pub fn allow_headers<I, S>(mut self, headers: I) -> Self
132    where
133        I: IntoIterator<Item = S>,
134        S: Into<String>,
135    {
136        self.allow_headers = Some(headers.into_iter().map(Into::into).collect());
137        self
138    }
139
140    pub fn max_request_bytes(mut self, bytes: u64) -> Self {
141        self.max_request_bytes = Some(bytes);
142        self
143    }
144
145    pub fn max_response_bytes(mut self, bytes: u64) -> Self {
146        self.max_response_bytes = Some(bytes);
147        self
148    }
149
150    pub fn follow_redirects(mut self, enabled: bool) -> Self {
151        self.follow_redirects = Some(enabled);
152        self
153    }
154
155    pub fn redirect_policy(mut self, policy: impl Into<String>) -> Self {
156        self.redirect_policy = Some(policy.into());
157        self
158    }
159
160    pub fn build(self) -> HttpAllowRule {
161        HttpAllowRule {
162            scheme: self.scheme,
163            host: self.host,
164            port: self.port,
165            path: self.path,
166            methods: self.methods,
167            allow_headers: self.allow_headers,
168            max_request_bytes: self.max_request_bytes,
169            max_response_bytes: self.max_response_bytes,
170            follow_redirects: self.follow_redirects,
171            redirect_policy: self.redirect_policy,
172        }
173    }
174}