version_number/parsers/
modular.rs

1//! The _modular parser_ module.
2//!
3//! Please refer to to the [`parsers`] module for a more detailed description of
4//! this type of parser.
5//!
6//! [`crate::parsers`]
7
8use crate::parsers::{BaseVersionParser, FullVersionParser, VersionParser};
9use crate::{BaseVersion, FullVersion, ParserError, Version};
10
11pub use error::{ModularParserError, NumberError};
12pub use parser::{ParsedBase, ParsedFull, ParsedState, Parser, Unparsed};
13
14mod component;
15mod error;
16mod parser;
17mod take_while_peekable;
18
19/// A convenience interface to the modular parser.
20///
21/// If you want low-lever access to the modular parser, and use it as intended, you should use the
22/// [`modular::Parser`] struct directly. This interface primarily exists to have an implementation
23/// of the _modular parser_ for the [`VersionParser`] trait, which allows for interchangeability of
24/// implementations.
25///
26/// [`modular::Parser`]: Parser
27#[derive(Debug)]
28pub struct ModularParser;
29
30impl VersionParser for ModularParser {
31    fn parse_version<B: AsRef<[u8]>>(&self, input: B) -> Result<Version, ParserError> {
32        let parser = Parser::from_slice(input.as_ref());
33
34        parser.parse().map_err(ParserError::from)
35    }
36}
37
38impl BaseVersionParser for ModularParser {
39    fn parse_base<B: AsRef<[u8]>>(&self, input: B) -> Result<BaseVersion, ParserError> {
40        let parser = Parser::from_slice(input.as_ref());
41
42        parser
43            .parse_base()
44            .and_then(|parser| parser.finish_base_version())
45            .map_err(ParserError::from)
46    }
47}
48
49impl FullVersionParser for ModularParser {
50    fn parse_full<B: AsRef<[u8]>>(&self, input: B) -> Result<FullVersion, ParserError> {
51        let parser = Parser::from_slice(input.as_ref());
52
53        parser
54            .parse_full()
55            .and_then(|parser| parser.finish_full_version())
56            .map_err(ParserError::from)
57    }
58}