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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! A collection of parses for use in command argument nodes.
pub mod angle;
pub mod block_pos;
pub mod bool;
pub mod color;
pub mod column_pos;
pub mod entity_anchor;
pub mod entity_selector;
pub mod gamemode;
pub mod inventory_slot;
pub mod numbers;
pub mod rotation;
pub mod score_holder;
pub mod strings;
pub mod swizzle;
pub mod time;
pub mod vec2;
pub mod vec3;

use std::ops::Add;

pub use block_pos::BlockPos;
pub use column_pos::ColumnPos;
pub use entity_anchor::EntityAnchor;
pub use entity_selector::EntitySelector;
pub use inventory_slot::InventorySlot;
pub use rotation::Rotation;
pub use score_holder::ScoreHolder;
pub use strings::{GreedyString, QuotableString};
pub use swizzle::Swizzle;
use thiserror::Error;
pub use time::Time;
use tracing::error;
pub(crate) use valence_server::protocol::packets::play::command_tree_s2c::Parser;
pub use vec2::Vec2;
pub use vec3::Vec3;

pub trait CommandArg: Sized {
    fn arg_from_str(string: &str) -> Result<Self, CommandArgParseError> {
        Self::parse_arg(&mut ParseInput::new(string))
    }

    fn parse_arg(input: &mut ParseInput) -> Result<Self, CommandArgParseError>;
    /// what will the client be sent
    fn display() -> Parser;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseInput<'a>(&'a str);

impl<'a> ParseInput<'a> {
    fn advance(&mut self) {
        self.advance_n_chars(1);
    }

    fn advance_n_chars(&mut self, n: usize) {
        if self.is_done() {
            return;
        }
        match self.0.char_indices().nth(n) {
            Some((len, _)) => {
                self.0 = &self.0[len..];
            }
            None => {
                self.0 = &self.0[self.0.len()..];
            }
        }
    }

    fn advance_n_bytes(&mut self, n: usize) {
        if self.is_done() {
            return;
        }
        self.0 = &self.0[n..];
    }
    pub fn new(input: &'a str) -> Self {
        ParseInput(input)
    }

    /// Returns the next character without advancing the input
    pub fn peek(&self) -> Option<char> {
        self.0.chars().next()
    }

    /// Returns the next n characters without advancing the input
    pub fn peek_n(&self, n: usize) -> &'a str {
        self.0
            .char_indices()
            .nth(n)
            .map_or(self.0, |(idx, _)| &self.0[..idx])
    }

    /// Returns the next word without advancing the input
    pub fn peek_word(&self) -> &'a str {
        self.0
            .char_indices()
            .find(|(_, c)| c.is_whitespace())
            .map_or(self.0, |(idx, _)| &self.0[..idx])
    }

    /// Checks if the input is empty
    pub fn is_done(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the next character and advances the input
    pub fn pop(&mut self) -> Option<char> {
        let c = self.peek()?;
        self.advance();
        Some(c)
    }

    /// Returns the next n characters and advances the input
    pub fn pop_n(&mut self, n: usize) -> &str {
        let s = self.peek_n(n);
        self.advance_n_bytes(s.len());
        s
    }

    /// Returns the next word and advances the input
    pub fn pop_word(&mut self) -> &str {
        let s = self.peek_word();
        self.advance_n_bytes(s.len());
        s
    }

    /// Returns the rest of the input and advances the input
    pub fn pop_all(&mut self) -> Option<&str> {
        let s = self.0;
        self.advance_n_bytes(self.0.len());
        Some(s)
    }

    /// Returns the next word and advances the input
    pub fn pop_to_next(&mut self, c: char) -> Option<&str> {
        let pos = self.0.find(c)?;
        let s = &self.0[..pos];
        self.advance_n_bytes(pos);
        Some(s)
    }

    /// Matches the case-insensitive string and advances the input if it matches
    pub fn match_next(&mut self, string: &str) -> bool {
        if self
            .0
            .to_lowercase()
            .starts_with(string.to_lowercase().as_str())
        {
            self.advance_n_bytes(string.len());
            true
        } else {
            false
        }
    }

    /// Skip all whitespace at the front of the input
    pub fn skip_whitespace(&mut self) {
        while let Some(c) = self.peek() {
            if c.is_whitespace() {
                self.advance();
            } else {
                break;
            }
        }
    }

    /// Set the inner string
    pub fn into_inner(self) -> &'a str {
        self.0
    }

    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

