Skip to content

Commit fa3fd56

Browse files
committed
chore: format code
1 parent 0d70f0a commit fa3fd56

File tree

6 files changed

+65
-42
lines changed

6 files changed

+65
-42
lines changed

.rustfmt.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
2+
3+
edition = "2024"
4+
tab_spaces = 2
5+
# empty_item_single_line = true # nightly
6+
# group_imports = "StdExternalCrate" # nightly
7+
# imports_granularity = "Crate" # nightly
8+
# imports_layout = "HorizontalVertical" # nightly
9+
# match_arm_blocks = false # nightly
10+
match_block_trailing_comma = true
11+
# normalize_comments = true # nightly
12+
# normalize_doc_attributes = true # nightly
13+
# overflow_delimited_expr = true # nightly
14+
# reorder_impl_items = true # nightly
15+
# spaces_around_ranges = true # nightly
16+
# unstable_features = true # nightly
17+
use_field_init_shorthand = true
18+
use_try_shorthand = true

crates/provider_openai/src/speech.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use axum::{body::Bytes, http::{header, HeaderMap}};
1+
use axum::{
2+
body::Bytes,
3+
http::{HeaderMap, header},
4+
};
25
use reqwest::Client;
36
use serde::Serialize;
4-
use unspeech_shared::{
5-
AppError,
6-
speech::ProcessedSpeechOptions,
7-
};
7+
use unspeech_shared::{AppError, speech::ProcessedSpeechOptions};
88

99
#[derive(Serialize)]
1010
// https://platform.openai.com/docs/api-reference/audio/createSpeech
@@ -40,21 +40,26 @@ pub async fn handle(
4040
speed: options.speed,
4141
};
4242

43-
let res = client.post("https://api.openai.com/v1/audio/speech")
43+
let res = client
44+
.post("https://api.openai.com/v1/audio/speech")
4445
.bearer_auth(token)
4546
.json(&body)
4647
.send()
4748
.await?;
4849

4950
if !res.status().is_success() {
5051
let status = res.status();
51-
let body = res.text().await.unwrap_or_else(|err| format!("Could not read error body: {}", err));
52-
return Err(AppError::new(anyhow::anyhow!("API request failed with status: {}\nBody: {}", status, body), None));
52+
let body = res
53+
.text()
54+
.await
55+
.unwrap_or_else(|err| format!("Could not read error body: {}", err));
56+
return Err(AppError::new(
57+
anyhow::anyhow!("API request failed with status: {}\nBody: {}", status, body),
58+
None,
59+
));
5360
}
5461

55-
let bytes = res
56-
.bytes()
57-
.await?;
62+
let bytes = res.bytes().await?;
5863

5964
let mut headers = HeaderMap::new();
6065
// TODO: remove unwrap

crates/shared/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use axum::{
2+
Json,
23
http::StatusCode,
3-
response::{Response, IntoResponse},
4-
Json
4+
response::{IntoResponse, Response},
55
};
66
use serde::Serialize;
77

@@ -39,7 +39,7 @@ impl AppErrorResponse {
3939

4040
impl IntoResponse for AppError {
4141
fn into_response(self) -> Response {
42-
(self.status, Json(AppErrorResponse::new(self))).into_response()
42+
(self.status, Json(AppErrorResponse::new(self))).into_response()
4343
}
4444
}
4545

crates/shared/src/speech.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,21 @@ pub struct ProcessedSpeechOptions {
4747

4848
pub fn process_speech_options(options: SpeechOptions) -> Result<ProcessedSpeechOptions, AppError> {
4949
match options.model.split_once('/') {
50-
Some((provider, model)) if !provider.is_empty() && !model.is_empty() => Ok(ProcessedSpeechOptions {
51-
input: options.input,
52-
model: model.to_string(),
53-
voice: options.voice,
54-
instructions: options.instructions,
55-
response_format: options.response_format,
56-
speed: options.speed,
57-
extra: options.extra,
58-
provider: provider.to_string(),
59-
}),
60-
_ => Err(AppError::new(anyhow::anyhow!("Invalid model: {}", options.model), Some(StatusCode::BAD_REQUEST)))
50+
Some((provider, model)) if !provider.is_empty() && !model.is_empty() => {
51+
Ok(ProcessedSpeechOptions {
52+
input: options.input,
53+
model: model.to_string(),
54+
voice: options.voice,
55+
instructions: options.instructions,
56+
response_format: options.response_format,
57+
speed: options.speed,
58+
extra: options.extra,
59+
provider: provider.to_string(),
60+
})
61+
},
62+
_ => Err(AppError::new(
63+
anyhow::anyhow!("Invalid model: {}", options.model),
64+
Some(StatusCode::BAD_REQUEST),
65+
)),
6166
}
6267
}

src/main.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::time::Duration;
22

33
use axum::{
4-
routing::{get, post},
54
Router,
5+
routing::{get, post},
66
};
77
use reqwest::Client;
88

@@ -17,24 +17,21 @@ use unspeech_shared::AppError;
1717
async fn main() -> Result<(), AppError> {
1818
tracing_subscriber::fmt::init();
1919

20-
let client = Client::builder()
21-
.timeout(Duration::from_secs(60))
22-
.build()?;
20+
let client = Client::builder().timeout(Duration::from_secs(60)).build()?;
2321

2422
let app = Router::new()
2523
.route("/", get(root))
2624
.route("/v1/audio/speech", post(speech))
2725
.with_state(client);
2826

29-
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
30-
.await?;
27+
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
3128

3229
tracing::debug!("listening on {}", listener.local_addr()?);
3330

3431
Ok(
3532
axum::serve(listener, app)
3633
.with_graceful_shutdown(shutdown_signal())
37-
.await?
34+
.await?,
3835
)
3936
}
4037

src/speech.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
use axum::{
2-
body::Bytes, debug_handler, extract::State, http::HeaderMap, Json
3-
};
1+
use axum::{Json, body::Bytes, debug_handler, extract::State, http::HeaderMap};
42
use axum_extra::{
5-
headers::{authorization::Bearer, Authorization},
6-
TypedHeader
3+
TypedHeader,
4+
headers::{Authorization, authorization::Bearer},
75
};
86
use reqwest::Client;
97
use unspeech_shared::{
108
AppError,
11-
speech::{
12-
SpeechOptions,
13-
process_speech_options
14-
}
9+
speech::{SpeechOptions, process_speech_options},
1510
};
1611

1712
#[debug_handler]
@@ -26,6 +21,9 @@ pub async fn speech(
2621
match options.provider.as_str() {
2722
#[cfg(feature = "openai")]
2823
"openai" => unspeech_provider_openai::speech::handle(options, client, token).await,
29-
_ => Err(AppError::new(anyhow::anyhow!("Unsupported provider: {}", options.provider), None)),
24+
_ => Err(AppError::new(
25+
anyhow::anyhow!("Unsupported provider: {}", options.provider),
26+
None,
27+
)),
3028
}
3129
}

0 commit comments

Comments
 (0)