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
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

// Probably should create a macro to generate various of the entries. Now, I've to manually
// add a new topic to the name match function, and to the available topics list.
// The Rust documentation advises to use an enum if you now your values in advance.
// Here, the values are known in advance, but since I also need the string values, it might be
// an option to just use the string values, even for pattern matching.
// Another option is to generate help pages and the index by looking at the available
// pages (text files) in the docs/ directory. The category and page can be parsed
// (for example) from the file name.
// Or a similar option, include a 'docs.json' like file which specifies the meta data for the
// docs to be included.
#[derive(Debug, Clone, Copy)]
pub enum HelpTopicKind {
    Script,

    Blur,
    Brighten,
    Contrast,
    Filter3x3,
    FlipH,
    FlipV,
    GrayScale,
    HueRotate,
    Invert,
    Resize,
    Rotate90,
    Rotate180,
    Rotate270,
    Unsharpen,

    Unavailable,
}

impl HelpTopicKind {
    pub fn name(self) -> String {
        let text = match self {
            HelpTopicKind::Script => "script",
            HelpTopicKind::Blur => "blur",
            HelpTopicKind::Brighten => "brighten",
            HelpTopicKind::Contrast => "contrast",
            HelpTopicKind::Filter3x3 => "filter3x3",
            HelpTopicKind::FlipH => "fliph",
            HelpTopicKind::FlipV => "flipv",
            HelpTopicKind::GrayScale => "grayscale",
            HelpTopicKind::HueRotate => "huerotate",
            HelpTopicKind::Invert => "invert",
            HelpTopicKind::Resize => "resize",
            HelpTopicKind::Rotate90 => "rotate90",
            HelpTopicKind::Rotate180 => "rotate180",
            HelpTopicKind::Rotate270 => "rotate270",
            HelpTopicKind::Unsharpen => "unsharpen",
            _ => "",
        };

        text.to_string()
    }

    pub fn from_name(topic: &str) -> HelpTopicKind {
        match topic {
            "script" => HelpTopicKind::Script,
            "blur" => HelpTopicKind::Blur,
            "brighten" => HelpTopicKind::Brighten,
            "contrast" => HelpTopicKind::Contrast,
            "filter3x3" => HelpTopicKind::Filter3x3,
            "fliph" => HelpTopicKind::FlipH,
            "flipv" => HelpTopicKind::FlipV,
            "grayscale" => HelpTopicKind::GrayScale,
            "huerotate" => HelpTopicKind::HueRotate,
            "invert" => HelpTopicKind::Invert,
            "resize" => HelpTopicKind::Resize,
            "rotate90" => HelpTopicKind::Rotate90,
            "rotate180" => HelpTopicKind::Rotate180,
            "rotate270" => HelpTopicKind::Rotate270,
            "unsharpen" => HelpTopicKind::Unsharpen,
            _ => HelpTopicKind::Unavailable,
        }
    }

    pub fn text(self) -> String {
        let help_page = match self {
            HelpTopicKind::Script => include_str!("../../docs/cli_help_script.txt"),
            HelpTopicKind::Blur => include_str!("../../docs/cli_help_script_blur.txt"),
            HelpTopicKind::Brighten => include_str!("../../docs/cli_help_script_brighten.txt"),
            HelpTopicKind::Contrast => include_str!("../../docs/cli_help_script_contrast.txt"),
            HelpTopicKind::Filter3x3 => include_str!("../../docs/cli_help_script_filter3x3.txt"),
            HelpTopicKind::FlipH => include_str!("../../docs/cli_help_script_fliph.txt"),
            HelpTopicKind::FlipV => include_str!("../../docs/cli_help_script_flipv.txt"),
            HelpTopicKind::GrayScale => include_str!("../../docs/cli_help_script_grayscale.txt"),
            HelpTopicKind::HueRotate => include_str!("../../docs/cli_help_script_huerotate.txt"),
            HelpTopicKind::Invert => include_str!("../../docs/cli_help_script_invert.txt"),
            HelpTopicKind::Resize => include_str!("../../docs/cli_help_script_resize.txt"),
            HelpTopicKind::Rotate90 => include_str!("../../docs/cli_help_script_rotate90.txt"),
            HelpTopicKind::Rotate180 => include_str!("../../docs/cli_help_script_rotate180.txt"),
            HelpTopicKind::Rotate270 => include_str!("../../docs/cli_help_script_rotate270.txt"),
            HelpTopicKind::Unsharpen => include_str!("../../docs/cli_help_script_unsharpen.txt"),
            HelpTopicKind::Unavailable => unreachable!(),
        };

        help_page.to_string()
    }
}

impl PartialEq for HelpTopicKind {
    fn eq(&self, other: &HelpTopicKind) -> bool {
        self.name() == other.name()
    }
}

impl Eq for HelpTopicKind {}

impl Hash for HelpTopicKind {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name().hash(state);
    }
}

// Up for consideration: perhaps a (short) description should be added.
// This might help to select a topic from the index.
// Same for an explicit category.
#[derive(Debug)]
pub struct HelpTopic {
    name: String,
    pub help_text: String,
}

impl HelpTopic {
    pub fn get_name(&self) -> &str {
        &*self.name
    }
}

pub struct HelpIndex {
    index: HashMap<HelpTopicKind, HelpTopic>,
}

impl Default for HelpIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl HelpIndex {
    fn new() -> HelpIndex {
        let mut dict = HashMap::new();

        // ugh.
        insert_help_entry(&mut dict, HelpTopicKind::Script);
        insert_help_entry(&mut dict, HelpTopicKind::Blur);
        insert_help_entry(&mut dict, HelpTopicKind::Brighten);
        insert_help_entry(&mut dict, HelpTopicKind::Contrast);
        insert_help_entry(&mut dict, HelpTopicKind::Filter3x3);
        insert_help_entry(&mut dict, HelpTopicKind::FlipH);
        insert_help_entry(&mut dict, HelpTopicKind::FlipV);
        insert_help_entry(&mut dict, HelpTopicKind::GrayScale);
        insert_help_entry(&mut dict, HelpTopicKind::HueRotate);
        insert_help_entry(&mut dict, HelpTopicKind::Invert);
        insert_help_entry(&mut dict, HelpTopicKind::Resize);
        insert_help_entry(&mut dict, HelpTopicKind::Rotate90);
        insert_help_entry(&mut dict, HelpTopicKind::Rotate180);
        insert_help_entry(&mut dict, HelpTopicKind::Rotate270);
        insert_help_entry(&mut dict, HelpTopicKind::Unsharpen);

        HelpIndex { index: dict }
    }

    pub fn get_topic(&self, name: &str) -> Option<&HelpTopic> {
        let topic = &HelpTopicKind::from_name(name);
        self.index.get(topic)
    }

    pub fn get_available_topics(&self) -> String {
        let mut options = self
            .index
            .values()
            .map(HelpTopic::get_name)
            .collect::<Vec<_>>();

        options.sort_unstable();
        options.join("\n\t* ")
    }
}

// Dislike the cloning here, this should be possible in a different way (?)
fn insert_help_entry(dict: &mut HashMap<HelpTopicKind, HelpTopic>, kind: HelpTopicKind) {
    dict.insert(
        kind,
        HelpTopic {
            name: kind.name(),
            help_text: kind.text(),
        },
    );
}