Skip to content

Container implementation docs #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs-test-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
"ibc-channel" => include_str!("../templates/ibc-channel.tpl"),
"ibc-packet" => include_str!("../templates/ibc-packet.tpl"),
"storage" => include_str!("../templates/storage.tpl"),
"storey-container-impl" => include_str!("../templates/storey-container-impl.tpl"),
"storey-container-impl-iter" => include_str!("../templates/storey-container-impl-iter.tpl"),
"sylvia-storey-contract" => include_str!("../templates/sylvia/storey_contract.tpl"),
"sylvia-cw-storage-contract" => include_str!("../templates/sylvia/cw_storage_contract.tpl"),
"sylvia-empty" => include_str!("../templates/sylvia/empty.tpl"),
Expand Down
225 changes: 225 additions & 0 deletions docs-test-gen/templates/storey-container-impl-iter.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#![allow(
unexpected_cfgs,
dead_code,
unused_variables,
unused_imports,
clippy::new_without_default,
)]
use cosmwasm_schema::cw_serde;
use cosmwasm_std::*;
use storey::containers::{IterableStorable, NonTerminal, Storable};
use storey::storage::{IntoStorage, StorageBranch};

mod users {
use super::*;

use cw_storage_plus::{index_list, IndexedMap, MultiIndex, UniqueIndex};

pub type Handle = String;

#[cw_serde]
pub struct User {
pub handle: String,
pub country: String,
}

pub struct ExampleUsers {
pub alice: User,
pub bob: User,
}

pub fn example_users() -> ExampleUsers {
ExampleUsers {
alice: User {
handle: "alice".to_string(),
country: "Wonderland".to_string(),
},
bob: User {
handle: "bob".to_string(),
country: "USA".to_string(),
},
}
}

#[index_list(User)]
pub struct UserIndexes<'a> {
pub handle_ix: UniqueIndex<'a, Handle, User, Addr>,
pub country_ix: MultiIndex<'a, String, User, Addr>,
}

pub fn user_indexes() -> UserIndexes<'static> {
user_indexes_custom("u", "uh", "uc")
}

pub fn user_indexes_custom(
ns: &'static str,
handle_prefix: &'static str,
country_prefix: &'static str,
) -> UserIndexes<'static> {
UserIndexes {
handle_ix: UniqueIndex::new(|user| user.handle.clone(), handle_prefix),
country_ix: MultiIndex::new(|_pk, user| user.country.clone(), ns, country_prefix),
}
}
}

fn advance_height(env: &mut Env, blocks: u64) {
env.block.height += blocks;
}

pub struct MyMap<V> {
prefix: u8,
phantom: std::marker::PhantomData<V>,
}

impl<V> MyMap<V>
where
V: Storable,
{
pub const fn new(prefix: u8) -> Self {
Self {
prefix,
phantom: std::marker::PhantomData,
}
}

pub fn access<F, S>(&self, storage: F) -> MyMapAccess<V, StorageBranch<S>>
where
(F,): IntoStorage<S>,
{
let storage = (storage,).into_storage();
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
}
}

pub struct MyMapAccess<V, S> {
storage: S,
phantom: std::marker::PhantomData<V>,
}

impl<V> Storable for MyMap<V>
where
V: Storable,
{
type Kind = NonTerminal;
type Accessor<S> = MyMapAccess<V, S>;

fn access_impl<S>(storage: S) -> MyMapAccess<V, S> {
MyMapAccess {
storage,
phantom: std::marker::PhantomData,
}
}
}

impl<V, S> MyMapAccess<V, S>
where
V: Storable,
{
pub fn entry(&self, key: u32) -> V::Accessor<StorageBranch<&S>> {
let key = key.to_be_bytes().to_vec();

V::access_impl(StorageBranch::new(&self.storage, key))
}

pub fn entry_mut(&mut self, key: u32) -> V::Accessor<StorageBranch<&mut S>> {
let key = key.to_be_bytes().to_vec();

V::access_impl(StorageBranch::new(&mut self.storage, key))
}
}

use storey::containers::IterableAccessor;
use storey::storage::IterableStorage;

impl<V> IterableStorable for MyMap<V>
where
V: IterableStorable,
<V as IterableStorable>::KeyDecodeError: std::fmt::Display,
{
type Key = (u32, V::Key);
type KeyDecodeError = String;
type Value = V::Value;
type ValueDecodeError = V::ValueDecodeError;

fn decode_key(key: &[u8]) -> Result<Self::Key, String> {
if key.len() < 4 {
return Err(String::from("Key too short"));
}

let key_arr = key[0..4].try_into().map_err(|e| format!("Invalid key: {}", e))?;
let this_key = u32::from_be_bytes(key_arr);

let rest = V::decode_key(&key[4..]).map_err(|e| e.to_string())?;

Ok((this_key, rest))
}

fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError> {
V::decode_value(value)
}
}

impl<V, S> IterableAccessor for MyMapAccess<V, S>
where
V: IterableStorable,
S: IterableStorage,
{
type Storable = MyMap<V>;
type Storage = S;

fn storage(&self) -> &Self::Storage {
&self.storage
}
}

#[test]
fn doctest() {
#[allow(unused_variables, unused_mut)]
let mut storage = cosmwasm_std::testing::MockStorage::new();
#[allow(unused_mut)]
let mut env = cosmwasm_std::testing::mock_env();

let users = cw_storage_plus::IndexedMap::<Addr, _, _>::new(
"uu",
users::user_indexes_custom("uu", "uuh", "uuc"),
);

let users_data = [
(
Addr::unchecked("aaa"),
users::User {
handle: "alice".to_string(),
country: "Wonderland".to_string(),
},
),
(
Addr::unchecked("bbb"),
users::User {
handle: "bob".to_string(),
country: "USA".to_string(),
},
),
(
Addr::unchecked("ccc"),
users::User {
handle: "carol".to_string(),
country: "UK".to_string(),
},
),
(
Addr::unchecked("ddd"),
users::User {
handle: "dave".to_string(),
country: "USA".to_string(),
},
),
];

for (addr, user) in users_data {
users.save(&mut storage, addr, &user).unwrap();
}

#[rustfmt::skip]
{{code}}
}
Loading
Loading