osp-cli 1.5.1

CLI and REPL for querying and managing OSP infrastructure data
Documentation
use crate::core::{output_model::compute_key_index, row::Row};

#[derive(Debug, Clone, Default)]
pub struct RowContext {
    key_index: Vec<String>,
}

impl RowContext {
    /// Builds row-level context from the key ordering observed across `rows`.
    pub fn from_rows(rows: &[Row]) -> Self {
        Self {
            key_index: compute_key_index(rows),
        }
    }

    /// Returns the cached key index in first-seen order.
    pub fn key_index(&self) -> &[String] {
        &self.key_index
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::RowContext;

    #[test]
    fn keeps_first_seen_key_order() {
        let rows = vec![
            json!({"uid": "oistes", "cn": "Oistein"})
                .as_object()
                .cloned()
                .expect("object"),
            json!({"mail": "o@uio.no", "uid": "oistes"})
                .as_object()
                .cloned()
                .expect("object"),
        ];

        let context = RowContext::from_rows(&rows);
        assert_eq!(context.key_index(), &["uid", "cn", "mail"]);
    }
}