From a14afd656b4def8c59db6784e242871bf4ad447e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 22 Dec 2023 12:47:18 -0800 Subject: [PATCH] variable sets --- rust/src/raw.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rust/src/raw.rs b/rust/src/raw.rs index e0c5a79eff..ac2d1960ac 100644 --- a/rust/src/raw.rs +++ b/rust/src/raw.rs @@ -1839,6 +1839,57 @@ impl From for TextRecord { } } +#[derive(Clone, Debug)] +pub struct VariableSet { + pub name: String, + pub vars: Vec, +} + +impl VariableSet { + fn parse(input: &str) -> Result { + let (name, input) = input.split_once('=').ok_or(Error::TBD)?; + let vars = input.split_ascii_whitespace().map(String::from).collect(); + Ok(VariableSet { + name: name.into(), + vars, + }) + } +} + +#[derive(Clone, Debug)] +pub struct VariableSetRecord { + pub offsets: Range, + pub sets: Vec +} + +impl VariableSetRecord { + fn decode<'a>(source: &TextRecord, decoder: &Decoder) -> VariableSetRecord { + let mut sets = Vec::new(); + let input = decoder.decode(&source.text); + for line in input.lines() { + if let Some(set) = VariableSet::parse(line).warn_on_error(&decoder.warn) { + sets.push(set) + } + } + VariableSetRecord { offsets: source.offsets.clone(), sets } + } +} + +trait WarnOnError { + fn warn_on_error(self, warn: &F) -> Option; +} +impl WarnOnError for Result { + fn warn_on_error(self, warn: &F) -> Option { + match self { + Ok(result) => Some(result), + Err(error) => { + warn(error); + None + } + } + } +} + #[derive(Clone, Debug)] pub struct Extension { pub offsets: Range, -- 2.30.2