Remove duplicated code and make base64 error handling better
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user