5 Commits

Author SHA1 Message Date
a49d37f03d refactor: address review feedback on retry_delay (#19)
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.
2026-10-28 08:44:53 +00:00
cecc14d31e feat: expose Gemini RetryInfo retry_delay() on VertexApiError and Error (#19)
All checks were successful
Rust / check (pull_request) Successful in 2m40s
VertexApiError.details is now Option<Vec<serde_json::Value>> so that
unknown google.rpc.* detail types (e.g. RetryInfo, QuotaFailure) no
longer break deserialization and fall back to Error::HttpStatus. The
unused ErrorType, ErrorInfoMetadata, FieldViolation and Link types are
removed.

Add VertexApiError::retry_delay() which parses the protobuf duration in a
google.rpc.RetryInfo entry, and Error::retry_delay() which delegates to it.

Closes #19
2026-10-28 08:38:02 +00:00
4791125943 Merge pull request 'ci: run fmt, clippy and tests on the Gitea runner' (#18) from ci/gitea-actions into main
All checks were successful
Rust / check (push) Successful in 2m39s
Reviewed-on: #18
2026-10-26 20:42:05 +00:00
6d49f9939c ci: install Rust on the Gitea runner and check fmt and clippy
All checks were successful
Rust / check (pull_request) Successful in 5m14s
The Gitea runner image doesn't include Rust, so every job failed at
'cargo: command not found'. Install the stable toolchain, cache the
build, and run cargo fmt --check and clippy alongside the tests.
2026-10-26 20:34:40 +00:00
c911fc382e style: run cargo fmt on examples 2026-10-26 20:34:40 +00:00
8 changed files with 101 additions and 88 deletions

View File

@@ -10,13 +10,19 @@ env:
CARGO_TERM_COLOR: always CARGO_TERM_COLOR: always
jobs: jobs:
build: check:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Build # The Gitea runner image doesn't ship with Rust, unlike GitHub's.
run: cargo build --verbose - uses: dtolnay/rust-toolchain@stable
- name: Run tests with:
run: cargo test --verbose components: clippy, rustfmt
- uses: Swatinem/rust-cache@v2
- name: Format
run: cargo fmt --check
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
- name: Test
run: cargo test

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "gemini-rs" name = "gemini-rs"
version = "0.1.0" version = "0.2.0"
edition = "2024" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -1,5 +1,5 @@
use console::style; use console::style;
use dialoguer::{theme::ColorfulTheme, Input}; use dialoguer::{Input, theme::ColorfulTheme};
use gemini_rs::prelude::*; use gemini_rs::prelude::*;
use std::time::Duration; use std::time::Duration;

View File

@@ -19,10 +19,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.add_text_part("Answer as if you were Yoda") .add_text_part("Answer as if you were Yoda")
.build(); .build();
let user_prompt = vec![Content::builder() let user_prompt = vec![
.role(Role::User) Content::builder()
.add_text_part("What is the airspeed of an unladen swallow?") .role(Role::User)
.build()]; .add_text_part("What is the airspeed of an unladen swallow?")
.build(),
];
let request = GenerateContentRequest::builder() let request = GenerateContentRequest::builder()
.contents(user_prompt) .contents(user_prompt)

View File

@@ -15,10 +15,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
location_id, location_id,
); );
let prompt = vec![Content::builder() let prompt = vec![
.role(Role::User) Content::builder()
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.") .role(Role::User)
.build()]; .add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
.build(),
];
let request = GenerateContentRequest::builder().contents(prompt).build(); let request = GenerateContentRequest::builder().contents(prompt).build();

View File

@@ -14,10 +14,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
location_id, location_id,
); );
let prompt = vec![Content::builder() let prompt = vec![
.role(Role::User) Content::builder()
.add_text_part("What is the airspeed of an unladen swallow?") .role(Role::User)
.build()]; .add_text_part("What is the airspeed of an unladen swallow?")
.build(),
];
let request = GenerateContentRequest::builder().contents(prompt).build(); let request = GenerateContentRequest::builder().contents(prompt).build();
let response = gemini.generate_content(&request, "gemini-pro").await?; let response = gemini.generate_content(&request, "gemini-pro").await?;

View File

@@ -116,16 +116,18 @@ impl Error {
mod tests { mod tests {
use super::*; use super::*;
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,
})
}
#[test] #[test]
fn vertex_error_retryable_classification() { fn vertex_error_retryable_classification() {
let make_err = |code: i32, status: &str| { let make_err = |code: i32, status: &str| vertex_error(code, status, None);
Error::VertexError(types::VertexApiError {
code,
message: "test".to_string(),
status: status.to_string(),
details: None,
})
};
// Retryable codes // Retryable codes
assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable()); assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable());
@@ -167,15 +169,14 @@ mod tests {
#[test] #[test]
fn retry_delay_delegates_to_vertex_error() { fn retry_delay_delegates_to_vertex_error() {
let err = Error::VertexError(types::VertexApiError { let err = vertex_error(
code: 429, 429,
message: "Quota exceeded".to_string(), "RESOURCE_EXHAUSTED",
status: "RESOURCE_EXHAUSTED".to_string(), Some(vec![serde_json::json!({
details: Some(vec![serde_json::json!({
"@type": "type.googleapis.com/google.rpc.RetryInfo", "@type": "type.googleapis.com/google.rpc.RetryInfo",
"retryDelay": "15s" "retryDelay": "15s"
})]), })]),
}); );
assert_eq!(err.retry_delay(), Some(Duration::from_secs(15))); assert_eq!(err.retry_delay(), Some(Duration::from_secs(15)));
assert_eq!(Error::NoCandidatesError.retry_delay(), None); assert_eq!(Error::NoCandidatesError.retry_delay(), None);

View File

@@ -92,7 +92,7 @@ fn parse_duration(s: &str) -> Option<Duration> {
return None; return None;
} }
let parsed: u32 = frac.parse().ok()?; 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, None => 0,
}; };