-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.rs
104 lines (87 loc) · 2.22 KB
/
domain.rs
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
use std::fmt::{Display, Formatter};
use lazy_static::lazy_static;
use regex::Regex;
use url::Url;
use crate::error::Error;
use crate::error::Error::InvalidArgument;
#[derive(Debug, Clone, derive::Ksuid, Eq, PartialEq)]
#[prefix = "app"]
pub struct ApplicationId {
id: String,
}
#[derive(Debug, Clone)]
pub struct Application {
pub id: ApplicationId,
pub name: String,
}
impl Application {
pub fn new(name: String) -> Self {
Self {
id: ApplicationId::new(),
name,
}
}
}
#[derive(Debug, Clone, derive::Ksuid, Eq, PartialEq)]
#[prefix = "ep"]
pub struct EndpointId {
id: String,
}
#[derive(Clone, Debug)]
pub enum EndpointStatus {
Initial,
DisabledManually,
}
#[derive(Debug, Clone)]
pub struct Endpoint {
pub id: EndpointId,
pub app_id: ApplicationId,
pub url: Url,
pub topics: Vec<Topic>,
pub status: EndpointStatus,
}
impl Endpoint {
pub fn new(url: String, app_id: ApplicationId, topics: Vec<Topic>) -> Self {
Self {
id: EndpointId::new(),
url: Url::parse(url.as_str()).unwrap(),
topics,
app_id,
status: EndpointStatus::Initial,
}
}
pub fn disable_manually(&mut self) {
self.status = EndpointStatus::DisabledManually;
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Topic {
name: String,
}
impl Topic {
pub fn new(name: String) -> Result<Self, Error> {
lazy_static! {
static ref RE: Regex = Regex::new(r"^[a-zA-Z_\.\-]+$").unwrap();
}
if !RE.is_match(name.as_str()) {
return Err(InvalidArgument("Invalid topic name".to_string()));
}
Ok(Self { name })
}
}
impl Display for Topic {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
#[cfg(test)]
mod tests {
use crate::configuration::domain::Topic;
#[test]
fn topic_name_construct() {
assert!(Topic::new("customer_updated".to_string()).is_ok());
assert!(Topic::new("customer-updated".to_string()).is_ok());
assert!(Topic::new("customer.updated".to_string()).is_ok());
assert!(Topic::new("customer.updated2".to_string()).is_err());
}
}