Skip to main content

stix_rs/observables/
file.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// File observable (STIX 2.1)
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub struct File {
8    pub hashes: Option<HashMap<String, String>>,
9    pub name: Option<String>,
10    pub size: Option<u64>,
11    pub mime_type: Option<String>,
12    pub parent_directory_ref: Option<String>,
13    pub content_ref: Option<String>,
14    #[serde(flatten)]
15    pub custom_properties: HashMap<String, serde_json::Value>,
16}
17
18impl File {
19    /// Builder convenience
20    pub fn builder() -> FileBuilder { FileBuilder::default() }
21}
22
23#[derive(Debug, Default)]
24pub struct FileBuilder {
25    hashes: Option<HashMap<String, String>>,
26    name: Option<String>,
27    size: Option<u64>,
28    mime_type: Option<String>,
29    parent_directory_ref: Option<String>,
30    content_ref: Option<String>,
31    custom_properties: HashMap<String, serde_json::Value>,
32}
33
34impl FileBuilder {
35    pub fn hashes(mut self, hashes: HashMap<String, String>) -> Self { self.hashes = Some(hashes); self }
36    pub fn name(mut self, name: impl Into<String>) -> Self { self.name = Some(name.into()); self }
37    pub fn size(mut self, size: u64) -> Self { self.size = Some(size); self }
38    pub fn mime_type(mut self, mime: impl Into<String>) -> Self { self.mime_type = Some(mime.into()); self }
39    pub fn parent_directory_ref(mut self, dir: impl Into<String>) -> Self { self.parent_directory_ref = Some(dir.into()); self }
40    pub fn content_ref(mut self, content: impl Into<String>) -> Self { self.content_ref = Some(content.into()); self }
41    pub fn property(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self { self.custom_properties.insert(key.into(), value.into()); self }
42    pub fn build(self) -> File { File { hashes: self.hashes, name: self.name, size: self.size, mime_type: self.mime_type, parent_directory_ref: self.parent_directory_ref, content_ref: self.content_ref, custom_properties: self.custom_properties } }
43}
44
45impl From<File> for crate::StixObjectEnum { fn from(f: File) -> Self { crate::StixObjectEnum::File(f) } }