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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
//use std::rc::Rc;

use crate::commands::execution::{ValueExecution, ValueResult};
use crate::context::PicoContext;
use crate::errors::PicoError;
//use crate::state::PicoState;
//use super::namespace;
use crate::rules::PicoRules;
use crate::runtime::PicoRuntime;
use crate::PicoValue;

pub type LookupDict = HashMap<String, PicoValue>;

#[derive(Serialize, Deserialize, Debug)]
pub struct LookupTable {
    pub entries: LookupDict,
    pub default: PicoValue,

    // namespaces this lookup table is available in
    pub namespaces: Option<Vec<String>>,
}

impl Default for LookupTable {
    fn default() -> Self {
        Self {
            default: PicoValue::String("unknown".to_string()),
            entries: HashMap::new(),
            namespaces: None,
        }
    }
}

impl LookupTable {
    pub fn new() -> LookupTable {
        Default::default()
    }

    pub fn lookup(&self, key: &str) -> &PicoValue {
        if let Some(value) = self.entries.get(key) {
            value
        } else {
            &self.default
        }
    }
}

//pub type Lookups = HashMap<String, Rc<LookupTable>>;
pub type Lookups = HashMap<String, LookupTable>;

#[derive(Serialize, Deserialize, Debug)]
pub struct LookupCommand {
    lookup: (String, String), // table, key
}

impl ValueExecution for LookupCommand {
    fn run_with_context(
        &self,
        pico_rules: &PicoRules,
        runtime: &mut PicoRuntime,
        _ctx: &mut PicoContext,
    ) -> ValueResult {
        info!(
            "Lookup Dictionary {:?} -> {:?}",
            self.lookup.0, self.lookup.1
        );

        /*
        FIXME
        if let Some(v) = state.get_lookup_value(&self.lookup.0, &self.lookup.1) {
            match v {
                PicoValue::String(s) => {
                    return Ok(ExecutionResult::Continue(PicoValue::String(s.to_string())))
                }
                _ => {
                    return Err(PicoError::NoSuchValue(format!(
                        "{}/{}",
                        &self.lookup.0, &self.lookup.1
                    )))
                }
            }
        }
        */

        info!("Lookup failed for {:?}", self.lookup.0);

        Err(PicoError::NoSuchValue(format!(
            "No Such table {}",
            &self.lookup.0
        )))
    }
}