aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortilpner2020-09-10 22:04:26 +0200
committertilpner2020-09-10 22:05:25 +0200
commit124e48e238a19c03d7434f7f0e6fe3ef1cfdcfa6 (patch)
treefef0a4b71e40537d01c41df78e62dfa712b69fa0 /src
parent30d8303badb0718c35ecc97334b9d7a4fc264ce6 (diff)
downloadrpb-s3-124e48e238a19c03d7434f7f0e6fe3ef1cfdcfa6.tar.gz
rpb-s3-124e48e238a19c03d7434f7f0e6fe3ef1cfdcfa6.tar.xz
rpb-s3-124e48e238a19c03d7434f7f0e6fe3ef1cfdcfa6.zip
Update to async, smol 1.0
Diffstat (limited to 'src')
-rw-r--r--src/base62.rs1
-rw-r--r--src/main.rs38
2 files changed, 23 insertions, 16 deletions
diff --git a/src/base62.rs b/src/base62.rs
index a556b13..a87503f 100644
--- a/src/base62.rs
+++ b/src/base62.rs
@@ -24,7 +24,6 @@ where I: Iterator<Item=u8> {
pub fn digits_to_string<'a, I>(x: I, alphabet: &'a [u8]) -> String
where I: Iterator<Item=u8> {
x.map(|d| alphabet[d as usize] as char).collect()
-
}
#[cfg(test)]
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<dyn BufRead + Send> = if let Ok(ref path) = path {
+ let input: Box<dyn BufRead + Send + Sync> = 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<Iterator<Item=Vec<u8>> + Send> =
+ let data: Box<dyn Iterator<Item=Bytes> + 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<dyn BufRead + Send>) -> impl Iterator<Item=Vec<u8>> {
+fn highlight(cfg: &Config, mut input: Box<dyn BufRead + Send>) -> impl Iterator<Item=Bytes> {
use syntect::{
parsing::{ SyntaxSet, SyntaxDefinition },
highlighting::ThemeSet,
@@ -215,14 +223,14 @@ fn highlight(cfg: &Config, mut input: Box<dyn BufRead + Send>) -> 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<dyn BufRead + Send>) -> impl Iterator<Item=Vec<u8>> {
+fn chunk_iter(input: Box<dyn BufRead + Send + Sync>) -> impl Iterator<Item=Bytes> {
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<dyn BufRead + Send>) -> impl Iterator<Item=Vec<u8>> {
})
}
-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 {