Struct version_number::parsers::modular::Parser

source ·
pub struct Parser<'p, S: ParsedState> { /* private fields */ }
Expand description

A parser which may be used to parse a Version or its discriminants (BaseVersion and FullVersion), incrementally.

Implementations§

source§

impl<'p> Parser<'p, Unparsed>

source

pub fn from_slice(bytes: &'p [u8]) -> Parser<'p, Unparsed>

Construct a parser from a byte slice.

§Example
use version_number::parsers::modular::Parser;
let parser = Parser::from_slice("1.0.0".as_bytes());
source§

impl<'p> Parser<'p, Unparsed>

source

pub fn parse_base(self) -> Result<Parser<'p, ParsedBase>, ModularParserError>

Parse the base of a Version. The base are the major and minor components of a version. An example of a base version which will parse, would be 1.2.

This method returns another Parser instance. To get the parsed version after parsing the base, you may use Parser::finish.

In case you want to either parse a two or three component version, and you don’t care which one you have, you should use Parser::parse instead.

§Example
use version_number::BaseVersion;
use version_number::parsers::modular::{Parser};

let parser = Parser::from_slice("1.2".as_bytes());

let base = parser.parse_base().unwrap();

assert_eq!(base.inner_version(), &BaseVersion::new(1, 2));
source

pub fn parse_full(self) -> Result<Parser<'p, ParsedFull>, ModularParserError>

Parse a full, three component major, minor, patch Version. A two component input, consisting of only the major and minor components, will be rejected.

§Example
use version_number::{BaseVersion, FullVersion};
use version_number::parsers::modular::{Parser};
let parser = Parser::from_slice("1.2.3".as_bytes());

let base = parser.parse_full().unwrap();

assert_eq!(base.inner_version(), &FullVersion::new(1, 2, 3));
source

pub fn parse(self) -> Result<Version, ModularParserError>

Parse a base, two component major.minor Version, or a full, three component major.minor.patch, depending on the input.

§Example 1
use version_number::{BaseVersion, FullVersion, Version};
use version_number::parsers::modular::Parser;

let parser = Parser::from_slice("1.2".as_bytes());

let version = parser.parse();

assert_eq!(version.unwrap(), Version::Base(BaseVersion::new(1, 2)));
§Example 2
use version_number::{FullVersion, Version};
use version_number::parsers::modular::Parser;

let parser = Parser::from_slice("1.2.3".as_bytes());

let version = parser.parse();

assert_eq!(version.unwrap(), Version::Full(FullVersion::new(1, 2, 3)));
§Example 3
use version_number::{FullVersion, Version};
use version_number::parsers::modular::Parser;

let parser = Parser::from_slice("1.2.".as_bytes());

let version = parser.parse();

assert!(version.is_err());
source§

impl<'p> Parser<'p, ParsedBase>

source

pub fn parse_patch(self) -> Result<Parser<'p, ParsedFull>, ModularParserError>

Parse the patch component, to produce a FullVersion.

§Example
use version_number::{FullVersion};
use version_number::parsers::modular::Parser;

let input = "1.2.3";

let parser = Parser::from_slice(input.as_bytes());
let full = parser
    .parse_base()
    .unwrap()
    .parse_patch()
    .unwrap();

assert_eq!(full.inner_version(), &FullVersion::new(1, 2, 3));
source

pub fn parse_patch_or_finish(self) -> Result<Version, ModularParserError>

Parses a patch component if it exists and returns a Version::Full, or if the input does not have a third component, returns the two component Version::Base variant instead.

Prefer Parser::parse over this method when possible, as this method clones the underlying iterator to determine whether we do have additional content.

source

pub fn finish(self) -> Result<Version, ModularParserError>

Checks that there is no remaining input, and returns a Version, which wraps the parsed base version.

When there is remaining input, this method will return a [ModularParserError::ExpectedEOI] instead.

source

pub fn finish_base_version(self) -> Result<BaseVersion, ModularParserError>

Checks that there is no remaining input, and returns a BaseVersion.

When there is remaining input, this method will return a [ModularParserError::ExpectedEOI] instead.

source

pub fn inner_version(&self) -> &BaseVersion

Returns the so far successfully parsed version state.

NB: Unless the end of input has been reached, this version may not be valid.

source§

impl<'p> Parser<'p, ParsedFull>

source

pub fn finish(self) -> Result<Version, ModularParserError>

Checks that there is no remaining input, and returns a Version, which wraps the parsed base version.

When there is remaining input, this method will return a [ModularParserError::ExpectedEOI]

source

pub fn finish_full_version(self) -> Result<FullVersion, ModularParserError>

Checks that there is no remaining input, and returns a FullVersion.

When there is remaining input, this method will return a [ModularParserError::ExpectedEOI] instead.

source

pub fn inner_version(&self) -> &FullVersion

Returns the so far successfully parsed version.

NB: Unless the end of input has been reached, this version may not be valid.

Trait Implementations§

source§

impl<'p, S: Debug + ParsedState> Debug for Parser<'p, S>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'p, S> Freeze for Parser<'p, S>
where S: Freeze,

§

impl<'p, S> RefUnwindSafe for Parser<'p, S>
where S: RefUnwindSafe,

§

impl<'p, S> Send for Parser<'p, S>
where S: Send,

§

impl<'p, S> Sync for Parser<'p, S>
where S: Sync,

§

impl<'p, S> Unpin for Parser<'p, S>
where S: Unpin,

§

impl<'p, S> UnwindSafe for Parser<'p, S>
where S: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.