Skip to content

Commit fa7b449

Browse files
committed
Use async file io and don't unwrap
1 parent 3dcecf1 commit fa7b449

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT"
77
edition = "2021"
88

99
[dependencies]
10+
async-std = "1"
1011
serde = { version = "^1.0", features = ["derive"] }
1112
serde_with = { version = "^3.8", default-features = false, features = ["base64", "std", "macros"] }
1213
serde_json = "^1.0"

generate.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ sed -i 's/Result<models::CurrentUser, Error<GetCurrentUserError>>/Result<models:
4242
# https://github.com/vrchatapi/vrchatapi-rust/pull/29
4343
sed -i "s/local_var_req_builder = local_var_req_builder.json(&\(.*\));/if let Some(\1) = \1 { \0 }/g" src/apis/files_api.rs
4444

45-
sed -i 's|// TODO: support file upload for '\''file'\'' parameter|let part = reqwest::multipart::Part::bytes(std::fs::read(\&file).unwrap()).file_name(file.file_name().unwrap().to_string_lossy().to_string()).mime_str(if file.file_name().unwrap().to_string_lossy().to_string().ends_with("png") { "image/png" } else { "application/octet-stream" })?;\n\tlocal_var_form = local_var_form.part("file", part);|' src/apis/files_api.rs
45+
#https://github.com/vrchatapi/vrchatapi-rust/pull/30
46+
#Add async_std for async file io
47+
sed -i 's/\[dependencies\]/\0\nasync-std = "1"/' Cargo.toml
48+
sed -i "s/pub\s\+enum\s\+Error<T>\s\+{/\0\nAsyncStdIo(::async_std::io::Error),/" src/apis/mod.rs
49+
sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\("reqwest", e\.to_string\(\)\),/Error::AsyncStdIo(e) => ("async_std", e.to_string()),\0/' src/apis/mod.rs #Fix Debug
50+
sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\e,/Error::AsyncStdIo(e) => e,\n\0/' src/apis/mod.rs #Fix Error
51+
sed -i "s/impl<T> From<reqwest::Error> for Error<T>/impl<T> From<::async_std::io::Error> for Error<T> {\nfn from(val: ::async_std::io::Error) -> Self {\n Error::AsyncStdIo(val)\n}\n\}\n\0/" src/apis/mod.rs #Add From impl
52+
#Use the async file io
53+
sed -i 's|// TODO: support file upload for '\''file'\'' parameter|let part = {let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(\&file).await?);\nif let Some(filename) = file.file_name() { \npart = part.file_name(filename.to_string_lossy().to_string()).mime_str(if filename.to_string_lossy().ends_with("png") { "image/png" } else { "application/octet-stream" })?;} \n else { part = part.mime_str("application/octet-stream")?; }\n part };\n\tlocal_var_form = local_var_form.part("file", part);|' src/apis/files_api.rs
4654

4755
cargo fmt
4856
cargo build

src/apis/files_api.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -585,21 +585,21 @@ pub async fn upload_gallery_image(
585585
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
586586
}
587587
let mut local_var_form = reqwest::multipart::Form::new();
588-
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
589-
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
590-
.mime_str(
591-
if file
592-
.file_name()
593-
.unwrap()
594-
.to_string_lossy()
595-
.to_string()
596-
.ends_with("png")
597-
{
598-
"image/png"
599-
} else {
600-
"application/octet-stream"
601-
},
602-
)?;
588+
let part = {
589+
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
590+
if let Some(filename) = file.file_name() {
591+
part = part
592+
.file_name(filename.to_string_lossy().to_string())
593+
.mime_str(if filename.to_string_lossy().ends_with("png") {
594+
"image/png"
595+
} else {
596+
"application/octet-stream"
597+
})?;
598+
} else {
599+
part = part.mime_str("application/octet-stream")?;
600+
}
601+
part
602+
};
603603
local_var_form = local_var_form.part("file", part);
604604
local_var_req_builder = local_var_req_builder.multipart(local_var_form);
605605

@@ -641,21 +641,21 @@ pub async fn upload_icon(
641641
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
642642
}
643643
let mut local_var_form = reqwest::multipart::Form::new();
644-
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
645-
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
646-
.mime_str(
647-
if file
648-
.file_name()
649-
.unwrap()
650-
.to_string_lossy()
651-
.to_string()
652-
.ends_with("png")
653-
{
654-
"image/png"
655-
} else {
656-
"application/octet-stream"
657-
},
658-
)?;
644+
let part = {
645+
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
646+
if let Some(filename) = file.file_name() {
647+
part = part
648+
.file_name(filename.to_string_lossy().to_string())
649+
.mime_str(if filename.to_string_lossy().ends_with("png") {
650+
"image/png"
651+
} else {
652+
"application/octet-stream"
653+
})?;
654+
} else {
655+
part = part.mime_str("application/octet-stream")?;
656+
}
657+
part
658+
};
659659
local_var_form = local_var_form.part("file", part);
660660
local_var_req_builder = local_var_req_builder.multipart(local_var_form);
661661

@@ -700,21 +700,21 @@ pub async fn upload_image(
700700
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
701701
}
702702
let mut local_var_form = reqwest::multipart::Form::new();
703-
let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap())
704-
.file_name(file.file_name().unwrap().to_string_lossy().to_string())
705-
.mime_str(
706-
if file
707-
.file_name()
708-
.unwrap()
709-
.to_string_lossy()
710-
.to_string()
711-
.ends_with("png")
712-
{
713-
"image/png"
714-
} else {
715-
"application/octet-stream"
716-
},
717-
)?;
703+
let part = {
704+
let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?);
705+
if let Some(filename) = file.file_name() {
706+
part = part
707+
.file_name(filename.to_string_lossy().to_string())
708+
.mime_str(if filename.to_string_lossy().ends_with("png") {
709+
"image/png"
710+
} else {
711+
"application/octet-stream"
712+
})?;
713+
} else {
714+
part = part.mime_str("application/octet-stream")?;
715+
}
716+
part
717+
};
718718
local_var_form = local_var_form.part("file", part);
719719
local_var_form = local_var_form.text("tag", tag.to_string());
720720
if let Some(local_var_param_value) = animation_style {

src/apis/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct ResponseContent<T> {
1010

1111
#[derive(Debug)]
1212
pub enum Error<T> {
13+
AsyncStdIo(::async_std::io::Error),
1314
Reqwest(reqwest::Error),
1415
Serde(serde_json::Error),
1516
Io(std::io::Error),
@@ -19,6 +20,7 @@ pub enum Error<T> {
1920
impl<T> fmt::Display for Error<T> {
2021
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2122
let (module, e) = match self {
23+
Error::AsyncStdIo(e) => ("async_std", e.to_string()),
2224
Error::Reqwest(e) => ("reqwest", e.to_string()),
2325
Error::Serde(e) => ("serde", e.to_string()),
2426
Error::Io(e) => ("IO", e.to_string()),
@@ -31,6 +33,7 @@ impl<T> fmt::Display for Error<T> {
3133
impl<T: fmt::Debug> error::Error for Error<T> {
3234
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
3335
Some(match self {
36+
Error::AsyncStdIo(e) => e,
3437
Error::Reqwest(e) => e,
3538
Error::Serde(e) => e,
3639
Error::Io(e) => e,

0 commit comments

Comments
 (0)