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
use std::ops::{Deref, DerefMut};

use crate::{CreateParserState, HasParser};
use crate::{ParseResult, Parser, StringParser};

#[derive(Clone, Debug)]
/// A single word.
pub struct Word<const MIN_LENGTH: usize = 1, const MAX_LENGTH: usize = 20>(pub String);

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Word<MIN_LENGTH, MAX_LENGTH> {
    /// Create a new word.
    pub fn new(word: String) -> Self {
        assert!(word.len() >= MIN_LENGTH);
        assert!(word.len() <= MAX_LENGTH);
        Self(word)
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> From<Word<MIN_LENGTH, MAX_LENGTH>>
    for String
{
    fn from(word: Word<MIN_LENGTH, MAX_LENGTH>) -> Self {
        word.0
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> From<String>
    for Word<MIN_LENGTH, MAX_LENGTH>
{
    fn from(word: String) -> Self {
        Self(word)
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Deref for Word<MIN_LENGTH, MAX_LENGTH> {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> DerefMut for Word<MIN_LENGTH, MAX_LENGTH> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// A parser for a word.
pub struct WordParser<const MIN_LENGTH: usize, const MAX_LENGTH: usize> {
    parser: StringParser<fn(char) -> bool>,
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Default
    for WordParser<MIN_LENGTH, MAX_LENGTH>
{
    fn default() -> Self {
        Self {
            parser: StringParser::new(MIN_LENGTH..=MAX_LENGTH)
                .with_allowed_characters(|c| c.is_ascii_alphanumeric()),
        }
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> CreateParserState
    for WordParser<MIN_LENGTH, MAX_LENGTH>
{
    fn create_parser_state(&self) -> <Self as Parser>::PartialState {
        self.parser.create_parser_state()
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Parser
    for WordParser<MIN_LENGTH, MAX_LENGTH>
{
    type Error = <StringParser<fn(char) -> bool> as Parser>::Error;
    type Output = Word<MIN_LENGTH, MAX_LENGTH>;
    type PartialState = <StringParser<fn(char) -> bool> as Parser>::PartialState;

    fn parse<'a>(
        &self,
        state: &Self::PartialState,
        input: &'a [u8],
    ) -> Result<ParseResult<'a, Self::PartialState, Self::Output>, Self::Error> {
        self.parser
            .parse(state, input)
            .map(|result| result.map(Into::into))
    }
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> HasParser for Word<MIN_LENGTH, MAX_LENGTH> {
    type Parser = WordParser<MIN_LENGTH, MAX_LENGTH>;

    fn new_parser() -> Self::Parser {
        WordParser::default()
    }

    fn create_parser_state() -> <Self::Parser as Parser>::PartialState {
        Default::default()
    }
}