update buf-rand

This commit is contained in:
LordMZTE 2020-10-06 01:22:51 +02:00
parent ebe59d3195
commit aaad7908eb

View file

@ -82,7 +82,7 @@ async fn main() -> Result<()> {
if let (Some(favicon), true) = (response.favicon, matches.is_present("image")) { if let (Some(favicon), true) = (response.favicon, matches.is_present("image")) {
//The image parsing and asciifying is done while the table is printing //The image parsing and asciifying is done while the table is printing
image = Some(tokio::spawn(get_image( image = Some(tokio::spawn(asciify_base64_image(
favicon, favicon,
AsciiConfig { AsciiConfig {
size: Some(image_size), size: Some(image_size),
@ -122,10 +122,10 @@ async fn main() -> Result<()> {
/// returns the asciifyed image from base64 /// returns the asciifyed image from base64
/// returns Err if the base64 image is invalid /// returns Err if the base64 image is invalid
async fn get_image(favicon: String, config: AsciiConfig) -> Result<String> { async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<String> {
let img = image_base64::from_base64(favicon); let img = image_base64::from_base64(favicon);
let image = let image =
image::load(Cursor::new(img), ImageFormat::Png).context("favicon has invalid format")?; image::load(Cursor::new(img), ImageFormat::Png).context("image has invalid format")?;
let builder = config.apply(AsciiBuilder::new_from_image(image)); let builder = config.apply(AsciiBuilder::new_from_image(image));
@ -139,6 +139,7 @@ async fn get_image(favicon: String, config: AsciiConfig) -> Result<String> {
builder.to_stream(&mut buf); builder.to_stream(&mut buf);
buf buf
}; };
//reset color
buf.reset()?; buf.reset()?;
let bytes = buf.as_slice().to_vec(); let bytes = buf.as_slice().to_vec();
@ -147,6 +148,7 @@ async fn get_image(favicon: String, config: AsciiConfig) -> Result<String> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let out = String::from_utf8(bytes).expect("asciifyed image is invalid utf8"); let out = String::from_utf8(bytes).expect("asciifyed image is invalid utf8");
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
//bytes should always be valid utf8
let out = unsafe { String::from_utf8_unchecked(bytes) }; let out = unsafe { String::from_utf8_unchecked(bytes) };
Ok(out) Ok(out)