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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2016-2018 Mateusz Sieczko and other GilRs Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use std::error::Error as StdError;
use std::fmt::{self, Display};

use uuid::Uuid;

use crate::ev::{Axis, AxisOrBtn, Button};

// Must be sorted!
static AXES_SDL: [&str; 31] = [
    "a",
    "b",
    "back",
    "c",
    "dpdown",
    "dpleft",
    "dpright",
    "dpup",
    "guide",
    "leftshoulder",
    "leftstick",
    "lefttrigger",
    "leftx",
    "lefty",
    "leftz",
    "misc1",
    "paddle1",
    "paddle2",
    "paddle3",
    "paddle4",
    "rightshoulder",
    "rightstick",
    "righttrigger",
    "rightx",
    "righty",
    "rightz",
    "start",
    "touchpad",
    "x",
    "y",
    "z",
];
static AXES: [AxisOrBtn; 31] = [
    AxisOrBtn::Btn(Button::South),
    AxisOrBtn::Btn(Button::East),
    AxisOrBtn::Btn(Button::Select),
    AxisOrBtn::Btn(Button::C),
    AxisOrBtn::Btn(Button::DPadDown),
    AxisOrBtn::Btn(Button::DPadLeft),
    AxisOrBtn::Btn(Button::DPadRight),
    AxisOrBtn::Btn(Button::DPadUp),
    AxisOrBtn::Btn(Button::Mode),
    AxisOrBtn::Btn(Button::LeftTrigger),
    AxisOrBtn::Btn(Button::LeftThumb),
    AxisOrBtn::Btn(Button::LeftTrigger2),
    AxisOrBtn::Axis(Axis::LeftStickX),
    AxisOrBtn::Axis(Axis::LeftStickY),
    AxisOrBtn::Axis(Axis::LeftZ),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::RightTrigger),
    AxisOrBtn::Btn(Button::RightThumb),
    AxisOrBtn::Btn(Button::RightTrigger2),
    AxisOrBtn::Axis(Axis::RightStickX),
    AxisOrBtn::Axis(Axis::RightStickY),
    AxisOrBtn::Axis(Axis::RightZ),
    AxisOrBtn::Btn(Button::Start),
    AxisOrBtn::Btn(Button::Unknown),
    AxisOrBtn::Btn(Button::West),
    AxisOrBtn::Btn(Button::North),
    AxisOrBtn::Btn(Button::Z),
];

pub struct Parser<'a> {
    data: &'a str,
    pos: usize,
    state: State,
}

impl<'a> Parser<'a> {
    pub fn new(mapping: &'a str) -> Self {
        Parser {
            data: mapping,
            pos: 0,
            state: State::Uuid,
        }
    }

    pub fn next_token(&mut self) -> Option<Result<Token<'_>, Error>> {
        if self.pos >= self.data.len() {
            None
        } else {
            Some(match self.state {
                State::Uuid => self.parse_uuid(),
                State::Name => self.parse_name(),
                State::KeyVal => self.parse_key_val(),
                State::Invalid => Err(Error::new(ErrorKind::InvalidParserState, self.pos)),
            })
        }
    }

    fn parse_uuid(&mut self) -> Result<Token<'_>, Error> {
        let next_comma = self.next_comma_or_end();
        let uuid_field = &self.data[self.pos..next_comma];
        let uuid = if uuid_field == "xinput" {
            Ok(Token::Uuid(Uuid::nil()))
        } else {
            Uuid::parse_str(uuid_field)
                .map(Token::Uuid)
                .map_err(|_| Error::new(ErrorKind::InvalidGuid, self.pos))
        };

        if uuid.is_err() {
            self.state = State::Invalid;
        } else if next_comma == self.data.len() {
            self.state = State::Invalid;

            return Err(Error::new(ErrorKind::UnexpectedEnd, self.pos));
        } else {
            self.state = State::Name;
            self.pos = next_comma + 1;
        }

        uuid
    }

    fn parse_name(&mut self) -> Result<Token<'_>, Error> {
        let next_comma = self.next_comma_or_end();
        let name = &self.data[self.pos..next_comma];

        self.state = State::KeyVal;
        self.pos = next_comma + 1;

        Ok(Token::Name(name))
    }

    fn parse_key_val(&mut self) -> Result<Token<'_>, Error> {
        let next_comma = self.next_comma_or_end();
        let pair = &self.data[self.pos..next_comma];
        let pos = self.pos;
        self.pos = next_comma + 1;

        let mut split = pair.split(':');
        let key = split
            .next()
            .ok_or_else(|| Error::new(ErrorKind::InvalidKeyValPair, pos))?;
        let value = split
            .next()
            .ok_or_else(|| Error::new(ErrorKind::InvalidKeyValPair, pos))?;

        if split.next().is_some() {
            return Err(Error::new(ErrorKind::InvalidKeyValPair, pos));
        }

        if value.is_empty() {
            return Err(Error::new(ErrorKind::EmptyValue, pos));
        }

        if key == "platform" {
            return Ok(Token::Platform(value));
        }

        let mut input = AxisRange::Full;
        let mut output = AxisRange::Full;
        let mut inverted = false;
        let mut is_axis = false;

        let key = match key.get(0..1) {
            Some("+") => {
                output = AxisRange::UpperHalf;
                &key[1..]
            }
            Some("-") => {
                output = AxisRange::LowerHalf;
                &key[1..]
            }
            _ => key,
        };

        let from = match value.get(0..1) {
            Some("+") if value.get(1..2) == Some("a") => {
                is_axis = true;
                input = AxisRange::UpperHalf;

                if value.get((value.len() - 1)..) == Some("~") {
                    inverted = true;

                    &value[2..(value.len() - 1)]
                } else {
                    &value[2..]
                }
            }
            Some("-") if value.get(1..2) == Some("a") => {
                is_axis = true;
                input = AxisRange::LowerHalf;

                if value.get((value.len() - 1)..) == Some("~") {
                    inverted = true;

                    &value[2..(value.len() - 1)]
                } else {
                    &value[2..]
                }
            }
            Some("a") => {
                is_axis = true;

                if value.get((value.len() - 1)..) == Some("~") {
                    inverted = true;

                    &value[1..(value.len() - 1)]
                } else {
                    &value[1..]
                }
            }
            Some("b") => &value[1..],
            Some("h") => {
                let dot_idx = value
                    .find('.')
                    .ok_or_else(|| Error::new(ErrorKind::InvalidValue, pos))?;
                let hat = value[1..dot_idx]
                    .parse()
                    .map_err(|_| Error::new(ErrorKind::InvalidValue, pos + 1))?;
                let direction = value
                    .get((dot_idx + 1)..)
                    .and_then(|s| s.parse().ok())
                    .ok_or_else(|| Error::new(ErrorKind::InvalidValue, pos + dot_idx + 1))?;

                let idx = AXES_SDL
                    .binary_search(&key)
                    .map_err(|_| Error::new(ErrorKind::UnknownButton, pos))?;

                return Ok(Token::HatMapping {
                    hat,
                    direction,
                    to: AXES[idx],
                    output,
                });
            }
            _ => return Err(Error::new(ErrorKind::InvalidValue, pos)),
        }
        .parse::<u16>()
        .map_err(|_| Error::new(ErrorKind::InvalidValue, pos))?;

        if is_axis {
            let idx = AXES_SDL
                .binary_search(&key)
                .map_err(|_| Error::new(ErrorKind::UnknownAxis, pos))?;

            Ok(Token::AxisMapping {
                from,
                to: AXES[idx],
                input,
                output,
                inverted,
            })
        } else {
            let idx = AXES_SDL
                .binary_search(&key)
                .map_err(|_| Error::new(ErrorKind::UnknownButton, pos))?;

            Ok(Token::ButtonMapping {
                from,
                to: AXES[idx],
                output,
            })
        }
    }

    fn next_comma_or_end(&self) -> usize {
        self.data[self.pos..]
            .find(',')
            .map(|x| x + self.pos)
            .unwrap_or_else(|| self.data.len())
    }
}

