use std::collections::HashMap;
use std::sync::LazyLock;
use crate::base::CommonProperties;
use crate::cyber_observable_objects::sco::CYBER_OBJECT_TYPE_VARIANTS;
use crate::domain_objects::sdo::DOMAIN_OBJECT_TYPE_VARIANTS;
use crate::meta_objects::extension_definition::ExtensionDefinition;
use crate::meta_objects::language_content::LanguageContent;
use crate::meta_objects::marking_definition::MarkingDefinition;
use crate::relationship_objects::RELATIONSHIP_OBJECT_TYPE_VARIANTS;
use crate::types::get_object_type;
pub trait StixProperties {
const KNOWN: &'static [&'static str];
const REQUIRED: &'static [&'static str];
}
pub struct VariantProps {
pub tag: &'static str,
pub known: &'static [&'static str],
pub required: &'static [&'static str],
}
pub struct TypeProperties {
pub known: Vec<&'static str>,
pub required: Vec<&'static str>,
}
const TYPE_KEY: &str = "type";
const SCO_COMMON: &[&str] = &[
"spec_version",
"id",
"defanged",
"extensions",
"object_marking_refs",
"granular_markings",
];
fn common_except_defanged() -> Vec<&'static str> {
CommonProperties::KNOWN
.iter()
.copied()
.filter(|k| *k != "defanged")
.collect()
}
fn compose(variant: &VariantProps, common: &[&'static str]) -> TypeProperties {
let mut known: Vec<&'static str> = Vec::with_capacity(variant.known.len() + common.len() + 1);
known.extend_from_slice(variant.known);
known.extend_from_slice(common);
known.push(TYPE_KEY);
let mut required: Vec<&'static str> = Vec::with_capacity(variant.required.len() + 1);
required.extend_from_slice(variant.required);
required.extend_from_slice(CommonProperties::REQUIRED);
TypeProperties { known, required }
}
fn compose_meta(
tag: &'static str,
known: &'static [&'static str],
required: &'static [&'static str],
common: &[&'static str],
) -> TypeProperties {
compose(
&VariantProps {
tag,
known,
required,
},
common,
)
}
static REGISTRY: LazyLock<HashMap<&'static str, TypeProperties>> = LazyLock::new(|| {
let mut m: HashMap<&'static str, TypeProperties> = HashMap::new();
let sdo_common = common_except_defanged();
for variant in DOMAIN_OBJECT_TYPE_VARIANTS {
m.insert(variant.tag, compose(variant, &sdo_common));
}
for variant in CYBER_OBJECT_TYPE_VARIANTS {
m.insert(variant.tag, compose(variant, SCO_COMMON));
}
let mut sro_common = common_except_defanged();
sro_common.push("description");
for variant in RELATIONSHIP_OBJECT_TYPE_VARIANTS {
m.insert(variant.tag, compose(variant, &sro_common));
}
let meta_common = common_except_defanged();
m.insert(
"marking-definition",
compose_meta(
"marking-definition",
MarkingDefinition::KNOWN,
MarkingDefinition::REQUIRED,
&meta_common,
),
);
m.insert(
"language-content",
compose_meta(
"language-content",
LanguageContent::KNOWN,
LanguageContent::REQUIRED,
&meta_common,
),
);
m.insert(
"extension-definition",
compose_meta(
"extension-definition",
ExtensionDefinition::KNOWN,
ExtensionDefinition::REQUIRED,
&meta_common,
),
);
m
});
pub fn type_properties(type_name: &str) -> Option<&'static TypeProperties> {
REGISTRY.get(type_name)
}
#[allow(dead_code)]
pub fn type_properties_by_category(type_name: &str) -> Option<&'static TypeProperties> {
match get_object_type(type_name) {
cat if cat == "custom" || cat == "bundle" => None,
_ => type_properties(type_name),
}
}