url_encor

Crates.iourl_encor
lib.rsurl_encor
version1.0.2
sourcesrc
created_at2024-07-09 20:14:15.359847
updated_at2024-07-11 20:15:03.340788
descriptionA lightweight library to encode and decode special characters in urls
homepage
repositoryhttps://github.com/Dari-OS/url_encor
max_upload_size
id1297440
size25,502
Darius (Dari-OS)

documentation

README

url_encor :globe_with_meridians:

A small and lightweight:feather: rust library to encode and decode URLs!

Goal :white_check_mark:

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:

  • 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 :gear:

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 :link:

Issues :interrobang:

If you encounter any problem, bug or issue, please open a new issue

Commit count: 35

cargo fmt