From 124e48e238a19c03d7434f7f0e6fe3ef1cfdcfa6 Mon Sep 17 00:00:00 2001 From: tilpner Date: Thu, 10 Sep 2020 22:04:26 +0200 Subject: Update to async, smol 1.0 --- src/main.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 7810a1d..672ddde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,9 +13,11 @@ use rand; use config::Config; use structopt::StructOpt; -use mime_guess::{ Mime, guess_mime_type_opt }; +use mime_guess::{ Mime, MimeGuess }; -use futures::stream; +use bytes::Bytes; +use futures::stream::{ self, StreamExt }; +use async_compat::Compat; use rusoto_core::{ request, Region, ByteStream, @@ -89,6 +91,10 @@ fn main() { cfg }; + smol::block_on(Compat::new(async_main(cfg))); +} + +async fn async_main(cfg: Config) { let region = Region::Custom { endpoint: { // trailing / causes problems with rusoto @@ -104,18 +110,18 @@ fn main() { let code = cfg.get_bool("code").unwrap() || lang.is_some(); - let input: Box = if let Ok(ref path) = path { + let input: Box = if let Ok(ref path) = path { Box::new(BufReader::new(File::open(path).unwrap())) } else { let stdin = io::stdin(); Box::new(BufReader::new(stdin)) }; - let data: Box> + Send> = + let data: Box + Send + Sync> = if code { Box::new(highlight(&cfg, input)) } else { Box::new(chunk_iter(input)) }; - let body = ByteStream::new(stream::iter_ok(data)); + let body = ByteStream::new(stream::iter(data).map(Ok)); let dispatcher = request::HttpClient::new().expect("Unable to create rusoto http client"); let credentials = StaticProvider::new_minimal( @@ -128,14 +134,16 @@ fn main() { .unwrap_or_else(|| { let len = cfg.get_int("id_length").unwrap() as u32; (0..).map(|_| random_id(len)) - .filter(|key| !check_exists(&client, bucket.clone(), key.clone())) + .filter(|key| !smol::block_on(check_exists(&client, bucket.clone(), key.clone()))) .next().unwrap() }); let mime = if code { String::from("text/html; charset=utf-8") } else { cfg.get_str("mime").ok() - .or_else(|| path.ok().and_then(guess_mime_type_opt) - .as_ref().map(Mime::to_string)) + .or_else(|| path.ok() + .and_then(|path| MimeGuess::from_path(path).first()) + .as_ref() + .map(Mime::to_string)) .unwrap_or_else(|| String::from("text/plain; charset=utf-8")) }; @@ -145,7 +153,7 @@ fn main() { content_type: Some(mime), body: Some(body), ..Default::default() - }).sync().expect("Put failed"); + }).await.expect("Put failed"); println!("{}", name); } @@ -153,7 +161,7 @@ fn main() { static TEMPLATE: &'static str = include_str!("./code.html"); #[derive(Serialize)] struct Template { title: String, code: String } -fn highlight(cfg: &Config, mut input: Box) -> impl Iterator> { +fn highlight(cfg: &Config, mut input: Box) -> impl Iterator { use syntect::{ parsing::{ SyntaxSet, SyntaxDefinition }, highlighting::ThemeSet, @@ -215,14 +223,14 @@ fn highlight(cfg: &Config, mut input: Box) -> impl Iterator< code: String::from_utf8(html).expect("Invalid UTF8") }).unwrap(); - iter::once(rendered.as_bytes().to_vec()) + iter::once(Bytes::copy_from_slice(rendered.as_bytes())) } -fn chunk_iter(input: Box) -> impl Iterator> { +fn chunk_iter(input: Box) -> impl Iterator { itertools::unfold(input, |input| { let buf = match input.fill_buf().ok() { Some([]) => None, - Some(buf) => Some(buf.to_vec()), + Some(buf) => Some(Bytes::copy_from_slice(buf)), None => None }; @@ -231,11 +239,11 @@ fn chunk_iter(input: Box) -> impl Iterator> { }) } -fn check_exists(c: &S3Client, bucket: String, key: String) -> bool { +async fn check_exists(c: &S3Client, bucket: String, key: String) -> bool { c.head_object(HeadObjectRequest { bucket, key, ..Default::default() - }).sync().is_ok() + }).await.is_ok() } fn random_id(size: u32) -> String { -- cgit v1.2.3