sarif_rust 0.3.0

A comprehensive Rust library for parsing, generating, and manipulating SARIF (Static Analysis Results Interchange Format) v2.1.0 files
Documentation
//! SARIF message types
//!
//! This module defines message-related structures for conveying information in SARIF.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A message string or message format string rendered in multiple formats
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Message {
    /// A plain text message string
    pub text: Option<String>,

    /// A Markdown message string  
    pub markdown: Option<String>,

    /// The identifier for this message
    pub id: Option<String>,

    /// An array of strings to substitute into the message string
    pub arguments: Option<Vec<String>>,

    /// Key/value pairs that provide additional information about the message
    #[serde(flatten)]
    pub properties: Option<HashMap<String, serde_json::Value>>,
}

/// Encapsulates a message intended to be read by the end user
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MultiformatMessage {
    /// A plain text message string
    pub text: String,

    /// A Markdown message string
    pub markdown: Option<String>,

    /// Key/value pairs that provide additional information about the message
    #[serde(flatten)]
    pub properties: Option<HashMap<String, serde_json::Value>>,
}

impl Message {
    /// Create a new message with plain text
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: Some(text.into()),
            markdown: None,
            id: None,
            arguments: None,
            properties: None,
        }
    }

    /// Create a message with both text and markdown
    pub fn with_markdown(text: impl Into<String>, markdown: impl Into<String>) -> Self {
        Self {
            text: Some(text.into()),
            markdown: Some(markdown.into()),
            id: None,
            arguments: None,
            properties: None,
        }
    }

    /// Create a message with format arguments
    pub fn with_arguments(text: impl Into<String>, arguments: Vec<String>) -> Self {
        Self {
            text: Some(text.into()),
            markdown: None,
            id: None,
            arguments: Some(arguments),
            properties: None,
        }
    }

    /// Add an argument to the message
    pub fn add_argument(mut self, arg: impl Into<String>) -> Self {
        self.arguments.get_or_insert_with(Vec::new).push(arg.into());
        self
    }

    /// Set the message ID
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }
}

impl MultiformatMessage {
    /// Create a new multiformat message
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            markdown: None,
            properties: None,
        }
    }

    /// Add markdown content
    pub fn with_markdown(mut self, markdown: impl Into<String>) -> Self {
        self.markdown = Some(markdown.into());
        self
    }
}

impl From<String> for Message {
    fn from(text: String) -> Self {
        Message::new(text)
    }
}

impl From<&str> for Message {
    fn from(text: &str) -> Self {
        Message::new(text)
    }
}

impl From<String> for MultiformatMessage {
    fn from(text: String) -> Self {
        MultiformatMessage::new(text)
    }
}

impl From<&str> for MultiformatMessage {
    fn from(text: &str) -> Self {
        MultiformatMessage::new(text)
    }
}