azalea_brigadier/arguments/
long_argument_type.rs1use std::{any::Any, sync::Arc};
2
3use super::ArgumentType;
4use crate::{
5 context::CommandContext,
6 errors::{BuiltInError, CommandSyntaxError},
7 string_reader::StringReader,
8};
9
10#[derive(Default)]
11struct Long {
12 pub minimum: Option<i64>,
13 pub maximum: Option<i64>,
14}
15
16impl ArgumentType for Long {
17 fn parse(&self, reader: &mut StringReader) -> Result<Arc<dyn Any>, CommandSyntaxError> {
18 let start = reader.cursor;
19 let result = reader.read_long()?;
20 if let Some(minimum) = self.minimum
21 && result < minimum
22 {
23 reader.cursor = start;
24 return Err(BuiltInError::LongTooSmall {
25 found: result,
26 min: minimum,
27 }
28 .create_with_context(reader));
29 }
30 if let Some(maximum) = self.maximum
31 && result > maximum
32 {
33 reader.cursor = start;
34 return Err(BuiltInError::LongTooBig {
35 found: result,
36 max: maximum,
37 }
38 .create_with_context(reader));
39 }
40 Ok(Arc::new(result))
41 }
42
43 fn examples(&self) -> Vec<String> {
44 vec!["0", "123", "-123"]
45 .into_iter()
46 .map(|s| s.to_string())
47 .collect()
48 }
49}
50
51pub fn long() -> impl ArgumentType {
52 Long::default()
53}
54pub fn get_long<S>(context: &CommandContext<S>, name: &str) -> Option<i64> {
55 context
56 .argument(name)
57 .unwrap()
58 .downcast_ref::<i64>()
59 .copied()
60}