Remove duplicated code and make base64 error handling better

This commit is contained in:
2022-08-14 09:12:36 -04:00
parent fbf26efa44
commit 5158c16d56
8 changed files with 132 additions and 88 deletions

View File

@@ -55,7 +55,7 @@ impl BytesBase64 {
Bytes(v.chunks(4).map(|c| to_bytes(c)).flatten().collect())
}
pub fn from_base64(s: &str) -> BytesBase64 {
pub fn from_base64(s: &str) -> Result<BytesBase64, String> {
let mut r: Vec<u8> = Vec::with_capacity(s.len());
for c in s.chars() {
match c {
@@ -64,10 +64,15 @@ impl BytesBase64 {
'0'..='9' => r.push((c as u8) - ('0' as u8) + 52),
'+' => r.push(62),
'/' => r.push(63),
_ => (),
'\n' => (),
'=' => (),
' ' => (),
_ => {
return Err(format!("Unexpected character '{}'", c));
}
}
}
BytesBase64(r)
Ok(BytesBase64(r))
}
pub fn to_string(&self) -> String {