Skip to main content

oauth1_client/
percent_encoding.rs

1//! Module with methods for handling RFC3986 percent encoding
2//! with an OAuth1.0a specific reserved characters set
3
4use rfc3986::{percent_decode_str, utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
5
6/// Reserved characters set for the RFC3986 percent encoding
7pub const RESERVED_CHARACTERS: &AsciiSet = &NON_ALPHANUMERIC
8    .remove(b'-')
9    .remove(b'.')
10    .remove(b'_')
11    .remove(b'~');
12
13/// Encode string with the RFC3986 percent encoding with the reserved characters set specified in OAuth1.0a
14pub fn encode_string(input: &str) -> String {
15    utf8_percent_encode(input, RESERVED_CHARACTERS).to_string()
16}
17
18/// Decode string with the RFC3986 percent encoding with the reserved characters set specified in OAuth1.0a
19pub fn decode_string(input: &str) -> Result<String, Box<dyn std::error::Error>> {
20    Ok(percent_decode_str(input)?.decode_utf8()?.to_string())
21}