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
//! The _modular parser_ module.
//!
//! Please refer to to the [`parsers`] module for a more detailed description of
//! this type of parser.
//!
//! [`crate::parsers`]

use crate::parsers::{BaseVersionParser, FullVersionParser, VersionParser};
use crate::{BaseVersion, FullVersion, ParserError, Version};

pub use error::{ModularParserError, NumberError};
pub use parser::{ParsedBase, ParsedFull, ParsedState, Parser, Unparsed};

mod component;
mod error;
mod parser;
mod take_while_peekable;

/// A convenience interface to the modular parser.
///
/// If you want low-lever access to the modular parser, and use it as intended, you should use the
/// [`modular::Parser`] struct directly. This interface primarily exists to have an implementation
/// of the _modular parser_ for the [`VersionParser`] trait, which allows for interchangeability of
/// implementations.
///
/// [`modular::Parser`]: Parser
#[derive(Debug)]
pub struct ModularParser;

impl VersionParser for ModularParser {
    fn parse_version<B: AsRef<[u8]>>(&self, input: B) -> Result<Version, ParserError> {
        let parser = Parser::from_slice(input.as_ref());

        parser.parse().map_err(ParserError::from)
    }
}

impl BaseVersionParser for ModularParser {
    fn parse_base<B: AsRef<[u8]>>(&self, input: B) -> Result<BaseVersion, ParserError> {
        let parser = Parser::from_slice(input.as_ref());

        parser
            .parse_base()
            .and_then(|parser| parser.finish_base_version())
            .map_err(ParserError::from)
    }
}

impl FullVersionParser for ModularParser {
    fn parse_full<B: AsRef<[u8]>>(&self, input: B) -> Result<FullVersion, ParserError> {
        let parser = Parser::from_slice(input.as_ref());

        parser
            .parse_full()
            .and_then(|parser| parser.finish_full_version())
            .map_err(ParserError::from)
    }
}