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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use fehler::throws;
use serde_derive::Deserialize;
use std::{collections::HashMap, path::Path};

#[derive(Debug, Deserialize)]
pub struct SkillTree {
    pub group: Vec<Group>,
    pub cluster: Option<Vec<Cluster>>,
    pub graphviz: Option<Graphviz>,
    pub doc: Option<Doc>,
}

#[derive(Debug, Deserialize)]
pub struct Graphviz {
    pub rankdir: Option<String>,
}

#[derive(Default, Debug, Deserialize)]
pub struct Doc {
    pub columns: Vec<String>,
    pub defaults: Option<HashMap<String, String>>,
    pub emoji: Option<HashMap<String, EmojiMap>>,
}

pub type EmojiMap = HashMap<String, String>;

#[derive(Debug, Deserialize)]
pub struct Cluster {
    pub name: String,
    pub label: String,
    pub color: Option<String>,
    pub style: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Group {
    pub name: String,
    pub cluster: Option<String>,
    pub label: Option<String>,
    pub requires: Option<Vec<String>>,
    pub description: Option<Vec<String>>,
    pub items: Vec<Item>,
    pub width: Option<f64>,
    pub status: Option<Status>,
    pub href: Option<String>,
    pub header_color: Option<String>,
    pub description_color: Option<String>,
}

#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct GroupIndex(pub usize);

pub type Item = HashMap<String, String>;

#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct ItemIndex(pub usize);

#[derive(Copy, Clone, Debug, Deserialize)]
pub enum Status {
    /// Can't work on it now
    Blocked,

    /// Would like to work on it, but need someone
    Unassigned,

    /// People are actively working on it
    Assigned,

    /// This is done!
    Complete,
}

impl SkillTree {
    #[throws(anyhow::Error)]
    pub fn load(path: &Path) -> SkillTree {
        let skill_tree_text = std::fs::read_to_string(path)?;
        Self::parse(&skill_tree_text)?
    }

    #[throws(anyhow::Error)]
    pub fn parse(text: &str) -> SkillTree {
        toml::from_str(text)?
    }

    #[throws(anyhow::Error)]
    pub fn validate(&self) {
        // gather: valid requires entries

        for group in &self.group {
            group.validate(self)?;
        }
    }

    pub fn groups(&self) -> impl Iterator<Item = &Group> {
        self.group.iter()
    }

    pub fn group_named(&self, name: &str) -> Option<&Group> {
        self.group.iter().find(|g| g.name == name)
    }

    /// Returns the expected column titles for each item (excluding the label).
    pub fn columns(&self) -> &[String] {
        if let Some(doc) = &self.doc {
            &doc.columns
        } else {
            &[]
        }
    }

    /// Translates an "input" into an emoji, returning "input" if not found.
    pub fn emoji<'me>(&'me self, column: &str, input: &'me str) -> &'me str {
        if let Some(doc) = &self.doc {
            if let Some(emoji_maps) = &doc.emoji {
                if let Some(emoji_map) = emoji_maps.get(column) {
                    if let Some(output) = emoji_map.get(input) {
                        return output;
                    }
                }
            }
        }
        input
    }
}

impl Group {
    #[throws(anyhow::Error)]
    pub fn validate(&self, tree: &SkillTree) {
        // check: that `name` is a valid graphviz identifier

        // check: each of the things in requires has the form
        //        `identifier` or `identifier:port` and that all those
        //        identifiers map to groups

        for group_name in self.requires.iter().flatten() {
            if tree.group_named(group_name).is_none() {
                anyhow::bail!(
                    "the group `{}` has a dependency on a group `{}` that does not exist",
                    self.name,
                    group_name,
                )
            }
        }

        for item in &self.items {
            item.validate()?;
        }
    }

    pub fn items(&self) -> impl Iterator<Item = &Item> {
        self.items.iter()
    }
}

pub trait ItemExt {
    fn href(&self) -> Option<&String>;
    fn label(&self) -> &String;
    fn column_value<'me>(&'me self, tree: &'me SkillTree, c: &str) -> &'me str;

    #[allow(redundant_semicolons)] // bug in "throws"
    #[throws(anyhow::Error)]
    fn validate(&self);
}

impl ItemExt for Item {
    fn href(&self) -> Option<&String> {
        self.get("href")
    }

    fn label(&self) -> &String {
        self.get("label").unwrap()
    }

    fn column_value<'me>(&'me self, tree: &'me SkillTree, c: &str) -> &'me str {
        if let Some(v) = self.get(c) {
            return v;
        }

        if let Some(doc) = &tree.doc {
            if let Some(defaults) = &doc.defaults {
                if let Some(default_value) = defaults.get(c) {
                    return default_value;
                }
            }
        }

        ""
    }

    #[throws(anyhow::Error)]
    fn validate(&self) {
        // check: each of the things in requires has the form
        //        `identifier` or `identifier:port` and that all those
        //        identifiers map to groups

        // check: only contains known keys
    }
}