use crate::percent_codec::{
percent_decode_bytes,
percent_encode_bytes,
};
use crate::{
CodecError,
CodecResult,
Decoder,
Encoder,
};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FormUrlencodedCodec;
impl FormUrlencodedCodec {
pub fn new() -> Self {
Self
}
pub fn encode(&self, text: &str) -> String {
percent_encode_bytes(text.as_bytes(), true)
}
pub fn decode(&self, text: &str) -> CodecResult<String> {
String::from_utf8(percent_decode_bytes(text, true)?).map_err(CodecError::from)
}
}
impl Encoder<str> for FormUrlencodedCodec {
type Error = CodecError;
type Output = String;
fn encode(&self, input: &str) -> Result<Self::Output, Self::Error> {
Ok(FormUrlencodedCodec::encode(self, input))
}
}
impl Decoder<str> for FormUrlencodedCodec {
type Error = CodecError;
type Output = String;
fn decode(&self, input: &str) -> Result<Self::Output, Self::Error> {
FormUrlencodedCodec::decode(self, input)
}
}