Skip to main content

stix_rs/observables/
socket_addr.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub struct SocketAddr {
6    pub value: Option<String>,
7    #[serde(flatten)]
8    pub custom_properties: std::collections::HashMap<String, serde_json::Value>,
9}
10
11impl SocketAddr {
12    pub fn builder() -> SocketAddrBuilder { SocketAddrBuilder::default() }
13}
14
15#[derive(Debug, Default)]
16pub struct SocketAddrBuilder { value: Option<String>, custom_properties: std::collections::HashMap<String, serde_json::Value> }
17
18impl SocketAddrBuilder {
19    pub fn value(mut self, v: impl Into<String>) -> Self { self.value = Some(v.into()); self }
20    pub fn property(mut self, k: impl Into<String>, val: impl Into<serde_json::Value>) -> Self { self.custom_properties.insert(k.into(), val.into()); self }
21    pub fn build(self) -> SocketAddr { SocketAddr { value: self.value, custom_properties: self.custom_properties } }
22}
23
24impl From<SocketAddr> for crate::StixObjectEnum { fn from(s: SocketAddr) -> Self { crate::StixObjectEnum::SocketAddr(s) } }
25
26#[cfg(test)]
27mod tests {
28    use serde_json::json;
29
30    #[test]
31    fn socket_addr_serde() {
32        let v = json!({"type": "socket-addr", "value": "192.0.2.1:443"});
33        let obj: crate::StixObjectEnum = serde_json::from_value(v).expect("deserialize into StixObjectEnum");
34        match obj {
35            crate::StixObjectEnum::SocketAddr(sa) => assert_eq!(sa.value.unwrap(), "192.0.2.1:443"),
36            _ => panic!("expected SocketAddr variant"),
37        }
38    }
39}