Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Dec 12, 2023
0 parents commit 773b873
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "stork"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cosmwasm-std = "1.5.0"
70 changes: 70 additions & 0 deletions src/backend/cosmwasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
impl<B> super::StorageBackend for B
where
B: cosmwasm_std::Storage,
{
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
<B as cosmwasm_std::Storage>::get(self, key)
}

fn set(&mut self, key: &[u8], value: &[u8]) {
<B as cosmwasm_std::Storage>::set(self, key, value)
}

fn remove(&mut self, key: &[u8]) {
<B as cosmwasm_std::Storage>::remove(self, key)
}
}

impl<B> super::StorageIterableBackend for B
where
B: cosmwasm_std::Storage,
{
type KeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a;
type ValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a;
type PairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a;

fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> {
self.range(start, end, cosmwasm_std::Order::Ascending)
}

fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> {
self.range_keys(start, end, cosmwasm_std::Order::Ascending)
}

fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> {
self.range_values(start, end, cosmwasm_std::Order::Ascending)
}
}

impl<B> super::StorageRevIterableBackend for B
where
B: cosmwasm_std::Storage,
{
type RevKeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a;
type RevValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a;
type RevPairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a;

fn rev_pairs<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevPairsIterator<'a> {
self.range(start, end, cosmwasm_std::Order::Descending)
}

fn rev_keys<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevKeysIterator<'a> {
self.range_keys(start, end, cosmwasm_std::Order::Descending)
}

fn rev_values<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevValuesIterator<'a> {
self.range_values(start, end, cosmwasm_std::Order::Descending)
}
}
55 changes: 55 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mod cosmwasm;

pub trait StorageBackend {
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
fn set(&mut self, key: &[u8], value: &[u8]);
fn remove(&mut self, key: &[u8]);

fn has(&self, key: &[u8]) -> bool {
self.get(key).is_some()
}
}

pub trait StorageIterableBackend: StorageBackend {
type KeysIterator<'a>: Iterator<Item = Vec<u8>>
where
Self: 'a;
type ValuesIterator<'a>: Iterator<Item = Vec<u8>>
where
Self: 'a;
type PairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)>
where
Self: 'a;

fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a>;
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a>;
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a>;
}

pub trait StorageRevIterableBackend: StorageBackend {
type RevKeysIterator<'a>: Iterator<Item = Vec<u8>>
where
Self: 'a;
type RevValuesIterator<'a>: Iterator<Item = Vec<u8>>
where
Self: 'a;
type RevPairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)>
where
Self: 'a;

fn rev_keys<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevKeysIterator<'a>;
fn rev_values<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevValuesIterator<'a>;
fn rev_pairs<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevPairsIterator<'a>;
}
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
mod backend;

#[cfg(test)]
mod tests {
use cosmwasm_std::testing::MockStorage;

use crate::backend::{
StorageBackend as _, StorageIterableBackend as _, StorageRevIterableBackend as _,
};

#[test]
fn foo() {
let mut storage = MockStorage::new();

storage.set(b"foo", b"bar");
storage.set(b"baz", b"bax");
assert_eq!(storage.get(b"foo").unwrap(), b"bar");

assert!(storage.has(b"foo"));
assert!(!storage.has(b"foobar"));

assert_eq!(
storage.keys(None, None).collect::<Vec<_>>(),
vec![b"baz", b"foo"],
);
assert_eq!(
storage.rev_keys(None, None).collect::<Vec<_>>(),
vec![b"foo", b"baz"],
);

storage.remove(b"foo");
assert!(!storage.has(b"foo"));
assert_eq!(storage.get(b"foo"), None);
}
}

0 comments on commit 773b873

Please sign in to comment.