pub mod command;
pub mod error;
pub mod protocol;
use std::{future::Future, marker::PhantomData};
use crux_core::{Command, Request, command::RequestBuilder};
pub use error::*;
pub use protocol::*;
pub struct KeyValue<Effect, Event> {
effect: PhantomData<Effect>,
event: PhantomData<Event>,
}
impl<Effect, Event> KeyValue<Effect, Event>
where
Effect: Send + From<Request<KeyValueOperation>> + 'static,
Event: Send + 'static,
{
pub fn get(
key: impl Into<String>,
) -> RequestBuilder<Effect, Event, impl Future<Output = DataResult>> {
Command::request_from_shell(KeyValueOperation::Get { key: key.into() })
.map(KeyValueResult::unwrap_get)
}
pub fn set(
key: impl Into<String>,
value: Vec<u8>,
) -> RequestBuilder<Effect, Event, impl Future<Output = DataResult>> {
Command::request_from_shell(KeyValueOperation::Set {
key: key.into(),
value,
})
.map(KeyValueResult::unwrap_set)
}
pub fn delete(
key: impl Into<String>,
) -> RequestBuilder<Effect, Event, impl Future<Output = DataResult>> {
Command::request_from_shell(KeyValueOperation::Delete { key: key.into() })
.map(KeyValueResult::unwrap_delete)
}
pub fn exists(
key: impl Into<String>,
) -> RequestBuilder<Effect, Event, impl Future<Output = StatusResult>> {
Command::request_from_shell(KeyValueOperation::Exists { key: key.into() })
.map(KeyValueResult::unwrap_exists)
}
pub fn list_keys(
prefix: impl Into<String>,
cursor: u64,
) -> RequestBuilder<Effect, Event, impl Future<Output = ListResult>> {
Command::request_from_shell(KeyValueOperation::ListKeys {
prefix: prefix.into(),
cursor,
})
.map(KeyValueResult::unwrap_list_keys)
}
}
#[cfg(test)]
mod tests;