Skip to main content

stix_rs/observables/
url.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub struct Url {
6    pub value: String,
7    pub url_scheme: Option<String>,
8    pub host: Option<String>,
9    pub port: Option<u16>,
10    pub path: Option<String>,
11    #[serde(flatten)]
12    pub custom_properties: std::collections::HashMap<String, serde_json::Value>,
13}
14
15impl Url {
16    pub fn builder() -> UrlBuilder {
17        UrlBuilder::default()
18    }
19}
20
21#[derive(Debug, Default)]
22pub struct UrlBuilder {
23    value: Option<String>,
24    url_scheme: Option<String>,
25    host: Option<String>,
26    port: Option<u16>,
27    path: Option<String>,
28    custom_properties: std::collections::HashMap<String, serde_json::Value>,
29}
30
31impl UrlBuilder {
32    pub fn value(mut self, v: impl Into<String>) -> Self {
33        self.value = Some(v.into());
34        self
35    }
36    pub fn scheme(mut self, s: impl Into<String>) -> Self {
37        self.url_scheme = Some(s.into());
38        self
39    }
40    pub fn host(mut self, h: impl Into<String>) -> Self {
41        self.host = Some(h.into());
42        self
43    }
44    pub fn port(mut self, p: u16) -> Self {
45        self.port = Some(p);
46        self
47    }
48    pub fn path(mut self, p: impl Into<String>) -> Self {
49        self.path = Some(p.into());
50        self
51    }
52    pub fn property(mut self, k: impl Into<String>, v: impl Into<serde_json::Value>) -> Self {
53        self.custom_properties.insert(k.into(), v.into());
54        self
55    }
56    pub fn build(self) -> Url {
57        Url {
58            value: self.value.unwrap_or_default(),
59            url_scheme: self.url_scheme,
60            host: self.host,
61            port: self.port,
62            path: self.path,
63            custom_properties: self.custom_properties,
64        }
65    }
66}
67
68impl From<Url> for crate::StixObjectEnum {
69    fn from(u: Url) -> Self {
70        crate::StixObjectEnum::Url(u)
71    }
72}