litcheck_filecheck/expr/
cli.rs1use std::str::FromStr;
2
3use litcheck::variables::TypedVariable;
4
5use crate::common::*;
6
7use super::{Value, parser::NumericVarParser};
8
9#[derive(Debug, Clone)]
10pub struct CliVariable(super::Variable<'static>);
11
12impl CliVariable {
13 #[inline(always)]
14 pub const fn name(&self) -> VariableName {
15 self.0.name
16 }
17
18 #[inline(always)]
19 pub const fn value(&self) -> &Value<'static> {
20 &self.0.value
21 }
22}
23
24impl FromStr for CliVariable {
25 type Err = String;
26
27 fn from_str(input: &str) -> Result<Self, Self::Err> {
28 let span = SourceSpan::from_range_unchecked(SourceId::UNKNOWN, 0..input.len());
29 <CliVariable as TypedVariable>::try_parse(Span::new(span, input))
30 .map_err(|err| litcheck::reporting::PrintDiagnostic::new(err).to_string())
31 }
32}
33
34impl TypedVariable for CliVariable {
35 type Key<'a> = Symbol;
36 type Value<'a> = Value<'static>;
37 type Variable<'a> = CliVariable;
38
39 fn try_parse<'input>(input: Span<&'input str>) -> Result<Self::Variable<'input>, Report> {
40 if input.starts_with('#') {
41 let mut parser = NumericVarParser;
42 let var = parser.parse(input)?;
43 let name = var.name;
44 Ok(Self(match var.value {
45 Value::Undef => super::Variable::new(name, Value::Undef),
46 Value::Num(n) => super::Variable::new(name, Value::Num(n)),
47 Value::Str(s) => super::Variable::new(name, Value::Str(Cow::Owned(s.into_owned()))),
48 }))
49 } else {
50 super::Variable::<'static>::try_parse(input).map(Self)
51 }
52 }
53}