generated from gear-foundation/dapp-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(io): meta abstraction * chore(dapp): make everything compiles * chore(dapp): program test for uploading source * feat(io): tests for state reading * feat(prog): introduce method search * feat(io): abstract domain interface * chore(tests): use the debug version of meta.wasm * feat(state): list labels * chore(io): remove unused HandleOutput * chore(dep): use gear toolkit at v1.1.1 * chore(clippy): make clippy happy * feat(io): add plans for identity * chore(io): make clippy happy
- Loading branch information
Showing
9 changed files
with
942 additions
and
804 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,11 @@ | ||
use crate::Source; | ||
use gstd::{Decode, Encode, String, TypeInfo}; | ||
|
||
/// The input for the `handle` entry point. | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct HandleInput { | ||
pub domain: String, | ||
pub src: Source, | ||
} |
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,82 @@ | ||
use gstd::{collections::BTreeMap, ActorId, Decode, Encode, String, TypeInfo, Vec}; | ||
|
||
/// Profile of the content | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Default, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct Profile { | ||
pub title: String, | ||
pub links: BTreeMap<String, String>, | ||
} | ||
|
||
/// The header of the page. | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Default, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct Header { | ||
/// Title of the page. | ||
pub title: String, | ||
/// Logo of the page. | ||
pub logo: Option<String>, | ||
} | ||
|
||
/// Source of the content | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub enum Content { | ||
// TODO: use demo-identity instead. | ||
Profile(Profile), | ||
Markdown(String), | ||
} | ||
|
||
impl Content { | ||
/// If the content contains the token. | ||
pub fn contains(&self, token: &str) -> bool { | ||
match self { | ||
Content::Profile(profile) => profile.title.contains(token), | ||
Content::Markdown(markdown) => markdown.contains(token), | ||
} | ||
} | ||
} | ||
|
||
/// Footer abstraction. | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Default, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct Footer { | ||
/// Centered information in footer. | ||
pub info: String, | ||
} | ||
|
||
/// Source of the page. | ||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct Source { | ||
pub labels: Vec<String>, | ||
pub header: Header, | ||
pub content: Content, | ||
pub footer: Footer, | ||
} | ||
|
||
/// Domain of pages. | ||
/// | ||
/// TODO: | ||
/// | ||
/// 1) access control for the domain. | ||
/// 2) enable this interface in the next version. | ||
#[allow(unused)] | ||
pub struct Domain { | ||
/// add a new field for the owner struct. | ||
/// | ||
/// - owner: ActorId | ||
/// - identity: programId. | ||
pub owner: ActorId, | ||
pub paths: BTreeMap<String, Source>, | ||
/// people who has edit access to the domain source. | ||
pub editors: Vec<ActorId>, | ||
} | ||
|
||
/// Program state. | ||
pub type State = BTreeMap<String, Source>; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
edition = "2021" | ||
use_field_init_shorthand = true | ||
newline_style = "Unix" | ||
force_explicit_abi = false |
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 |
---|---|---|
@@ -1,28 +1,55 @@ | ||
#![no_std] | ||
|
||
use gstd::{prelude::*, ActorId}; | ||
use gstd::{collections::BTreeMap, prelude::*, Vec}; | ||
use template_io::*; | ||
|
||
#[gmeta::metawasm] | ||
pub mod metafns { | ||
pub type State = template_io::State; | ||
|
||
pub fn query(state: State, query: StateQuery) -> StateQueryReply { | ||
match query { | ||
StateQuery::Pingers => StateQueryReply::Pingers(pingers(state)), | ||
StateQuery::PingCount(actor) => StateQueryReply::PingCount(ping_count(state, actor)), | ||
} | ||
} | ||
pub type State = template_io::State; | ||
|
||
pub fn pingers(state: State) -> Vec<ActorId> { | ||
state.iter().map(|(pinger, _)| *pinger).collect() | ||
} | ||
/// Returns all domains (pages) that matches the search input. | ||
/// | ||
/// For the source of the competition: | ||
/// - domain name | ||
/// - labels | ||
/// - header title | ||
/// - content | ||
pub fn search(state: State, input: String) -> BTreeMap<String, Source> { | ||
let tokens: Vec<&str> = input.split_whitespace().collect(); | ||
|
||
pub fn ping_count(state: State, actor: ActorId) -> u128 { | ||
state | ||
.iter() | ||
.find_map(|(some_actor, count)| (some_actor == &actor).then_some(count)) | ||
.copied() | ||
.unwrap_or_default() | ||
.filter_map(|(domain, source)| { | ||
if tokens.iter().any(|t| { | ||
domain.contains(t) | ||
|| source.labels.iter().any(|l| l.contains(t)) | ||
|| source.header.title.contains(t) | ||
|| source.content.contains(t) | ||
}) { | ||
return Some((domain.clone(), source.clone())); | ||
} | ||
None | ||
}) | ||
.collect() | ||
} | ||
|
||
/// List all labels | ||
pub fn labels(state: State) -> Vec<String> { | ||
let mut labels: Vec<String> = state | ||
.values() | ||
.map(|s| s.labels.clone()) | ||
.collect::<Vec<Vec<String>>>() | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
labels.sort(); | ||
labels.dedup(); | ||
labels | ||
} | ||
|
||
// TODO: | ||
// | ||
// 1) list sub paths of a domain. | ||
// 2) | ||
} |
Oops, something went wrong.