From: Nicolas Luck Date: Thu, 27 Jul 2023 10:06:03 +0000 (+0200) Subject: HashSet -> BTreeSet: Make parsing or results and thus tests deterministic. Add comments. X-Git-Tag: remove^2~49 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=30dac8ea4186f915cd5c8eca06eb703bda31f027;p=scryer-prolog.git HashSet -> BTreeSet: Make parsing or results and thus tests deterministic. Add comments. --- diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index 1eec92b8..b75f9b6d 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::BTreeSet; use super::{ Machine, MachineConfig, QueryResult, QueryResolutionLine, @@ -36,19 +36,24 @@ impl Machine { Err(output) } else { // Remove duplicate lines - let output = output - .lines() - .collect::>() + Ok(output + // 1. Split into disjunct matches + .split(";") + .map(|s| s.trim()) + // 2. Dedupe through Set + .collect::>() .iter() .cloned() + // 3. Back to Vec .collect::>() - .join("\n"); - Ok(output - .split(";") + .iter() + // 4. Trim and remove empty lines .map(|s| s.trim()) .map(|s| s.replace(".", "")) .filter(|s| !s.is_empty()) + // 5. Parse into QueryResolutionLine .map(QueryResolutionLine::try_from) + // 6. Remove lines that couldn't be parsed, so we still keep the ones they did .filter_map(Result::ok) .collect::>() .into()) @@ -154,10 +159,10 @@ mod tests { result, Ok(QueryResolution::Matches(vec![ QueryMatch::from(btreemap! { - "Class" => Value::from("Todo") + "Class" => Value::from("Recipe") }), QueryMatch::from(btreemap! { - "Class" => Value::from("Recipe") + "Class" => Value::from("Todo") }), ])) );