Skip to content

Commit

Permalink
feat: support custom KV impls (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX authored Jan 17, 2025
1 parent 11e15ed commit 57f5cbc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
41 changes: 33 additions & 8 deletions balius-runtime/src/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::wit::balius::app::kv as wit;

pub use wit::{Host as CustomKv, KvError, Payload};

#[derive(Clone)]
pub enum Kv {
Mock,
Custom(Arc<Mutex<dyn wit::Host + Send + Sync>>),
}

#[async_trait::async_trait]
impl wit::Host for Kv {
async fn get_value(&mut self, key: String) -> Result<wit::Payload, wit::KvError> {
todo!()
async fn get_value(&mut self, key: String) -> Result<Payload, KvError> {
match self {
Self::Mock => todo!(),
Self::Custom(kv) => {
let mut lock = kv.lock().await;
lock.get_value(key).await
}
}
}

async fn set_value(&mut self, key: String, value: wit::Payload) -> Result<(), wit::KvError> {
println!("{}:{}", key, hex::encode(value));

Ok(())
async fn set_value(&mut self, key: String, value: Payload) -> Result<(), KvError> {
match self {
Self::Mock => {
println!("{}:{}", key, hex::encode(value));
Ok(())
}
Self::Custom(kv) => {
let mut lock = kv.lock().await;
lock.set_value(key, value).await
}
}
}

async fn list_values(&mut self, prefix: String) -> Result<Vec<String>, wit::KvError> {
todo!()
async fn list_values(&mut self, prefix: String) -> Result<Vec<String>, KvError> {
match self {
Self::Mock => todo!(),
Self::Custom(kv) => {
let mut lock = kv.lock().await;
lock.list_values(prefix).await
}
}
}
}
12 changes: 12 additions & 0 deletions balius-sdk/src/qol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum Error {
BadUtxo,
#[error("event mismatch, expected {0}")]
EventMismatch(String),
#[error("kv error: {0}")]
KV(wit::balius::app::kv::KvError),
#[error("ledger error: {0}")]
Ledger(wit::balius::app::ledger::LedgerError),
}
Expand All @@ -36,6 +38,10 @@ impl From<Error> for wit::HandleError {
code: 2,
message: "bad params".to_owned(),
},
Error::KV(err) => wit::HandleError {
code: 3,
message: err.to_string(),
},
Error::Ledger(err) => wit::HandleError {
code: 4,
message: err.to_string(),
Expand All @@ -58,6 +64,12 @@ impl From<serde_json::Error> for Error {
}
}

impl From<wit::balius::app::kv::KvError> for Error {
fn from(error: wit::balius::app::kv::KvError) -> Self {
Error::KV(error)
}
}

impl From<wit::balius::app::ledger::LedgerError> for Error {
fn from(error: wit::balius::app::ledger::LedgerError) -> Self {
Error::Ledger(error)
Expand Down
6 changes: 5 additions & 1 deletion wit/balius.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package balius:app@0.1.0;

interface kv {
type payload = list<u8>;
type kv-error = u32;
variant kv-error {
upstream(string),
internal(string),
not-found(string)
}

get-value: func(key: string) -> result<payload, kv-error>;
set-value: func(key: string, value: payload) -> result<_, kv-error>;
Expand Down

0 comments on commit 57f5cbc

Please sign in to comment.