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
85
86
87
88
89
#![allow(dead_code)]
use crate::{DecodeError, DecodeTrait, EncodeTrait};
extern crate urlencoding;
#[derive(Debug, Clone)]
pub struct URLCode(String);
impl Into<String> for URLCode {
fn into(self) -> String {
self.0
}
}
pub fn vec_ref<'a>(s: &'a String) -> Vec<&'a str> {
let temp = s.split("");
temp.filter(|&s| s != "").collect::<Vec<&str>>()
}
impl URLCode {
pub fn try_decode(&self) -> Result<String, DecodeError> {
Self::decode(self.get_inner())
}
pub fn get_inner(&self) -> String {
self.0.clone()
}
pub fn vec_ref(&self) -> Vec<&str> {
vec_ref(&self.0)
}
}
impl From<&str> for URLCode {
fn from(s: &str) -> Self {
URLCode(Self::encode(s))
}
}
impl From<String> for URLCode {
fn from(s: String) -> Self {
URLCode(Self::encode(s))
}
}
impl EncodeTrait<&str> for URLCode {
fn encode(s: &str) -> String {
urlencoding::encode(s)
}
}
impl EncodeTrait<String> for URLCode {
fn encode(s: String) -> String {
urlencoding::encode(&s)
}
}
impl DecodeTrait<&str> for URLCode {
fn decode(s: &str) -> Result<String, DecodeError> {
urlencoding::decode(s).map_err(|_| DecodeError(s.to_string()))
}
}
impl DecodeTrait<String> for URLCode {
fn decode(s: String) -> Result<String, DecodeError> {
urlencoding::decode(&s).map_err(|_| DecodeError(s.to_string()))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_escape_code_encode() {
let test = URLCode::encode("test测试");
assert_eq!("test%E6%B5%8B%E8%AF%95".to_string(), test);
}
#[test]
fn test_escape_code_decode() {
let test = URLCode::decode("test%E6%B5%8B%E8%AF%95");
assert_eq!(test, Ok("test测试".to_string()));
}
}