uri-resources 0.2.5

Resource URI building library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! # URI Routes Resources.
//! A sidecar library for detailing the specifics of how a URI should
//! be constructed.
//! Allows for a rudimentary check of path arguments, when/if they are
//! required to build the resulting URI.
use std::{borrow::BorrowMut, fmt::{Debug, Display}};

use anyhow::Result;

#[derive(Clone, Copy, Debug)]
pub enum ArgRequiredBy {
    Child,
    Me,
    NoOne,
    Parent,
}

impl ArgRequiredBy {
    pub fn is_child(self) -> bool {
        matches!(self, Self::Child)
    }

    pub fn is_me(self) -> bool {
        matches!(self, Self::Me)
    }

    pub fn is_noone(self) -> bool {
        matches!(self, Self::NoOne)
    }

    pub fn is_parent(self) -> bool {
        matches!(self, Self::Parent)
    }
}

#[derive(thiserror::Error, Clone, Debug)]
pub enum ArgError {
    #[error("{0} requires an argument")]
    Missing(String),
    #[error("{0} invalid with reason(s): {1:?}")]
    NotValid(String, Vec<String>)
}

#[derive(thiserror::Error, Clone, Debug)]
pub enum ResourceError {
    #[error("existing {1} node of {0} already set")]
    AlreadySet(String, String),
}

/// Represents a single part of of a URI path.
/// Where arguments are optional, there are
/// interfaces which allow this object to check
/// if an argument is required by either this
/// component, or entities that are related to it.
#[derive(Debug)]
pub struct ApiResource<'a, T: Display> {
    name:            &'a str,
    arg:             Option<T>,
    arg_required_by: ArgRequiredBy,
    arg_validators:  Vec<fn(&T) -> Result<()>>,
    child:           Option<Box<Self>>,
    parent:          Option<Box<Self>>,
    weight:          f32,
}

/// Barebones basic implementation of an
/// `ApiResource`.
/// ```rust
/// use uri_resources::ApiResource;
/// let resource: ApiResource<'_, String> = ApiResource::new("resource");
/// ```
impl<'a, T: Display> ApiResource<'a, T> {
    /// Create a new instance of `ApiResource`.
    pub fn new<'b: 'a>(name: &'b str) -> Self {
        Self{
            name,
            arg: None,
            arg_required_by: ArgRequiredBy::NoOne,
            arg_validators: vec![],
            child: None,
            parent: None,
            weight: 0.0
        }
    }
}

impl<T: Clone + Display> Clone for ApiResource<'_, T> {
    fn clone(&self) -> Self {
        Self{
            name: self.name,
            arg:  self.arg.clone(),
            arg_required_by: self.arg_required_by,
            arg_validators: self.arg_validators.clone(),
            child: self.child.clone(),
            parent: self.parent.clone(),
            weight: self.weight
        }
    }
}

/// Composes an object into a path component,
/// conditionally failing if the implemented
/// instance does not meet the requirements set
/// by it's declaration.
///
/// Ensure resources can be digested as path
/// components.
/// ```rust
/// use uri_resources::{ApiResource, PathComponent};
/// let path = ApiResource::<String>::new("resource").as_path_component();
/// assert!(!path.is_err())
/// ```
pub trait PathComponent {
    /// Composes this as a path component.
    ///
    /// Ensure resources can be digested and return
    /// the expected value.
    /// ```rust
    /// use uri_resources::{ApiResource, PathComponent};
    /// let path = ApiResource::<String>::new("resource").as_path_component();
    /// assert_eq!(path.unwrap(), String::from("resource/"))
    /// ```
    fn as_path_component(&self) -> Result<String>;
    /// Compose the entire heirarchy of components
    /// into one string.
    ///
    /// Ensure the composition of a multi node
    /// collection can be composed into a single
    /// String value without error.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource, PathComponent};
    /// let mut child0 = ApiResource::<String>::new("child_resource0");
    /// let mut child1 = ApiResource::<String>::new("child_resource1");
    ///
    /// child0 = *child0.with_child(&mut child1).expect("resource node");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child0);
    ///
    /// let path = parent.expect("parent node").compose();
    /// assert!(!path.is_err())
    /// ```
    ///
    /// Ensure the composition of a multi node
    /// collection can be composed into a single
    /// String value without error.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource, PathComponent};
    /// let mut child0 = ApiResource::<String>::new("child_resource0");
    /// let mut child1 = ApiResource::<String>::new("child_resource1");
    ///
    /// child0 = *child0.with_child(&mut child1).expect("resource node");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child0);
    ///
    /// let path = parent.expect("parent node").compose();
    /// assert_eq!(path.expect("composed path"), "parent_resource/child_resource0/child_resource1/")
    /// ```
    fn compose(&self) -> Result<String>;
}

