use std::collections::HashMap;
use std::fmt::Debug;
use anyhow::Error as AnyError;
use thiserror::Error;
use crate::version::Version;
pub const DEFAULT_OPTION_NAME: &str = "--features";
pub const DEFAULT_PREFIX: &str = "Features: ";
#[derive(Debug)]
#[non_exhaustive]
pub enum CalcResult {
Null,
Bool(bool),
Version(Version),
}
pub trait Calculable: Debug {
fn get_value(&self, features: &HashMap<String, Version>) -> Result<CalcResult, ParseError>;
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Mode {
List,
Single(Box<dyn Calculable + 'static>),
Simple(Box<dyn Calculable + 'static>),
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Config {
pub option_name: String,
pub prefix: String,
pub program: String,
pub mode: Mode,
}
impl Default for Config {
#[inline]
fn default() -> Self {
Self {
option_name: DEFAULT_OPTION_NAME.to_owned(),
prefix: DEFAULT_PREFIX.to_owned(),
program: "".to_owned(),
mode: Mode::List,
}
}
}
#[allow(clippy::missing_const_for_fn)]
impl Config {
#[inline]
#[must_use]
pub fn with_option_name(self, option_name: String) -> Self {
Self {
option_name,
..self
}
}
#[inline]
#[must_use]
pub fn with_prefix(self, prefix: String) -> Self {
Self { prefix, ..self }
}
#[inline]
#[must_use]
pub fn with_program(self, program: String) -> Self {
Self { program, ..self }
}
#[inline]
#[must_use]
pub fn with_mode(self, mode: Mode) -> Self {
Self { mode, ..self }
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ParseError {
#[error("Cannot compare {0} to {1}")]
CannotCompare(String, String),
#[error("Invalid comparison operator '{0}'")]
InvalidComparisonOperator(String),
#[error("Don't know how to compare {0} to anything, including {1}")]
Uncomparable(String, String),
#[error("Could not parse '{0}' as a valid expression")]
ParseFailure(String, #[source] AnyError),
#[error("Could not parse '{0}' as a valid expression: {1} bytes left over")]
ParseLeftovers(String, usize),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ObtainError {
#[error("Could not decode the {0} program's output as valid UTF-8")]
DecodeOutput(String, #[source] AnyError),
#[error("Could not execute the {0} program")]
RunProgram(String, #[source] AnyError),
#[error("Parse error")]
Parse(#[source] ParseError),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Obtained {
Failed(ObtainError),
NotSupported,
Features(HashMap<String, Version>),
}