Skip to main content

vantage_dataset/im/
valueset_readable.rs

1use async_trait::async_trait;
2use indexmap::IndexMap;
3use vantage_types::{Entity, Record};
4
5use crate::{im::ImTable, traits::ReadableValueSet};
6
7#[async_trait]
8impl<E> ReadableValueSet for ImTable<E>
9where
10    E: Entity,
11{
12    async fn list_values(&self) -> crate::traits::Result<IndexMap<Self::Id, Record<Self::Value>>> {
13        let table = self.data_source.get_or_create_table(&self.table_name);
14        Ok(table)
15    }
16
17    async fn get_value(&self, id: &Self::Id) -> crate::traits::Result<Option<Record<Self::Value>>> {
18        let table = self.data_source.get_or_create_table(&self.table_name);
19        Ok(table.get(id).cloned())
20    }
21
22    async fn get_some_value(
23        &self,
24    ) -> crate::traits::Result<Option<(Self::Id, Record<Self::Value>)>> {
25        let table = self.data_source.get_or_create_table(&self.table_name);
26
27        if let Some((id, record)) = table.iter().next() {
28            Ok(Some((id.clone(), record.clone())))
29        } else {
30            Ok(None)
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::im::ImDataSource;
39    use serde::{Deserialize, Serialize};
40    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
41    struct User {
42        id: Option<String>,
43        name: String,
44    }
45
46    #[tokio::test]
47    async fn test_list_values() {
48        let ds = ImDataSource::new();
49        let table = ImTable::<User>::new(&ds, "users");
50
51        let result = table.list_values().await.unwrap();
52        assert_eq!(result.len(), 0);
53    }
54
55    #[tokio::test]
56    async fn test_get_value() {
57        let ds = ImDataSource::new();
58        let table = ImTable::<User>::new(&ds, "users");
59
60        let result = table.get_value(&"nonexistent".to_string()).await.unwrap();
61        assert!(result.is_none());
62    }
63
64    #[tokio::test]
65    async fn test_get_some_value() {
66        let ds = ImDataSource::new();
67        let table = ImTable::<User>::new(&ds, "users");
68
69        let result = table.get_some_value().await.unwrap();
70        assert!(result.is_none());
71    }
72}