Skip to main content

stix_rs/sdos/
campaign.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::common::{CommonProperties, StixObject};
5
6/// Campaign SDO
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub struct Campaign {
10    #[serde(flatten)]
11    pub common: CommonProperties,
12
13    pub name: String,
14    pub description: Option<String>,
15    pub first_seen: Option<DateTime<Utc>>,
16    pub last_seen: Option<DateTime<Utc>>,
17}
18
19impl Campaign {
20    pub fn builder() -> CampaignBuilder {
21        CampaignBuilder::default()
22    }
23}
24
25#[derive(Debug, Default)]
26pub struct CampaignBuilder {
27    name: Option<String>,
28    description: Option<String>,
29    first_seen: Option<DateTime<Utc>>,
30    last_seen: Option<DateTime<Utc>>,
31    created_by_ref: Option<String>,
32}
33
34impl CampaignBuilder {
35    pub fn name(mut self, name: impl Into<String>) -> Self {
36        self.name = Some(name.into());
37        self
38    }
39    pub fn description(mut self, d: impl Into<String>) -> Self {
40        self.description = Some(d.into());
41        self
42    }
43    pub fn first_seen(mut self, t: DateTime<Utc>) -> Self {
44        self.first_seen = Some(t);
45        self
46    }
47    pub fn last_seen(mut self, t: DateTime<Utc>) -> Self {
48        self.last_seen = Some(t);
49        self
50    }
51    pub fn created_by_ref(mut self, r: impl Into<String>) -> Self {
52        self.created_by_ref = Some(r.into());
53        self
54    }
55    pub fn build(self) -> Result<Campaign, super::BuilderError> {
56        let name = self.name.ok_or(super::BuilderError::MissingField("name"))?;
57        let common = CommonProperties::new("campaign", self.created_by_ref);
58        Ok(Campaign {
59            common,
60            name,
61            description: self.description,
62            first_seen: self.first_seen,
63            last_seen: self.last_seen,
64        })
65    }
66}
67
68impl StixObject for Campaign {
69    fn id(&self) -> &str {
70        &self.common.id
71    }
72    fn type_(&self) -> &str {
73        &self.common.r#type
74    }
75    fn created(&self) -> DateTime<Utc> {
76        self.common.created
77    }
78}
79impl From<Campaign> for crate::StixObjectEnum {
80    fn from(c: Campaign) -> Self {
81        crate::StixObjectEnum::Campaign(c)
82    }
83}