nyandere 0.1.2

i help with keeping track of purchases. meow
Documentation
use super::prelude::*;

/// Evaluates how much `between.source` owes `between.target`.
#[apply(cmd_args!)]
pub struct Balance {
    #[named]
    pub between: Dir,
}

impl Command for Balance {
    type Args = Self;
}

impl Run for Balance {
    fn run(self: Box<Self>, ctx: &mut State) -> Output {
        // make sure that this is just a query
        let ctx = &ctx;

        let Self { between } = *self;
        // TODO: not sure if this resolve takes the direction that is queried by.
        // it *should*, but whether it does is another question
        let bal: ext::business::Balance = between.clone().resolve(ctx).expect("infallible");

        Some(Box::new(bal))
    }
}

#[cfg(test)]
mod tests {
    use insta::{assert_snapshot, with_settings};

    use crate::{Runtime, Script, one};

    const SETUP: &str = "
        create entity A
        create entity B

        pay 1€ from=A to=B
        pay 5€ from=B to=A
        pay 1€ from=A to=B
    ";

    fn rt() -> Runtime {
        let mut rt = Runtime::new();
        rt.run(Script::parse(SETUP).unwrap()).unwrap();

        rt
    }

    #[test]
    fn forward() {
        let mut rt = rt();
        with_settings!({ description => SETUP }, {
            assert_snapshot!(
                one(&mut rt, "balance from=A to=B"),
                @"entity A owes 3.00 € to entity B",
            );
        });
    }

    #[test]
    fn backward() {
        let mut rt = rt();
        with_settings!({ description => SETUP }, {
            assert_snapshot!(
                one(&mut rt, "balance from=B to=A"),
                @"entity B is owed 3.00 € by entity A",
            );
        });
    }

    #[test]
    fn even() {
        let mut rt = rt();
        let extra = "pay 3€ from=A to=B";
        one(&mut rt, extra);

        with_settings!({ description => SETUP.to_string() + extra }, {
            assert_snapshot!(
                one(&mut rt, "balance from=A to=B"),
                @"entity A is even with entity B",
            );
        });
    }
}