[][src]Crate pct_str

Percent-encoded strings manipulation.

This crate provides two types, PctStr and PctString, similar to str and String, representing percent-encoded strings used in URL, URI, IRI, etc. You can use them to encode, decode and compare percent-encoded strings.

Basic usage

You can parse/decode percent-encoded strings by building a PctStr slice over a str slice.

use pct_str::PctStr;

let pct_str = PctStr::new("Hello%20World%21").unwrap();

assert!(pct_str == "Hello World!");

let decoded_string: String = pct_str.decode();
println!("{}", decoded_string); // => Hello World!

To create new percent-encoded strings, use the PctString to copy or encode new strings.

use pct_str::{PctString, URIReserved};

// Copy the given percent-encoded string.
let pct_string = PctString::new("Hello%20World%21").unwrap();

// Encode the given regular string.
let pct_string = PctString::encode("Hello World!".chars(), URIReserved);

println!("{}", pct_string.as_str()); // => Hello World%21

You can choose which character will be percent-encoded by the encode function by implementing the Encoder trait.

use pct_str::{URIReserved, PctString};

struct CustomEncoder;

impl pct_str::Encoder for CustomEncoder {
	fn encode(&self, c: char) -> bool {
		URIReserved.encode(c) || c.is_uppercase()
	}
}

let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
println!("{}", pct_string.as_str()); // => %48ello %57orld%21

Structs

Chars

Characters iterator.

InvalidEncoding

Encoding error.

PctStr

Percent-Encoded string slice.

PctString

Owned, mutable percent-encoded string.

URIReserved

URI-reserved characters encoder.

Traits

Encoder

Encoding predicate.

Functions

is_pct_encoded

Checks if a string is a correct percent-encoded string.

Type Definitions

Result

Result of a function performing a percent-encoding check.