Skip to content

Commit

Permalink
Merge pull request #20 from CosmWasm/cw-storey
Browse files Browse the repository at this point in the history
Introduce `cw-storey`
  • Loading branch information
uint authored Mar 21, 2024
2 parents 485378a + 05eef5c commit 0603b5b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 16 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ authors = ["Tomasz Kurcz", "Confio GmbH"]
license = "Apache-2.0"

[workspace.dependencies]
thiserror = "1"

storey = { path = "crates/storey", version = "0.1.0" }
4 changes: 4 additions & 0 deletions crates/cw-storey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ edition = "2021"
license = { workspace = true }

[dependencies]
rmp-serde = "1"
cosmwasm-std = "2"
serde = "1"

storey = { workspace = true }
25 changes: 25 additions & 0 deletions crates/cw-storey/src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use storey::{StorageBackend, StorageBackendMut};

pub struct CwStorage<S>(pub S);

impl<S> StorageBackend for CwStorage<S>
where
S: cosmwasm_std::Storage,
{
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
cosmwasm_std::Storage::get(&self.0, key)
}
}

impl<S> StorageBackendMut for CwStorage<S>
where
S: cosmwasm_std::Storage,
{
fn set(&mut self, key: &[u8], value: &[u8]) {
cosmwasm_std::Storage::set(&mut self.0, key, value)
}

fn remove(&mut self, key: &[u8]) {
cosmwasm_std::Storage::remove(&mut self.0, key)
}
}
4 changes: 4 additions & 0 deletions crates/cw-storey/src/containers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub type Item<T> = storey::containers::Item<T, crate::encoding::CwEncoding>;
pub type Column<T> = storey::containers::Column<T, crate::encoding::CwEncoding>;

pub use storey::containers::Map;
26 changes: 26 additions & 0 deletions crates/cw-storey/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use storey::encoding::{Cover, DecodableWithImpl, EncodableWithImpl, Encoding};

pub struct CwEncoding;

impl Encoding for CwEncoding {
type DecodeError = ();
type EncodeError = ();
}

impl<T> EncodableWithImpl<CwEncoding> for Cover<&T>
where
T: serde::Serialize,
{
fn encode_impl(self) -> Result<Vec<u8>, ()> {
rmp_serde::to_vec(self.0).map_err(|_| ())
}
}

impl<T> DecodableWithImpl<CwEncoding> for Cover<T>
where
T: serde::de::DeserializeOwned,
{
fn decode_impl(data: &[u8]) -> Result<Self, ()> {
rmp_serde::from_slice(data).map(Cover).map_err(|_| ())
}
}
17 changes: 4 additions & 13 deletions crates/cw-storey/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
mod backend;
pub mod containers;
mod encoding;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub use backend::CwStorage;
18 changes: 18 additions & 0 deletions crates/cw-storey/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use cw_storey::{containers::Item, CwStorage};

use cosmwasm_std::Storage as _;

#[test]
fn smoke_test() {
let mut storage = CwStorage(cosmwasm_std::testing::MockStorage::new());

let item1 = Item::<u64>::new(&[0]);

item1.access(&mut storage).set(&42).unwrap();
assert_eq!(item1.access(&storage).get().unwrap(), Some(42));

let item2 = Item::<u64>::new(&[1]);
assert_eq!(item2.access(&storage).get().unwrap(), None);

assert_eq!(storage.0.get(&[0]), Some(vec![42]));
}
2 changes: 1 addition & 1 deletion crates/storey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ rust-version = "1.65" # https://caniuse.rs/features/generic_associated_types
license = { workspace = true }

[dependencies]
thiserror = { workspace = true }
thiserror = "1"

0 comments on commit 0603b5b

Please sign in to comment.