xio_jobset 0.11.0

XIO jobset datatypes
Documentation
use {Command, Condition, IndexMap, Job, JobSet};

/// Extract all descriptions from an item.
pub trait ExtractDescriptions {
    /// The type of the extracted descriptions.
    type Descriptions;

    /// In this method, the extraction is being processed.
    fn extract_descriptions(&self) -> Self::Descriptions;
}

impl<T> ExtractDescriptions for IndexMap<String, T>
where
    T: ExtractDescriptions,
{
    type Descriptions = IndexMap<String, T::Descriptions>;

    fn extract_descriptions(&self) -> Self::Descriptions {
        self.iter()
            .map(|(k, v)| (k.to_string(), v.extract_descriptions()))
            .collect()
    }
}

impl ExtractDescriptions for Job {
    type Descriptions = Vec<CommandDescription>;

    fn extract_descriptions(&self) -> Self::Descriptions {
        self.commands.extract_descriptions()
    }
}

impl<T> ExtractDescriptions for Vec<T>
where
    T: ExtractDescriptions,
{
    type Descriptions = Vec<T::Descriptions>;

    fn extract_descriptions(&self) -> Self::Descriptions {
        self.iter().map(|c| c.extract_descriptions()).collect()
    }
}

impl ExtractDescriptions for Command {
    type Descriptions = CommandDescription;

    fn extract_descriptions(&self) -> Self::Descriptions {
        CommandDescription {
            message: self.message.to_string(),
            description: self.description.to_string(),
            conditions: self.conditions.extract_descriptions(),
        }
    }
}

impl ExtractDescriptions for Condition {
    type Descriptions = ConditionDescription;

    fn extract_descriptions(&self) -> Self::Descriptions {
        ConditionDescription {
            message: self.message.to_string(),
            description: self.description.to_string(),
        }
    }
}

/// The description of a command and the conditions belonging to it.
#[derive(Clone, Debug, Default)]
pub struct CommandDescription {
    /// The description of the command itself.
    pub description: String,
    /// The message of the command itself.
    pub message: String,
    /// The descriptions of the conditions.
    pub conditions: Vec<ConditionDescription>,
}

/// The caption of a condition;
#[derive(Clone, Debug, Default)]
pub struct ConditionDescription {
    /// The description of the condition.
    pub description: String,
    /// The message of the condition.
    pub message: String,
}

impl ExtractDescriptions for JobSet {
    type Descriptions = IndexMap<String, Vec<CommandDescription>>;

    fn extract_descriptions(&self) -> Self::Descriptions {
        self.jobs.extract_descriptions()
    }
}