1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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()
    }
}