macro_rules! define_schema {
($schema_type:ident, $key_type:ty, $value_type:ty, $cf_name:expr) => { ... };
}
Expand description
A utility macro to define Schema
implementors. You must specify the
Schema
implementor’s name, the key type, the value type, and the column
family name.
§Example
use anyhow::Context;
use sov_schema_db::define_schema;
use sov_schema_db::schema::{
Schema, KeyEncoder, KeyDecoder, ValueCodec, Result,
};
define_schema!(PersonAgeByName, String, u32, "person_age_by_name");
impl KeyEncoder<PersonAgeByName> for String {
fn encode_key(&self) -> Result<Vec<u8>> {
Ok(self.as_bytes().to_vec())
}
}
impl KeyDecoder<PersonAgeByName> for String {
fn decode_key(data: &[u8]) -> Result<Self> {
Ok(String::from_utf8(data.to_vec()).context("Can't read key")?)
}
}
impl ValueCodec<PersonAgeByName> for u32 {
fn encode_value(&self) -> Result<Vec<u8>> {
Ok(self.to_le_bytes().to_vec())
}
fn decode_value(data: &[u8]) -> Result<Self> {
let mut buf = [0u8; 4];
buf.copy_from_slice(data);
Ok(u32::from_le_bytes(buf))
}
}