azalea_brigadier/arguments/
bool_argument_type.rs

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
use std::{any::Any, sync::Arc};

use super::ArgumentType;
use crate::{
    context::CommandContext,
    exceptions::CommandSyntaxException,
    string_reader::StringReader,
    suggestion::{Suggestions, SuggestionsBuilder},
};

#[derive(Default)]
struct Boolean;

impl ArgumentType for Boolean {
    fn parse(&self, reader: &mut StringReader) -> Result<Arc<dyn Any>, CommandSyntaxException> {
        Ok(Arc::new(reader.read_boolean()?))
    }

    fn list_suggestions(&self, mut builder: SuggestionsBuilder) -> Suggestions {
        if "true".starts_with(builder.remaining_lowercase()) {
            builder = builder.suggest("true");
        }
        if "false".starts_with(builder.remaining_lowercase()) {
            builder = builder.suggest("false");
        }
        builder.build()
    }

    fn examples(&self) -> Vec<String> {
        vec!["true".to_string(), "false".to_string()]
    }
}

pub fn bool() -> impl ArgumentType {
    Boolean
}
pub fn get_bool<S>(context: &CommandContext<S>, name: &str) -> Option<bool> {
    context
        .argument(name)
        .expect("argument with name not found")
        .downcast_ref::<bool>()
        .cloned()
}