pub mod deserialize;
#[cfg(test)]
mod tests;
pub mod validation;
pub use deserialize::deserialize_bundle_objects;
use crate::{
base::Stix,
error::StixError as Error,
object::StixObject,
types::{Identified, Identifier},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Bundle {
#[serde(rename = "type")]
pub object_type: String,
pub id: Identifier,
#[serde(deserialize_with = "deserialize_bundle_objects")]
pub objects: Vec<StixObject>,
}
impl Bundle {
pub fn new(object: StixObject) -> Self {
Self {
object_type: "bundle".to_string(),
id: Identifier::new("bundle").unwrap(),
objects: vec![object],
}
}
pub fn add(&mut self, object: StixObject) {
self.objects.push(object);
}
pub fn push_json(&mut self, json: &str) -> Result<(), Error> {
let object = StixObject::from_json(json, false)?;
self.objects.push(object);
Ok(())
}
pub fn from_json(json: &str) -> Result<Self, Error> {
let bundle: Self =
serde_json::from_str(json).map_err(|e| Error::DeserializationError(e.to_string()))?;
bundle.stix_check()?;
Ok(bundle)
}
pub fn get_objects(&self) -> Vec<StixObject> {
self.objects.clone()
}
}
impl Identified for Bundle {
fn get_id(&self) -> &Identifier {
&self.id
}
}