#![allow(dead_code)]
use crate::error::CheckContext;
pub mod builder;
pub mod relationship;
pub mod sighting;
pub mod validation;
pub use builder::RelationshipObjectBuilder;
pub use relationship::Relationship;
pub use sighting::Sighting;
pub use validation::check_sro_properties;
pub mod types;
use crate::{
base::{CommonProperties, Stix},
error::{add_error, return_multiple_errors, StixError as Error},
types::{Identified, Identifier},
validation::validate_value,
};
use stixflayer_derive::StixProperties;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use strum::{AsRefStr, Display as StrumDisplay};
pub trait Related {
fn add_relationship<T: Related + Identified>(
self,
target: T,
relationship_type: String,
) -> Result<RelationshipObjectBuilder, Error>;
}
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct RelationshipObject {
#[serde(flatten)]
pub object_type: RelationshipObjectType,
#[serde(flatten)]
pub common_properties: CommonProperties,
pub description: Option<String>,
}
impl RelationshipObject {
pub fn from_json(json: &str, allow_custom: bool) -> Result<Self, Error> {
let value: serde_json::Value =
serde_json::from_str(json).map_err(|e| Error::DeserializationError(e.to_string()))?;
validate_value(value, allow_custom, true)
}
pub fn is_revoked(&self) -> bool {
matches!(self.common_properties.revoked, Some(true))
}
pub fn get_relationship_type(&self) -> &str {
match &self.object_type {
RelationshipObjectType::Relationship(relationship) => {
relationship.relationship_type.as_ref()
}
RelationshipObjectType::Sighting(_) => "sighting",
}
}
}
impl Identified for RelationshipObject {
fn get_id(&self) -> &Identifier {
&self.common_properties.id
}
}
impl Stix for RelationshipObject {
fn check(&self, ctx: &mut CheckContext) -> Result<(), Error> {
ctx.set_object_id(Some(self.get_id().to_string()));
let mut errors = Vec::new();
add_error(&mut errors, check_sro_properties(&self.common_properties));
add_error(&mut errors, self.common_properties.check(ctx));
add_error(&mut errors, self.common_properties.id.check(ctx));
match &self.object_type {
RelationshipObjectType::Relationship(relationship) => add_error(
&mut errors,
relationship
.check(ctx)
.map_err(|e| Error::InvalidNestedObject {
context: format!(
"Relationship Object {} is not a valid generic relationship",
self.get_id()
),
source: Box::new(e),
}),
),
RelationshipObjectType::Sighting(sighting) => add_error(
&mut errors,
sighting.check(ctx).map_err(|e| Error::InvalidNestedObject {
context: format!(
"Relationship Object {} is not a valid sighting",
self.get_id()
),
source: Box::new(e),
}),
),
}
return_multiple_errors(errors)
}
}
crate::impl_custom_properties_holder!(RelationshipObject);
#[derive(
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AsRefStr, StrumDisplay, StixProperties,
)]
#[serde(tag = "type", rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum RelationshipObjectType {
Relationship(Relationship),
Sighting(Sighting),
}