#[derive(Debug)]
pub enum Token<'a> {
    Uuid(Uuid),
    Platform(&'a str),
    Name(&'a str),
    #[allow(dead_code)]
    AxisMapping {
        from: u16,
        to: AxisOrBtn,
        input: AxisRange,
        output: AxisRange,
        inverted: bool,
    },
    ButtonMapping {
        from: u16,
        to: AxisOrBtn,
        #[allow(dead_code)]
        output: AxisRange,
    },
    // This is just SDL representation, we will convert this to axis mapping later
    HatMapping {
        hat: u16,
        // ?
        direction: u16,
        to: AxisOrBtn,
        #[allow(dead_code)]
        output: AxisRange,
    },
}

#[repr(u8)]
#[derive(Debug)]
pub enum AxisRange {
    LowerHalf,
    UpperHalf,
    Full,
}

#[derive(Copy, Clone, Eq, PartialEq)]
enum State {
    Uuid,
    Name,
    KeyVal,
    Invalid,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    pub(crate) position: usize,
    kind: ErrorKind,
}

impl Error {
    pub fn new(kind: ErrorKind, position: usize) -> Self {
        Error { position, kind }
    }

    pub fn kind(&self) -> &ErrorKind {
        &self.kind
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
    InvalidGuid,
    InvalidKeyValPair,
    InvalidValue,
    EmptyValue,
    UnknownAxis,
    UnknownButton,
    InvalidParserState,
    UnexpectedEnd,
}

impl StdError for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self.kind {
            ErrorKind::InvalidGuid => "GUID is invalid",
            ErrorKind::InvalidKeyValPair => "expected key value pair",
            ErrorKind::InvalidValue => "value is not valid",
            ErrorKind::EmptyValue => "value is empty",
            ErrorKind::UnknownAxis => "invalid axis name",
            ErrorKind::UnknownButton => "invalid button name",
            ErrorKind::InvalidParserState => "attempt to parse after unrecoverable error",
            ErrorKind::UnexpectedEnd => "mapping does not have all required fields",
        };

        f.write_fmt(format_args!("{} at {}", s, self.position))
    }
}

#[cfg(test)]
mod tests {
    use crate::mapping::parser::{ErrorKind, Parser};
    use crate::utils::PATH_SEPARATOR;

    #[test]
    fn test_all_sdl_mappings_for_parse_errors() {
        let included_mappings = include_str!(concat!(
            env!("OUT_DIR"),
            PATH_SEPARATOR!(),
            "gamecontrollerdb.txt"
        ))
        .lines();

        let mut errors = 0;
        let mut index = 0;
        for line in included_mappings {
            let mut parser = Parser::new(line);

            while let Some(token) = parser.next_token() {
                if let Err(ref e) = token {
                    if e.kind() != &ErrorKind::EmptyValue {
                        errors += 1;
                        println!("{:?}", e);
                        println!(
                            "{}: {} (...) {}\n",
                            index,
                            line.chars().take(50).collect::<String>(),
                            line.chars().skip(e.position).take(15).collect::<String>()
                        );

                        if e.kind() == &ErrorKind::InvalidParserState {
                            break;
                        }
                    }
                }
                index += 1;
            }
        }
        assert_eq!(errors, 0);
    }
}