Skip to content

Commit

Permalink
feat: added a simple app with nested map and set
Browse files Browse the repository at this point in the history
  • Loading branch information
chefsale committed Nov 4, 2024
1 parent a687c2a commit d5516fb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"./apps/kv-store",
"./apps/only-peers",
"./apps/gen-ext",
"./apps/visited",

"./contracts/context-config",
"./contracts/registry",
Expand Down
14 changes: 14 additions & 0 deletions apps/visited/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "visited"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[lib]
crate-type = ["cdylib"]

[dependencies]
calimero-sdk = { path = "../../crates/sdk" }
calimero-storage = { path = "../../crates/storage" }
18 changes: 18 additions & 0 deletions apps/visited/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -e

cd "$(dirname $0)"

TARGET="${CARGO_TARGET_DIR:-../../target}"

rustup target add wasm32-unknown-unknown

cargo build --target wasm32-unknown-unknown --profile app-release

mkdir -p res

cp $TARGET/wasm32-unknown-unknown/app-release/visited.wasm ./res/

if command -v wasm-opt > /dev/null; then
wasm-opt -Oz ./res/visited.wasm -o ./res/visited.wasm
fi
52 changes: 52 additions & 0 deletions apps/visited/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![allow(clippy::len_without_is_empty)]

use std::result::Result;

use calimero_sdk::app;
use calimero_sdk::types::Error;
use calimero_storage::collections::{UnorderedMap, UnorderedSet};
use calimero_storage::entities::Element;
use calimero_storage::AtomicUnit;

#[app::state]
#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)]
#[root]
#[type_id(1)]
pub struct VisitedCities {
visited: UnorderedMap<String, UnorderedSet<String>>,
#[storage]
storage: Element,
}

#[app::logic]
impl VisitedCities {
#[app::init]
pub fn init() -> VisitedCities {
VisitedCities {
visited: UnorderedMap::new().unwrap(),
storage: Element::root(),
}
}

pub fn add_person(&mut self, person: String) -> Result<bool, Error> {
Ok(self.visited.insert(person, UnorderedSet::new().unwrap())?)
}

pub fn add_visited_city(&mut self, person: String, city: String) -> Result<bool, Error> {
Ok(self.visited.get(&person)?.unwrap().insert(city)?)
}

pub fn get_person_with_most_cities_visited(&self) -> Result<String, Error> {
let mut max = 0;
let mut person = String::new();

for entry in self.visited.entries()? {
let (person_key, cities_set) = entry;
if cities_set.len()? > max {
max = cities_set.len()?;
person = person_key.clone();
}
}
Ok(person)
}
}

0 comments on commit d5516fb

Please sign in to comment.