Created
September 12, 2020 19:21
-
-
Save Armiixteryx/a0aed5e423928de5b03d7fe14c70d9cd to your computer and use it in GitHub Desktop.
base64_file
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
#[derive(Debug)] | |
pub struct Base64File { | |
filename: String, | |
content: String, | |
content_type: String, | |
size: usize, | |
} | |
impl Base64File { | |
pub fn from_bytes(filename: String, bytes: Vec<u8>, content_type: String, size: usize) -> Base64File { | |
Self { | |
filename, | |
content: base64::encode(bytes), | |
content_type, | |
size | |
} | |
} | |
pub fn print(&self) -> &str { | |
self.content.as_str() | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
#[test] | |
fn convert_text() { | |
use crate::Base64File; | |
let bytes = String::from("Hello World!").into_bytes(); | |
let len = bytes.len(); | |
let result = Base64File::from_bytes("Hello".into(), bytes, "application/pdf".into(), len); | |
assert_eq!("SGVsbG8gV29ybGQh", result.print()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment