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