osp_cli/dsl/eval/
context.rs1use crate::core::{output_model::compute_key_index, row::Row};
2
3#[derive(Debug, Clone, Default)]
4pub struct RowContext {
5 key_index: Vec<String>,
6}
7
8impl RowContext {
9 pub fn from_rows(rows: &[Row]) -> Self {
10 Self {
11 key_index: compute_key_index(rows),
12 }
13 }
14
15 pub fn key_index(&self) -> &[String] {
16 &self.key_index
17 }
18}
19
20#[cfg(test)]
21mod tests {
22 use serde_json::json;
23
24 use super::RowContext;
25
26 #[test]
27 fn keeps_first_seen_key_order() {
28 let rows = vec![
29 json!({"uid": "oistes", "cn": "Oistein"})
30 .as_object()
31 .cloned()
32 .expect("object"),
33 json!({"mail": "o@uio.no", "uid": "oistes"})
34 .as_object()
35 .cloned()
36 .expect("object"),
37 ];
38
39 let context = RowContext::from_rows(&rows);
40 assert_eq!(context.key_index(), &["uid", "cn", "mail"]);
41 }
42}