forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparam.rs
84 lines (72 loc) · 2.54 KB
/
param.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
use aptos_api_types::{Address, Error, EventKey, MoveStructTag, TransactionId};
use move_deps::move_core_types::identifier::Identifier;
use percent_encoding::percent_decode_str;
use serde::{Deserialize, Deserializer};
use std::{convert::Infallible, str::FromStr};
pub type AddressParam = Param<Address>;
pub type EventKeyParam = Param<EventKey>;
pub type LedgerVersionParam = Param<u64>;
pub type MoveStructTagParam = Param<MoveStructTag>;
pub type MoveIdentifierParam = Param<Identifier>;
pub type TableHandleParam = Param<u128>;
pub type TransactionIdParam = Param<TransactionId>;
pub type TransactionVersionParam = Param<u64>;
/// `Param` is designed for parsing `warp` path parameter or query string
/// into a type specified by the generic type parameter of `Param`.
#[derive(Clone, Debug)]
pub struct Param<T: FromStr> {
data: String,
_value: Option<T>,
}
/// `FromStr` is required for parsing `warp` path parameter into `Param` type.
impl<T: FromStr> FromStr for Param<T> {
type Err = Infallible;
fn from_str(data: &str) -> Result<Self, Infallible> {
Ok(Self {
data: data.to_owned(),
_value: None,
})
}
}
impl<T: FromStr> Param<T> {
pub fn parse(self, name: &str) -> Result<T, Error> {
let decoded = percent_decode_str(&self.data)
.decode_utf8()
.map_err(|_| Error::invalid_param(name, &self.data))?;
decoded
.parse()
.map_err(|_| Error::invalid_param(name, &decoded))
}
}
/// `Deserialize` is required for parsing `warp` query string parameter into `Param` type.
impl<'de, T: FromStr> Deserialize<'de> for Param<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let data = <String>::deserialize(deserializer)?;
Ok(Self { data, _value: None })
}
}
#[cfg(test)]
mod tests {
use super::MoveIdentifierParam;
use std::str::FromStr;
#[test]
fn test_parse_percent_encoded_path_parameter() {
let param = MoveIdentifierParam::from_str("abcd%5F").unwrap();
assert!(param.parse("param_name").is_ok())
}
#[test]
fn test_parse_percent_encoded_path_parameter_failed() {
let param = MoveIdentifierParam::from_str("%3Aabcd").unwrap();
let ret = param.parse("param_name");
assert!(ret.is_err());
assert_eq!(
"400 Bad Request: invalid parameter param_name: :abcd",
ret.err().unwrap().to_string()
);
}
}