refactor: address review feedback on retry_delay (#19)
All checks were successful
Rust / check (pull_request) Successful in 2m42s
All checks were successful
Rust / check (pull_request) Successful in 2m42s
- Drop the unreachable checked_mul in parse_duration; a fraction of at most 9 digits scaled to nanoseconds always fits in a u32. - Share a vertex_error test helper in error.rs instead of building VertexApiError inline. - Bump version to 0.2.0 for the breaking VertexApiError.details change.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "gemini-rs"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
23
src/error.rs
23
src/error.rs
@@ -116,16 +116,18 @@ impl Error {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn vertex_error_retryable_classification() {
|
||||
let make_err = |code: i32, status: &str| {
|
||||
fn vertex_error(code: i32, status: &str, details: Option<Vec<serde_json::Value>>) -> Error {
|
||||
Error::VertexError(types::VertexApiError {
|
||||
code,
|
||||
message: "test".to_string(),
|
||||
status: status.to_string(),
|
||||
details: None,
|
||||
details,
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertex_error_retryable_classification() {
|
||||
let make_err = |code: i32, status: &str| vertex_error(code, status, None);
|
||||
|
||||
// Retryable codes
|
||||
assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable());
|
||||
@@ -167,15 +169,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn retry_delay_delegates_to_vertex_error() {
|
||||
let err = Error::VertexError(types::VertexApiError {
|
||||
code: 429,
|
||||
message: "Quota exceeded".to_string(),
|
||||
status: "RESOURCE_EXHAUSTED".to_string(),
|
||||
details: Some(vec![serde_json::json!({
|
||||
let err = vertex_error(
|
||||
429,
|
||||
"RESOURCE_EXHAUSTED",
|
||||
Some(vec![serde_json::json!({
|
||||
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||
"retryDelay": "15s"
|
||||
})]),
|
||||
});
|
||||
);
|
||||
assert_eq!(err.retry_delay(), Some(Duration::from_secs(15)));
|
||||
|
||||
assert_eq!(Error::NoCandidatesError.retry_delay(), None);
|
||||
|
||||
@@ -92,7 +92,7 @@ fn parse_duration(s: &str) -> Option<Duration> {
|
||||
return None;
|
||||
}
|
||||
let parsed: u32 = frac.parse().ok()?;
|
||||
parsed.checked_mul(10u32.pow(9 - frac.len() as u32))?
|
||||
parsed * 10u32.pow(9 - frac.len() as u32)
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user