impl<'a, T: Debug + Display + Clone> PathComponent for ApiResource<'a, T> {
    fn as_path_component(&self) -> Result<String> {
        let to_argnotfound = |n: &Self| {
            Err(ArgError::Missing(n.name().to_owned()).into())
        };

        let compose_this = || {
            let errors: Vec<_> = self.arg_validators
                .iter()
                .map(|f| { (f)(self.arg.as_ref().unwrap()) })
                .filter(|r| r.is_err())
                .map(|r| r.unwrap_err().to_string())
                .collect();

            if !errors.is_empty()  {
                Err(ArgError::NotValid(self.name(), errors).into())
            } else {
                let ret = format!(
                    "{}/{}",
                    self.name(),
                    self.arg.clone().map_or("".into(), |a| a.to_string()));
                Ok(ret)
            }
        };

        if self.arg.is_some() || self.required_by().is_noone() {
            compose_this()
        } else if self.required_by().is_parent() && self.parent.is_some() {
            to_argnotfound(self.parent().unwrap())
        } else if self.required_by().is_child() && self.child.is_some() {
            to_argnotfound(self.child().unwrap())
        } else {
            compose_this()
        }
    }

    fn compose(&self) -> Result<String> {
        let mut curr = Some(self);
        let mut components = vec![];

        while curr.is_some() {
            components.push(match curr.unwrap().as_path_component() {
                Ok(path) => {
                    curr = curr.unwrap().child();
                    path
                },
                e => return e
            });
        }
        Ok(components.join("/").replace("//", "/"))
    }
}

pub trait ArgedResource<T> {
    /// Argument set on this resource.
    fn argument(&self) -> Option<&T>;
    /// Determines if, and by whom, an argument
    /// set on this is required.
    fn required_by(&self) -> ArgRequiredBy;
    /// Sets an argument on this resource
    /// component.
    fn with_arg(&mut self, arg: T) -> &mut Self;
    /// Sets if, and by whom, this component's
    /// argument is required.
    fn with_arg_required(&mut self, required: ArgRequiredBy) -> &mut Self;
}

impl<'a, T: Clone + Display> ArgedResource<T> for ApiResource<'a, T> {
    fn argument(&self) -> Option<&T> {
        self.arg.as_ref()
    }

    fn required_by(&self) -> ArgRequiredBy {
        self.arg_required_by
    }

    fn with_arg(&mut self, arg: T) -> &mut Self {
        self.arg = Some(arg);
        self
    }

    fn with_arg_required(&mut self, required: ArgRequiredBy) -> &mut Self {
        self.arg_required_by = required;
        self
    }
}

/// The core functionality that is to be expected
/// of some resource object. These methods assist
/// in the work done by other traits in this
/// library. Specifically by managing the the
/// resource and it's relatives.
pub trait CoreResource<T> {
    /// The name of the resource component. Is
    /// used as the path component on digestion.
    fn name(&self) -> String;
}

impl<'a, T: Clone + Display> CoreResource<T> for ApiResource<'a, T> {
    fn name(&self) -> String {
        self.name.to_owned()
    }
}

