aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/quarrel/native/src/lenses/calculator.rs
blob: 640bdeb47b3ca276ee93a25d4ce8ec0d10c11e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// use meval::eval_str;
use cpc::{
    eval,
    units::Unit,
};
use mlua::prelude::*;

use crate::lenses::{
    Entry,
    Cache,
    Lense
};

pub struct Calculator;

impl Lense for Calculator {
    const NAME: &str = "Calculator";

    fn get_cache(&self) -> &Cache {
        &Cache::Stale
    }

    fn set_cache(&mut self, _: Cache) {}

    fn query(_: &Lua, input: String) -> Result<Vec<Entry>, anyhow::Error> {
        let result = match eval(input.trim(), true, Unit::Celsius, false) {
            Ok(result) => {
                format!("{result}")
            }
            Err(err) => { return Err(anyhow::anyhow!(err)); },
        };

        Ok(vec![Entry {
                message: result,
                exec: None,
            }; 1]
        )
    }
}