forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] hyperledger-iroha#2553: Add sorting to asset queries
Signed-off-by: Vladimir Pesterev <pesterev@pm.me>
- Loading branch information
Showing
8 changed files
with
249 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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,68 @@ | ||
//! Structures and traits related to sorting. | ||
|
||
#[cfg(not(feature = "std"))] | ||
use alloc::{ | ||
borrow::ToOwned as _, | ||
collections::btree_map, | ||
format, | ||
string::{String, ToString as _}, | ||
vec, | ||
vec::Vec, | ||
}; | ||
#[cfg(feature = "std")] | ||
use std::collections::btree_map; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
#[cfg(feature = "warp")] | ||
use warp::{Filter, Rejection}; | ||
|
||
use crate::prelude::*; | ||
|
||
const SORT_BY_KEY: &str = "sort_by_key"; | ||
|
||
/// Structure for sorting requests | ||
#[derive(Debug, Clone, Default, Serialize, Deserialize)] | ||
pub struct Sorting { | ||
/// [`Name`] of the key in [`Asset`]'s metadata used to order query result. | ||
pub sort_by_key: Option<Name>, | ||
} | ||
|
||
impl Sorting { | ||
/// | ||
pub fn new(key: Name) -> Self { | ||
Self { | ||
sort_by_key: Some(key), | ||
} | ||
} | ||
} | ||
|
||
impl From<Sorting> for btree_map::BTreeMap<String, String> { | ||
fn from(sorting: Sorting) -> Self { | ||
let mut btree = Self::new(); | ||
if let Some(key) = sorting.sort_by_key { | ||
btree.insert(String::from(SORT_BY_KEY), key.to_string()); | ||
} | ||
btree | ||
} | ||
} | ||
|
||
impl From<Sorting> for Vec<(&'static str, String)> { | ||
fn from(sorting: Sorting) -> Self { | ||
let mut vec = Vec::new(); | ||
if let Some(key) = sorting.sort_by_key { | ||
vec.push((SORT_BY_KEY, key.to_string())); | ||
} | ||
vec | ||
} | ||
} | ||
|
||
#[cfg(feature = "warp")] | ||
/// Filter for warp which extracts sorting | ||
pub fn sorting() -> impl Filter<Extract = (Sorting,), Error = Rejection> + Copy { | ||
warp::query() | ||
} | ||
|
||
pub mod prelude { | ||
//! Prelude: re-export most commonly used traits, structs and macros from this module. | ||
pub use super::*; | ||
} |