/// Allows resources to set their child and parent
/// nodes.
pub trait LinkedResource<'a, T: Display> {
    /// The child `Resource` node.
    fn child(&self) -> Option<&Self>;
    /// The parent `Resource` node.
    fn parent(&self) -> Option<&Self>;
    /// If this is a child of another resource.
    ///
    /// Initialy created object should produce a
    /// non-child node.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let resource = ApiResource::<String>::new("resource");
    /// assert_eq!(resource.is_child(), false)
    /// ```
    ///
    /// Try to create an instance of two nodes
    /// where one is related to the other as the
    /// parent.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let mut child = ApiResource::<String>::new("child_resource");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child);
    /// assert_eq!(child.is_child(), true)
    /// ```
    fn is_child(&self) -> bool;
    /// If this is the first resource of the path.
    ///
    /// Initialy created object should produce a
    /// root node.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let resource = ApiResource::<String>::new("resource");
    /// assert_eq!(resource.is_root(), true)
    /// ```
    ///
    /// Subsequent objects should not be a root
    /// node.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let mut child = ApiResource::<String>::new("child_resource");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child);
    /// assert_ne!(child.is_root(), true)
    /// ```
    fn is_root(&self) -> bool;
    /// If this is the last resource of the path.
    ///
    /// Root node can be a tail node if it is the
    /// only resource node.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let resource = ApiResource::<String>::new("resource");
    /// assert!(resource.is_tail())
    /// ```
    ///
    /// If there are otherwise child nodes, a root
    /// node cannot be the 'tail'.
    /// ```
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let mut child0 = ApiResource::<String>::new("child_resource0");
    /// let mut child1 = ApiResource::<String>::new("child_resource1");
    ///
    /// child0 = *child0.with_child(&mut child1).expect("resource node");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child0);
    /// assert!(!parent.expect("parent node").is_tail())
    /// ```
    ///
    /// The middle child cannot be the tail.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let mut child0 = ApiResource::<String>::new("child_resource0");
    /// let mut child1 = ApiResource::<String>::new("child_resource1");
    ///
    /// child0 = *child0.with_child(&mut child1).expect("resource node");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child0);
    /// assert!(child0.is_child() && !child0.is_tail());
    /// ```
    ///
    /// The last child should be the tail.
    /// ```rust
    /// use uri_resources::{ApiResource, LinkedResource};
    /// let mut child0 = ApiResource::<String>::new("child_resource0");
    /// let mut child1 = ApiResource::<String>::new("child_resource1");
    ///
    /// child0 = *child0.with_child(&mut child1).expect("resource node");
    /// let parent = ApiResource::<String>::new("parent_resource")
    ///     .with_child(&mut child0);
    /// assert!(child1.is_child() && child1.is_tail())
    /// ```
    fn is_tail(&self) -> bool;
    /// Adds a child node to this resource. Fails
    /// if the child is already set.
    fn with_child(&mut self, child: &mut ApiResource<'a, T>) -> Result<Box<Self>>;
    /// Adds the parent node to this resource.
    /// Fails if the parent is already set.
    fn with_parent(&mut self, parent: &mut ApiResource<'a, T>) -> Result<Box<Self>>;
}

impl<'a, T: Debug + Display + Clone> LinkedResource<'a, T> for ApiResource<'a, T> {
    fn child(&self) -> Option<&Self> {
        self.child.as_deref()
    }

    fn parent(&self) -> Option<&Self> {
        self.parent.as_deref()
    }

    fn is_child(&self) -> bool {
        self.parent.is_some()
    }

    fn is_root(&self) -> bool {
        self.parent.is_none()
    }

    fn is_tail(&self) -> bool {
        self.child.is_none()
    }

    fn with_child(&mut self, child: &mut ApiResource<'a, T>) -> Result<Box<Self>> {
        match self.child {
            None => {
                let mut new = self.clone();
                match child.with_parent(new.borrow_mut()) {
                    Ok(chld) => {
                        new.child = Some(Box::new(chld.as_ref().clone()));
                        Ok(Box::new(new))
                    },
                    Err(e) => Err(e)
                }
            },
            Some(_) => Err(ResourceError::AlreadySet(self.name(), "child".into()).into())
        }
    }

    fn with_parent(&mut self, parent: &mut ApiResource<'a, T>) -> Result<Box<Self>> {
        match self.parent {
            None => {
                self.parent = Box::new(parent.clone()).into();
                Ok(Box::new(self.clone()))
            },
            Some(_) => Err(ResourceError::AlreadySet(self.name(), "parent".into()).into())
        }
    }
}

/// Resource can be 'weighted'. This allows use
/// in `uri_routes`, after digestion to sort
/// paths in the final required. order.
pub trait WeightedResource {
    /// The sorting weight value of this.
    fn weight(&self) -> f32;
    /// Determines the ordering weight to be used
    /// by pre-digestion sorting.
    fn with_weight(&mut self, weight: f32) -> &Self;
}

impl<T: Display> WeightedResource for ApiResource<'_, T> {
    fn weight(&self) -> f32 {
        self.weight
    }

    fn with_weight(&mut self, weight: f32) -> &Self {
        self.weight = weight;
        self
    }
}

pub trait Resource<'a, T: Clone + Display>:
    CoreResource<T> +
    ArgedResource<T> +
    LinkedResource<'a, T> +
    WeightedResource {}

impl<'a, T: Clone + Debug + Display> Resource<'a, T> for ApiResource<'a, T> {}