sylvia_iot_broker/models/
network_route.rs

1//! Traits and structs for network routes.
2
3use std::error::Error as StdError;
4
5use async_trait::async_trait;
6use chrono::{DateTime, Utc};
7
8/// The item content.
9#[derive(Debug, PartialEq)]
10pub struct NetworkRoute {
11    pub route_id: String,
12    pub unit_id: String,
13    pub unit_code: String, // Application's unit code.
14    pub application_id: String,
15    pub application_code: String,
16    pub network_id: String,
17    pub network_code: String,
18    pub created_at: DateTime<Utc>,
19}
20
21// The network route cache item for uplink data.
22#[derive(Clone)]
23pub struct NetworkRouteCacheUlData {
24    pub app_mgr_keys: Vec<String>,
25}
26
27/// The sort keys for the list operation.
28pub enum SortKey {
29    CreatedAt,
30    ApplicationCode,
31    NetworkCode,
32}
33
34/// The sort condition for the list operation.
35pub struct SortCond {
36    pub key: SortKey,
37    pub asc: bool,
38}
39
40/// The list operation options.
41pub struct ListOptions<'a> {
42    /// The query conditions.
43    pub cond: &'a ListQueryCond<'a>,
44    /// The data offset.
45    pub offset: Option<u64>,
46    /// The maximum number to query.
47    pub limit: Option<u64>,
48    /// The sort conditions.
49    pub sort: Option<&'a [SortCond]>,
50    /// The maximum number items one time the `list()` returns.
51    ///
52    /// Use cursors until reaching `limit` or all data.
53    pub cursor_max: Option<u64>,
54}
55
56/// The query condition to get item(s).
57#[derive(Default)]
58pub struct QueryCond<'a> {
59    pub route_id: Option<&'a str>,
60    pub unit_id: Option<&'a str>,
61    pub application_id: Option<&'a str>,
62    pub network_id: Option<&'a str>,
63}
64
65/// The query condition for the list operation.
66#[derive(Default)]
67pub struct ListQueryCond<'a> {
68    /// To get the specified network route.
69    pub route_id: Option<&'a str>,
70    /// To get network routes of the specified unit.
71    pub unit_id: Option<&'a str>,
72    /// To get network routes of the specified unit code.
73    pub unit_code: Option<&'a str>,
74    /// To get network routes of the specified application.
75    pub application_id: Option<&'a str>,
76    /// To get network routes of the specified application code.
77    pub application_code: Option<&'a str>,
78    /// To get network routes of the specified network.
79    pub network_id: Option<&'a str>,
80    /// To get network routes of the specified network code.
81    pub network_code: Option<&'a str>,
82}
83
84/// Model operations.
85#[async_trait]
86pub trait NetworkRouteModel: Sync {
87    /// To create and initialize the table/collection.
88    async fn init(&self) -> Result<(), Box<dyn StdError>>;
89
90    /// To get item count for the query condition.
91    ///
92    /// **Note**: this may take a long time.
93    async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;
94
95    /// To get item list. The maximum number of returned items will be controlled by the
96    /// `cursor_max` of the list option.
97    ///
98    /// For the first time, `cursor` MUST use `None`. If one cursor is returned, it means that
99    /// there are more items to get. Use the returned cursor to get more data items.
100    ///
101    /// **Note**: using cursors is recommended to prevent exhausting memory.
102    async fn list(
103        &self,
104        opts: &ListOptions,
105        cursor: Option<Box<dyn Cursor>>,
106    ) -> Result<(Vec<NetworkRoute>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;
107
108    /// To get an item.
109    ///
110    /// **Note**: this is only used for function test.
111    async fn get(&self, route_id: &str) -> Result<Option<NetworkRoute>, Box<dyn StdError>>;
112
113    /// To add an item.
114    async fn add(&self, route: &NetworkRoute) -> Result<(), Box<dyn StdError>>;
115
116    /// To delete one or more items.
117    async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;
118}
119
120/// The operations for cursors.
121///
122/// All functions are private to let programs to pass them as arguments directly without any
123/// operation.
124#[async_trait]
125pub trait Cursor: Send {
126    async fn try_next(&mut self) -> Result<Option<NetworkRoute>, Box<dyn StdError>>;
127
128    fn offset(&self) -> u64;
129}
130
131/// Cache operations.
132#[async_trait]
133pub trait NetworkRouteCache: Sync {
134    /// To clear all network routes.
135    async fn clear(&self) -> Result<(), Box<dyn StdError>>;
136
137    /// To get network route for the uplink data.
138    async fn get_uldata(
139        &self,
140        network_id: &str,
141    ) -> Result<Option<NetworkRouteCacheUlData>, Box<dyn StdError>>;
142
143    /// To set network route for the uplink data.
144    async fn set_uldata(
145        &self,
146        network_id: &str,
147        value: Option<&NetworkRouteCacheUlData>,
148    ) -> Result<(), Box<dyn StdError>>;
149
150    /// To delete network route for the uplink data.
151    async fn del_uldata(&self, network_id: &str) -> Result<(), Box<dyn StdError>>;
152}