maec-rs 0.1.0

MAEC (Malware Attribute Enumeration and Characterization) data model library for Rust
Documentation
//! MAEC Malware Action object

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::common::{CommonProperties, MaecObject};
use crate::error::{MaecError, Result};
use crate::vocab_large::MalwareAction as MalwareActionVocab;

/// MAEC Malware Action
///
/// Represents a low-level action taken by malware (e.g., file operations, network connections).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct MalwareAction {
    /// Common MAEC properties
    #[serde(flatten)]
    pub common: CommonProperties,

    /// Name of the action
    pub name: MalwareActionVocab,

    /// Textual description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl MalwareAction {
    /// Creates a new MalwareAction builder
    pub fn builder() -> MalwareActionBuilder {
        MalwareActionBuilder::default()
    }

    /// Creates a minimal MalwareAction with just a name
    pub fn new(name: MalwareActionVocab) -> Self {
        Self {
            common: CommonProperties::new("malware-action", None),
            name,
            description: None,
        }
    }

    /// Validates the MalwareAction structure
    pub fn validate(&self) -> Result<()> {
        if self.common.r#type != "malware-action" {
            return Err(MaecError::ValidationError(format!(
                "type must be 'malware-action', got '{}'",
                self.common.r#type
            )));
        }

        if !crate::common::is_valid_maec_id(&self.common.id) {
            return Err(MaecError::InvalidId(self.common.id.clone()));
        }

        Ok(())
    }
}

impl MaecObject for MalwareAction {
    fn id(&self) -> &str {
        &self.common.id
    }

    fn type_(&self) -> &str {
        &self.common.r#type
    }

    fn created(&self) -> DateTime<Utc> {
        self.common.created
    }
}

/// Builder for MalwareAction objects
#[derive(Debug, Default)]
pub struct MalwareActionBuilder {
    id: Option<String>,
    name: Option<MalwareActionVocab>,
    description: Option<String>,
}

impl MalwareActionBuilder {
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    pub fn name(mut self, name: MalwareActionVocab) -> Self {
        self.name = Some(name);
        self
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    pub fn build(self) -> Result<MalwareAction> {
        let name = self.name.ok_or(MaecError::MissingField("name"))?;

        let mut common = CommonProperties::new("malware-action", None);
        if let Some(id) = self.id {
            common.id = id;
        }

        let action = MalwareAction {
            common,
            name,
            description: self.description,
        };

        action.validate()?;
        Ok(action)
    }
}