Moved Query into separate actions.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s

This commit is contained in:
2026-02-06 12:06:51 -05:00
parent 7f7e10a373
commit 5ffb79b7f2
12 changed files with 164 additions and 105 deletions

81
src/action/query.rs Normal file
View File

@@ -0,0 +1,81 @@
use crate::{message::wrapper::Calculation, name::NameType};
use std::collections::{HashMap, HashSet};
#[derive(Clone, Debug)]
pub struct Query {
data: HashMap<NameType, Calculation>,
}
impl Query {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn add<NT>(&mut self, name: NT, operation: Calculation)
where
NT: Into<NameType>,
{
self.data.insert(name.into(), operation);
}
#[allow(dead_code)]
fn get<NT>(&self, name: NT) -> Option<Calculation>
where
NT: Into<NameType>,
{
match self.data.get(&name.into()) {
Some(calc) => Some(calc.clone()),
None => None,
}
}
#[allow(dead_code)]
fn field_ids(&self) -> HashSet<&NameType> {
self.data.keys().collect()
}
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &Calculation)> {
self.data.iter()
}
}
#[cfg(test)]
mod queries {
use super::*;
use crate::{document::field::Field, message::wrapper::Operand, name::Name};
use uuid::Uuid;
#[test]
fn holds_calculation_to_run_query() {
let name = Name::english(Uuid::new_v4().to_string().as_str());
let data = Uuid::new_v4();
let mut bad_data = data.clone();
while bad_data == data {
bad_data = Uuid::new_v4();
}
let mut query = Query::new();
let mut calc = Calculation::new(Operand::Equal);
calc.add_value(data.clone()).unwrap();
query.add(name.clone(), calc);
match query.get(&name) {
Some(op) => {
let expected: Field = true.into();
let mut holder = op.clone();
holder.add_value(data).unwrap();
assert_eq!(holder.calculate(&Field::None), expected);
}
None => unreachable!("should have returned a calculation"),
}
match query.get(&name) {
Some(op) => {
let expected: Field = false.into();
let mut holder = op.clone();
holder.add_value(bad_data).unwrap();
assert_eq!(holder.calculate(&Field::None), expected);
}
None => unreachable!("should have returned a calculation"),
}
}
}