dyer/component/
info.rs

1//! A structure that carries basic meta-data,
2//! including its origin, stime stamp, privilege, encoding and so on.
3//!
4use crate::{plugin::deser::*, utils};
5use http::Uri;
6use serde::{Deserialize, Serialize};
7
8/// basic meta data related 3 ranging
9/// - identifier
10///   `id`, `marker`, `from`
11/// - time stamp
12///    `gap`, `created`, `able`
13/// - privilege
14///    `rank`, `unique`, `used`
15///
16/// Some infomation must be specified, such as `marker`, `id`, and so on
17///
18/// Some can be set from other [Info], like `from`, `used`
19///
20/// and others are initialized as default, such as `created`, `able`, `encoding`,
21#[derive(Deserialize, Debug, Serialize)]
22pub struct Info {
23    /// the actor it belongs to
24    pub marker: String,
25    /// identifier of the entity
26    pub id: u64,
27    /// uri that produces this `entity`
28    #[serde(with = "serde_uri")]
29    pub from: Uri,
30    /// the priority of this entity, the higher the eariler get executed, 0 as default
31    pub rank: i16,
32    /// time duration to execute the task
33    /// it remains 0 until it moves to response,
34    pub gap: f64,
35    /// the encoding when encoding uri
36    // TODO: String is not enough make it enum
37    pub encoding: String,
38    /// remove duplicate `entity`, `true` as default
39    pub unique: bool,
40    /// numbers that this `entity` has used, by default,
41    /// commonly the threshold is 2 for `Task` beyond which will ignore,
42    /// customize it in `ArgApp`
43    /// no restriction for `Affix`
44    pub used: u32,
45    /// meta data that the `entity` is created
46    pub created: f64,
47    /// timestamp in seconds by which `entity` is allowed to be executed
48    /// it is, as default, allowed when created
49    pub able: f64,
50}
51
52impl Clone for Info {
53    fn clone(&self) -> Self {
54        Self {
55            marker: self.marker.clone(),
56            id: self.id,
57            from: self.from.clone(),
58            rank: self.rank,
59            gap: self.gap,
60            encoding: self.encoding.clone(),
61            unique: self.unique,
62            used: self.used,
63            created: utils::now(),
64            able: self.able,
65        }
66    }
67}
68
69impl Default for Info {
70    fn default() -> Self {
71        let now = utils::now();
72        Self {
73            marker: "".into(),
74            id: 0,
75            from: Uri::default(),
76            rank: 0,
77            gap: 0.0,
78            encoding: "utf-8".into(),
79            unique: false,
80            used: 0,
81            created: now,
82            able: now,
83        }
84    }
85}
86
87#[test]
88fn test_info() {
89    let info = Info::default();
90    assert_eq!(info.unique, true);
91    assert_eq!(info.used, 0);
92    assert_eq!(info.rank, 0);
93    assert_eq!(info.encoding, "utf-8".to_string());
94}