6 releases (3 stable)
1.0.2 | Jul 11, 2024 |
---|---|
1.0.0 | Jul 10, 2024 |
0.3.0 | Jul 10, 2024 |
0.2.0 | Jul 9, 2024 |
0.1.0 | Jul 9, 2024 |
#528 in Text processing
26 downloads per month
Used in pods-blitz
19KB
213 lines
url_encor 🌐
A small and lightweight🪶 rust library to encode and decode URLs!
Goal ✅
url_encor aims to provide fast 🚀 url encoding and decoding
It achieves this by doing most of the heavy lifting 💪 using preprocessed data.
The following things get preprocessed:
- Decimal to Hex conversion
- Hex to Decimal conversion
- Deciding whether the character should get encoded
Take a look at this file and see what gets preprocessed!
Usage ⚙️
Encoding a String is as easy as it gets
use url_encor::Encoder;
fn main() {
let string_to_encode = String::from("Hello, World!");
println!("{}", string_to_encode.url_encode());
//OUTPUT: Hello%2C%20World%21
assert_eq!(string_to_encode.url_encode(), "Hello%2C%20World%21")
}
Decoding is easy, too
use url_encor::Encoder;
fn main() {
let string_to_decode = String::from("Hello%2C%20World%21");
println!("{}", string_to_decode.url_decode());
//OUTPUT: Hello, World!
assert_eq!(string_to_decode.url_decode(), "Hello, World!")
}
Implementing custom encoding logic is easy as well
use std::fmt::{Debug, Formatter};
use url_encor::{Encoder, encode};
fn main() {
let custom_type_to_encode = CharVector(vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ]);
println!("{:?}", custom_type_to_encode.url_encode());
//OUTPUT: ['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']
assert_eq!(custom_type_to_encode.url_encode().0, vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1'])
}
pub struct CharVector(Vec<char>);
impl Debug for CharVector {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Encoder<CharVector> for CharVector {
fn url_encode(&self) -> CharVector {
CharVector(encode(&self.0.iter().collect::<String>()).chars().collect())
}
fn url_decode(&self) -> CharVector {
todo!()
}
}
Implementation of custom decoding logic
use std::fmt::{Debug, Formatter};
use url_encor::{Encoder, decode};
fn main() {
let custom_type_to_decode = CharVector(vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']);
println!("{:?}", custom_type_to_decode.url_decode());
//OUTPUT: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
assert_eq!(custom_type_to_decode.url_decode().0, vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ])
}
pub struct CharVector(Vec<char>);
impl Debug for CharVector {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Encoder<CharVector> for CharVector {
fn url_encode(&self) -> CharVector {
todo!()
}
fn url_decode(&self) -> CharVector {
CharVector(decode(&self.0.iter().collect::<String>()).chars().collect())
}
}
Related links 🔗
Issues ⁉️
If you encounter any problem, bug or issue, please open a new issue