From fe3dcfc49fd7ba9ece41307cefccc77c4120eaf0 Mon Sep 17 00:00:00 2001 From: rise0chen Date: Tue, 21 Nov 2023 07:05:55 +0000 Subject: [PATCH] feat: storage --- src/lib.rs | 16 ++++++++++++++++ src/rpc/mod.rs | 1 + src/rpc/storage.rs | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/rpc/storage.rs diff --git a/src/lib.rs b/src/lib.rs index 0875e4c..595002d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -254,6 +254,22 @@ pub mod lebai_sdk { self.0.add_signal(index, value).await } + // Storage + #[classmethod] + pub async fn set_item(&self, key: String, value: String) -> Result<()> { + self.0.set_item(key, value).await + } + #[classmethod] + #[cmod::tags(ret)] + pub async fn get_item(&self, key: String) -> Result { + self.0.get_item(key).await + } + #[classmethod] + #[cmod::tags(ret)] + pub async fn get_items(&self, prefix: String) -> Result> { + self.0.get_items(prefix).await + } + //TASK #[classmethod] #[cmod::tags(args(params))] diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index aadece9..8ed44ae 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -8,6 +8,7 @@ pub mod motion; pub mod posture; pub mod serial; pub mod signal; +pub mod storage; pub mod system; pub mod task; diff --git a/src/rpc/storage.rs b/src/rpc/storage.rs new file mode 100644 index 0000000..35e0c91 --- /dev/null +++ b/src/rpc/storage.rs @@ -0,0 +1,21 @@ +use super::Robot; +use cmod::Result; +use proto::lebai::storage::*; + +impl Robot { + pub(crate) async fn set_item(&self, key: String, value: String) -> Result<()> { + let req = Item { key, value }; + let _ = self.c.set_item(Some(req)).await.map_err(|e| e.to_string())?; + Ok(()) + } + pub(crate) async fn get_item(&self, key: String) -> Result { + let req = ItemIndex { key }; + let resp = self.c.get_item(Some(req)).await.map_err(|e| e.to_string())?; + Ok(resp) + } + pub(crate) async fn get_items(&self, prefix: String) -> Result> { + let req = GetItemsRequest { prefix }; + let resp = self.c.get_items(Some(req)).await.map_err(|e| e.to_string())?; + Ok(resp.items) + } +}