-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a simple app with nested map and set
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |