rs_urlencoding/
lib.rs

1//! To encode a string, do the following:
2//!
3//! ```rust
4//! use urlencoding::encode;
5//!
6//! let encoded = encode("This string will be URL encoded.");
7//! println!("{}", encoded);
8//! // This%20string%20will%20be%20URL%20encoded.
9//! ```
10//!
11//! To decode a string, it's only slightly different:
12//!
13//! ```rust
14//! use urlencoding::decode;
15//!
16//! let decoded = decode("%F0%9F%91%BE%20Exterminate%21").expect("UTF-8");
17//! println!("{}", decoded);
18//! // 👾 Exterminate!
19//! ```
20//!
21//! To decode allowing arbitrary bytes and invalid UTF-8:
22//!
23//! ```rust
24//! use urlencoding::decode_binary;
25//!
26//! let binary = decode_binary(b"%F1%F2%F3%C0%C1%C2");
27//! let decoded = String::from_utf8_lossy(&binary);
28//! ```
29//!
30//! This library returns [`Cow`](https://doc.rust-lang.org/stable/std/borrow/enum.Cow.html) to avoid allocating when decoding/encoding is not needed. Call `.into_owned()` on the `Cow` to get a `Vec` or `String`.
31
32// #![feature(matches_macro)] fix bug use nightly-2019-12-01-x86_64-apple-darwin build
33//error[E0658]: use of unstable library feature 'matches_macro'
34//--> /Users/smalls/.cargo/registry/src/github.com-1ecc6299db9ec823/urlencoding-2.1.0/src/enc.rs:100:31
35//|
36//100 |             .take_while(|&&c| matches!(c, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' |  b'-' | b'.' | b'_' | b'~')).count();
37//|                               ^^^^^^^
38//|
39//= note: for more information, see https://github.com/rust-lang/rust/issues/65721
40//= help: add `#![feature(matches_macro)]` to the crate attributes to enable
41#![feature(matches_macro)]
42
43mod enc;
44pub use enc::encode;
45pub use enc::encode_binary;
46pub use enc::Encoded;
47
48mod dec;
49pub use dec::decode;
50pub use dec::decode_binary;
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::dec::from_hex_digit;
56
57    #[test]
58    fn it_encodes_successfully() {
59        let expected = "this%20that";
60        assert_eq!(expected, encode("this that"));
61    }
62
63    #[test]
64    fn it_encodes_successfully_emoji() {
65        let emoji_string = "👾 Exterminate!";
66        let expected = "%F0%9F%91%BE%20Exterminate%21";
67        assert_eq!(expected, encode(emoji_string));
68    }
69
70    #[test]
71    fn it_decodes_successfully() {
72        let expected = String::from("this that");
73        let encoded = "this%20that";
74        assert_eq!(expected, decode(encoded).unwrap());
75    }
76
77    #[test]
78    fn it_decodes_successfully_emoji() {
79        let expected = String::from("👾 Exterminate!");
80        let encoded = "%F0%9F%91%BE%20Exterminate%21";
81        assert_eq!(expected, decode(encoded).unwrap());
82    }
83
84    #[test]
85    fn it_decodes_unsuccessfully_emoji() {
86        let bad_encoded_string = "👾 Exterminate!";
87
88        assert_eq!(bad_encoded_string, decode(bad_encoded_string).unwrap());
89    }
90
91
92    #[test]
93    fn misc() {
94        assert_eq!(3, from_hex_digit(b'3').unwrap());
95        assert_eq!(10, from_hex_digit(b'a').unwrap());
96        assert_eq!(15, from_hex_digit(b'F').unwrap());
97        assert_eq!(None, from_hex_digit(b'G'));
98        assert_eq!(None, from_hex_digit(9));
99
100        assert_eq!("pureascii", encode("pureascii"));
101        assert_eq!("pureascii", decode("pureascii").unwrap());
102        assert_eq!("", encode(""));
103        assert_eq!("", decode("").unwrap());
104        assert_eq!("%26a%25b%21c.d%3Fe", encode("&a%b!c.d?e"));
105        assert_eq!("%00", encode("\0"));
106        assert_eq!("%00x", encode("\0x"));
107        assert_eq!("x%00", encode("x\0"));
108        assert_eq!("x%00x", encode("x\0x"));
109        assert_eq!("aa%00%00bb", encode("aa\0\0bb"));
110        assert_eq!("\0", decode("\0").unwrap());
111        assert!(decode("%F0%0F%91%BE%20Hello%21").is_err());
112        assert_eq!("this that", decode("this%20that").unwrap());
113        assert_eq!("this that%", decode("this%20that%").unwrap());
114        assert_eq!("this that%2", decode("this%20that%2").unwrap());
115        assert_eq!("this that%%", decode("this%20that%%").unwrap());
116        assert_eq!("this that%2%", decode("this%20that%2%").unwrap());
117        assert_eq!("this%2that", decode("this%2that").unwrap());
118        assert_eq!("this%%2that", decode("this%%2that").unwrap());
119        assert_eq!("this%2x&that", decode("this%2x%26that").unwrap());
120        // assert_eq!("this%2&that", decode("this%2%26that").unwrap());
121    }
122
123    #[test]
124    fn lazy_writer() {
125        let mut s = "he".to_string();
126        Encoded("llo").append_to(&mut s);
127        assert_eq!("hello", s);
128
129        assert_eq!("hello", Encoded("hello").to_string());
130        assert_eq!("hello", format!("{}", Encoded("hello")));
131        assert_eq!("hello", Encoded("hello").to_str());
132        assert!(matches!(Encoded("hello").to_str(), std::borrow::Cow::Borrowed(_)));
133    }
134
135    #[test]
136    fn whatwg_examples() {
137        assert_eq!(*decode_binary(b"%25%s%1G"), b"%%s%1G"[..]);
138        assert_eq!(*decode_binary("‽%25%2E".as_bytes()), b"\xE2\x80\xBD\x25\x2E"[..]);
139        assert_eq!(encode("≡"), "%E2%89%A1");
140        assert_eq!(encode("‽"), "%E2%80%BD");
141        assert_eq!(encode("Say what‽"), "Say%20what%E2%80%BD");
142    }
143
144}