#![allow(dead_code)]
pub mod filesystem;
pub mod filters;
use crate::{error::StixError as Error, object::StixObject, types::Identifier};
use filesystem::{FilesystemSink, FilesystemSource};
use filters::Filter;
use std::{path::PathBuf, str::FromStr};
use strum::Display;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataStore {
id: Uuid,
source: Option<DataSource>,
sink: Option<DataSink>,
}
impl DataStore {
pub fn new(source: Option<DataSource>, sink: Option<DataSink>) -> Self {
let id = Uuid::new_v4();
Self { id, source, sink }
}
pub fn add(&self, objects: Vec<PathBuf>) -> Result<(), Error> {
if let Some(sink) = &self.sink {
sink.add(objects)
} else {
Err(Error::MissingDataSink)
}
}
pub fn add_obects(&self, objects: Vec<StixObject>) -> Result<(), Error> {
if let Some(sink) = &self.sink {
sink.add_obects(objects)
} else {
Err(Error::MissingDataSink)
}
}
pub fn get(&self, stix_id: &str, version: Option<&str>) -> Result<StixObject, Error> {
if let Some(source) = &self.source {
source.get(stix_id, version)
} else {
Err(Error::MissingDataSource)
}
}
pub fn all_versions(&self, stix_id: &str) -> Result<Vec<StixObject>, Error> {
if let Some(source) = &self.source {
source.all_versions(stix_id)
} else {
Err(Error::MissingDataSource)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataSink {
id: Uuid,
pub data_type: DataSinkType,
pub filters: Vec<Filter>,
}
impl DataSink {
pub fn new(data_type: DataSinkType, filters: Vec<Filter>) -> Self {
let id = Uuid::new_v4();
Self {
id,
data_type,
filters,
}
}
pub fn add(&self, objects: Vec<PathBuf>) -> Result<(), Error> {
match &self.data_type {
DataSinkType::FileSystem(fs) => fs.add(objects),
_ => Err(Error::UnsupportedDataType(self.data_type.to_string())),
}
}
pub fn add_obects(&self, objects: Vec<StixObject>) -> Result<(), Error> {
match &self.data_type {
DataSinkType::FileSystem(fs) => fs.add_objects(objects),
_ => Err(Error::UnsupportedDataType(self.data_type.to_string())),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum DataSinkType {
FileSystem(FilesystemSink),
Memory,
Taxii,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataSource {
id: Uuid,
pub data_type: DataSourceType,
pub filters: Vec<Filter>,
}
impl DataSource {
pub fn new(data_type: DataSourceType, filters: Vec<Filter>) -> Self {
let id = Uuid::new_v4();
Self {
id,
data_type,
filters,
}
}
pub fn get(&self, stix_id: &str, version: Option<&str>) -> Result<StixObject, Error> {
let identifier = Identifier::from_str(stix_id)?;
match &self.data_type {
DataSourceType::FileSystem(fs) => fs.get(&identifier, version),
_ => Err(Error::UnsupportedDataType(self.data_type.to_string())),
}
}
pub fn all_versions(&self, stix_id: &str) -> Result<Vec<StixObject>, Error> {
let identifier = Identifier::from_str(stix_id)?;
match &self.data_type {
DataSourceType::FileSystem(fs) => fs.all_versions(&identifier),
_ => Err(Error::UnsupportedDataType(self.data_type.to_string())),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum DataSourceType {
FileSystem(FilesystemSource),
Memory,
Taxii,
}