#[derive(Debug, Error)]
pub enum CommandArgParseError {
    // these should be player facing and not disclose internal information
    #[error("invalid argument, expected {expected} got {got}")] // e.g. "integer" number
    InvalidArgument { expected: String, got: String },
    #[error("invalid argument length")]
    InvalidArgLength,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AbsoluteOrRelative<T> {
    Absolute(T),
    Relative(T), // current value + T
}

impl<T> AbsoluteOrRelative<T>
where
    T: Add<Output = T> + Copy,
{
    pub fn get(&self, original: T) -> T {
        match self {
            Self::Absolute(num) => *num,
            Self::Relative(num) => *num + original,
        }
    }
}

impl<T> CommandArg for AbsoluteOrRelative<T>
where
    T: CommandArg + Default,
{
    fn parse_arg(input: &mut ParseInput) -> Result<Self, CommandArgParseError> {
        input.skip_whitespace();
        if input.peek() == Some('~') {
            input.advance();
            if input.peek() == Some(' ') || input.peek().is_none() {
                Ok(AbsoluteOrRelative::Relative(T::default()))
            } else {
                Ok(AbsoluteOrRelative::Relative(T::parse_arg(input)?))
            }
        } else if input.peek() == Some(' ') || input.peek().is_none() {
            Err(CommandArgParseError::InvalidArgLength)
        } else {
            Ok(AbsoluteOrRelative::Absolute(T::parse_arg(input)?))
        }
    }

    fn display() -> Parser {
        T::display()
    }
}

impl<T: Default> Default for AbsoluteOrRelative<T> {
    fn default() -> Self {
        AbsoluteOrRelative::Absolute(T::default())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_parse_input() {
        let mut input = ParseInput::new("The QuIck brown FOX jumps over the lazy dog");
        assert_eq!(input.peek(), Some('T'));
        assert_eq!(input.peek_n(0), "");
        assert_eq!(input.peek_n(1), "T");
        assert_eq!(input.peek_n(2), "Th");
        assert_eq!(input.peek_n(3), "The");

        assert_eq!(input.peek_word(), "The");
        input.pop_word();
        input.skip_whitespace();
        assert_eq!(input.peek_word(), "QuIck");

        assert!(input.match_next("quick"));
        input.pop();
        assert_eq!(input.peek_word(), "brown");

        assert!(input.match_next("brown fox"));
        assert_eq!(input.pop_all(), Some(" jumps over the lazy dog"));
    }
    #[test]
    fn test_absolute_or_relative() {
        let mut input = ParseInput::new("~");
        assert_eq!(
            AbsoluteOrRelative::<i32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Relative(0)
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("~1");
        assert_eq!(
            AbsoluteOrRelative::<i32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Relative(1)
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("~1.5");
        assert_eq!(
            AbsoluteOrRelative::<f32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Relative(1.5)
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("1");
        assert_eq!(
            AbsoluteOrRelative::<i32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Absolute(1)
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("1.5 ");
        assert_eq!(
            AbsoluteOrRelative::<f32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Absolute(1.5)
        );
        assert!(!input.is_done());

        let mut input = ParseInput::new("1.5 2");
        assert_eq!(
            AbsoluteOrRelative::<f32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Absolute(1.5)
        );
        assert!(!input.is_done());
        assert_eq!(
            AbsoluteOrRelative::<f32>::parse_arg(&mut input).unwrap(),
            AbsoluteOrRelative::Absolute(2.0)
        );
        assert!(input.is_done());
    }
}