1#![allow(dead_code)]
3
4use crate::{DecodeError, DecodeTrait, EncodeTrait};
5
6extern crate urlencoding;
7
8#[derive(Debug, Clone)]
10pub struct URLCode(String);
11
12impl Into<String> for URLCode {
13 fn into(self) -> String {
14 self.0
15 }
16}
17
18pub fn vec_ref<'a>(s: &'a String) -> Vec<&'a str> {
19 let temp = s.split("");
20 temp.filter(|&s| s != "").collect::<Vec<&str>>()
21}
22
23impl URLCode {
24 pub fn try_decode(&self) -> Result<String, DecodeError> {
25 Self::decode(self.get_inner())
26 }
27
28 pub fn get_inner(&self) -> String {
29 self.0.clone()
30 }
31
32 pub fn vec_ref(&self) -> Vec<&str> {
33 vec_ref(&self.0)
34 }
35}
36
37impl From<&str> for URLCode {
38 fn from(s: &str) -> Self {
39 URLCode(Self::encode(s))
40 }
41}
42
43impl From<String> for URLCode {
44 fn from(s: String) -> Self {
45 URLCode(Self::encode(s))
46 }
47}
48
49impl EncodeTrait<&str> for URLCode {
50 fn encode(s: &str) -> String {
51 urlencoding::encode(s)
52 }
53}
54
55impl EncodeTrait<String> for URLCode {
56 fn encode(s: String) -> String {
57 urlencoding::encode(&s)
58 }
59}
60
61impl DecodeTrait<&str> for URLCode {
62 fn decode(s: &str) -> Result<String, DecodeError> {
63 urlencoding::decode(s).map_err(|_| DecodeError(s.to_string()))
64 }
65}
66
67impl DecodeTrait<String> for URLCode {
68 fn decode(s: String) -> Result<String, DecodeError> {
69 urlencoding::decode(&s).map_err(|_| DecodeError(s.to_string()))
70 }
71}
72
73#[cfg(test)]
74mod test {
75
76 use super::*;
77
78 #[test]
79 fn test_escape_code_encode() {
80 let test = URLCode::encode("test测试");
81 assert_eq!("test%E6%B5%8B%E8%AF%95".to_string(), test);
82 }
83
84 #[test]
85 fn test_escape_code_decode() {
86 let test = URLCode::decode("test%E6%B5%8B%E8%AF%95");
87 assert_eq!(test, Ok("test测试".to_string()));
88 }
89}