Crates.io | url_encor |
lib.rs | url_encor |
version | 1.0.2 |
source | src |
created_at | 2024-07-09 20:14:15.359847 |
updated_at | 2024-07-11 20:15:03.340788 |
description | A lightweight library to encode and decode special characters in urls |
homepage | |
repository | https://github.com/Dari-OS/url_encor |
max_upload_size | |
id | 1297440 |
size | 25,502 |
A small and lightweight:feather: rust library to encode and decode URLs!
url_encor aims to provide fast :rocket: url encoding and decoding
It achieves this by doing most of the heavy lifting :muscle: using preprocessed data.
The following things get preprocessed:
Take a look at this file and see what gets preprocessed!
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")
}
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!")
}
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!()
}
}
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())
}
}
If you encounter any problem, bug or issue, please open a new issue