-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7646cf7
commit 73398c3
Showing
8 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::errors::decode::DecodeError; | ||
|
||
pub(crate) fn percent_decode(input: &[u8]) -> Result<Vec<u8>, DecodeError> { | ||
if !input.contains(&b'%') { | ||
return Ok(input.to_vec()); | ||
} | ||
|
||
let mut output = Vec::with_capacity(input.len()); | ||
|
||
let mut i = 0; | ||
let len = input.len(); | ||
|
||
while i < len { | ||
if input[i] == b'%' && i + 2 < len { | ||
let a = input[i + 1]; | ||
let b = input[i + 2]; | ||
output.push(decode_hex(a, b)?); | ||
i += 3; | ||
} else { | ||
output.push(input[i]); | ||
i += 1; | ||
} | ||
} | ||
|
||
Ok(output) | ||
} | ||
|
||
#[allow(clippy::cast_possible_truncation)] | ||
fn decode_hex(a: u8, b: u8) -> Result<u8, DecodeError> { | ||
let a = (a as char) | ||
.to_digit(16) | ||
.ok_or(DecodeError::InvalidEncoding)?; | ||
|
||
let b = (b as char) | ||
.to_digit(16) | ||
.ok_or(DecodeError::InvalidEncoding)?; | ||
|
||
Ok((a as u8) << 4 | (b as u8)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod constraint; | ||
pub mod decode; | ||
pub mod delete; | ||
pub mod insert; | ||
pub mod route; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::{error::Error, fmt::Display, str::Utf8Error}; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum DecodeError { | ||
Utf8Error(Utf8Error), | ||
InvalidEncoding, | ||
} | ||
|
||
impl Error for DecodeError {} | ||
|
||
impl Display for DecodeError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Utf8Error(error) => error.fmt(f), | ||
Self::InvalidEncoding => write!(f, "Invalid Encoding"), | ||
} | ||
} | ||
} | ||
|
||
impl From<Utf8Error> for DecodeError { | ||
fn from(error: Utf8Error) -> Self { | ||
Self::Utf8Error(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod constraints; | ||
pub mod decode; | ||
pub mod errors; | ||
pub mod node; | ||
pub mod parts; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters