Crate url_encor

Source
Expand description

§url_encor Crate

This crate provides functionality for URL encoding and decoding. It includes both standalone functions and a trait implementation for convenient use with String types. The trait uses generics, therefore the return value can be manipulated!

§Examples

§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())
    }
}

§Issues?!

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

Traits§

Encoder
A trait for types that can be URL encoded and decoded.

Functions§

decode
Decodes a URL-encoded string.
encode
Encodes a string using url_encor.