feature_check/expr/parser.rs
1/*
2 * SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
3 * SPDX-License-Identifier: BSD-2-Clause
4 */
5//! A set of parsers for the query expressions supported by feature-check.
6
7use std::collections::HashMap;
8
9use crate::defs::{Mode, ParseError};
10use crate::version::{ParseError as VParseError, Version};
11
12pub mod p_nom;
13
14/// Parse a "feature" or "feature op version" expression for later evaluation.
15///
16/// Returns either [`Mode::Single`] or [`Mode::Simple`].
17///
18/// # Errors
19///
20/// Will return an error if the expression is neither a single feature name nor in
21/// the "var op value" format or if an unrecognized comparison operator is specified.
22#[inline]
23pub fn parse_expr(expr: &str) -> Result<Mode, ParseError> {
24 p_nom::parse_expr(expr)
25}
26
27/// Parse a version string.
28///
29/// # Errors
30///
31/// Returns an error if the version string is invalid.
32#[inline]
33pub fn parse_version(value: &str) -> Result<Version, VParseError> {
34 p_nom::parse_version(value)
35}
36
37/// Parse a line of `feature[=version]` pairs.
38///
39/// # Errors
40///
41/// Returns an error if the feature names or version strings are invalid.
42#[inline]
43pub fn parse_features_line(value: &str) -> Result<HashMap<String, Version>, ParseError> {
44 p_nom::parse_features_line(value)
45}