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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::app::{
    AppMeta, Autocomplete, AutocompleteSuggestion, CommandMatches, ContextAwareParse, Runnable,
};
use crate::utils::CaseInsensitiveStr;
use async_trait::async_trait;
use caith::Roller;
use initiative_macros::changelog;
use std::fmt;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AppCommand {
    About,
    Changelog,
    Debug,
    Help,
    Roll(String),
}

#[async_trait(?Send)]
impl Runnable for AppCommand {
    async fn run(self, _input: &str, app_meta: &mut AppMeta) -> Result<String, String> {
        Ok(match self {
            Self::About => include_str!("../../../../data/about.md")
                .trim_end()
                .to_string(),
            Self::Debug => format!(
                "{:?}\n\n{:?}",
                app_meta,
                app_meta.repository.journal().await,
            ),
            Self::Changelog => changelog!().to_string(),
            Self::Help => include_str!("../../../../data/help.md")
                .trim_end()
                .to_string(),
            Self::Roll(s) => Roller::new(&s)
                .ok()
                .and_then(|r| r.roll_with(&mut app_meta.rng).ok())
                .map(|result| {
                    result
                        .to_string()
                        .trim_end()
                        .replace('\n', "\\\n")
                        .replace('`', "")
                })
                .ok_or_else(|| {
                    format!(
                        "\"{}\" is not a valid dice formula. See `help` for some examples.",
                        s
                    )
                })?,
        })
    }
}

#[async_trait(?Send)]
impl ContextAwareParse for AppCommand {
    async fn parse_input(input: &str, _app_meta: &AppMeta) -> CommandMatches<Self> {
        if input.eq_ci("about") {
            CommandMatches::new_canonical(Self::About)
        } else if input.eq_ci("changelog") {
            CommandMatches::new_canonical(Self::Changelog)
        } else if input.eq_ci("debug") {
            CommandMatches::new_canonical(Self::Debug)
        } else if input.eq_ci("help") {
            CommandMatches::new_canonical(Self::Help)
        } else if input.starts_with_ci("roll ") {
            CommandMatches::new_canonical(Self::Roll(input[5..].to_string()))
        } else if !input.chars().all(|c| c.is_ascii_digit())
            && Roller::new(input).map_or(false, |r| r.roll().is_ok())
        {
            CommandMatches::new_fuzzy(Self::Roll(input.to_string()))
        } else {
            CommandMatches::default()
        }
    }
}

#[async_trait(?Send)]
impl Autocomplete for AppCommand {
    async fn autocomplete(input: &str, _app_meta: &AppMeta) -> Vec<AutocompleteSuggestion> {
        if input.is_empty() {
            return Vec::new();
        }

        [
            AutocompleteSuggestion::new("about", "about initiative.sh"),
            AutocompleteSuggestion::new("changelog", "show latest updates"),
            AutocompleteSuggestion::new("help", "how to use initiative.sh"),
        ]
        .into_iter()
        .filter(|suggestion| suggestion.term.starts_with_ci(input))
        .chain(
            ["roll"]
                .into_iter()
                .filter(|s| s.starts_with_ci(input))
                .map(|_| AutocompleteSuggestion::new("roll [dice]", "roll eg. 8d6 or d20+3")),
        )
        .collect()
    }
}

impl fmt::Display for AppCommand {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            Self::About => write!(f, "about"),
            Self::Changelog => write!(f, "changelog"),
            Self::Debug => write!(f, "debug"),
            Self::Help => write!(f, "help"),
            Self::Roll(s) => write!(f, "roll {}", s),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::app::assert_autocomplete;
    use crate::storage::NullDataStore;
    use crate::Event;
    use tokio_test::block_on;

    #[test]
    fn parse_input_test() {
        let app_meta = app_meta();

        assert_eq!(
            CommandMatches::new_canonical(AppCommand::Debug),
            block_on(AppCommand::parse_input("debug", &app_meta)),
        );

        assert_eq!(
            CommandMatches::new_canonical(AppCommand::Roll("d20".to_string())),
            block_on(AppCommand::parse_input("roll d20", &app_meta)),
        );

        assert_eq!(
            CommandMatches::new_fuzzy(AppCommand::Roll("d20".to_string())),
            block_on(AppCommand::parse_input("d20", &app_meta)),
        );

        assert_eq!(
            CommandMatches::default(),
            block_on(AppCommand::parse_input("potato", &app_meta)),
        );
    }

    #[test]
    fn autocomplete_test() {
        let app_meta = app_meta();

        [
            ("about", "about initiative.sh"),
            ("changelog", "show latest updates"),
            ("help", "how to use initiative.sh"),
        ]
        .into_iter()
        .for_each(|(term, summary)| {
            assert_eq!(
                vec![AutocompleteSuggestion::new(term, summary)],
                block_on(AppCommand::autocomplete(term, &app_meta)),
            );

            assert_eq!(
                block_on(AppCommand::autocomplete(term, &app_meta)),
                block_on(AppCommand::autocomplete(&term.to_uppercase(), &app_meta)),
            );
        });

        assert_autocomplete(
            &[("about", "about initiative.sh")][..],
            block_on(AppCommand::autocomplete("a", &app_meta)),
        );

        assert_autocomplete(
            &[("about", "about initiative.sh")][..],
            block_on(AppCommand::autocomplete("A", &app_meta)),
        );

        assert_autocomplete(
            &[("roll [dice]", "roll eg. 8d6 or d20+3")][..],
            block_on(AppCommand::autocomplete("roll", &app_meta)),
        );

        // Debug should be excluded from the autocomplete results.
        assert_eq!(
            Vec::<AutocompleteSuggestion>::new(),
            block_on(AppCommand::autocomplete("debug", &app_meta)),
        );
    }

    #[test]
    fn display_test() {
        let app_meta = app_meta();

        [
            AppCommand::About,
            AppCommand::Changelog,
            AppCommand::Debug,
            AppCommand::Help,
        ]
        .into_iter()
        .for_each(|command| {
            let command_string = command.to_string();
            assert_ne!("", command_string);

            assert_eq!(
                CommandMatches::new_canonical(command.clone()),
                block_on(AppCommand::parse_input(&command_string, &app_meta)),
                "{}",
                command_string,
            );

            assert_eq!(
                CommandMatches::new_canonical(command),
                block_on(AppCommand::parse_input(
                    &command_string.to_uppercase(),
                    &app_meta
                )),
                "{}",
                command_string.to_uppercase(),
            );
        });

        assert_eq!("roll d20", AppCommand::Roll("d20".to_string()).to_string());

        assert_eq!(
            CommandMatches::new_canonical(AppCommand::Roll("d20".to_string())),
            block_on(AppCommand::parse_input("roll d20", &app_meta)),
        );

        assert_eq!(
            CommandMatches::new_canonical(AppCommand::Roll("D20".to_string())),
            block_on(AppCommand::parse_input("ROLL D20", &app_meta)),
        );
    }

    fn event_dispatcher(_event: Event) {}

    fn app_meta() -> AppMeta {
        AppMeta::new(NullDataStore, &event_dispatcher)
    }
}