Skip to content

Commit 3707a51

Browse files
author
Joshua Tracey
committed
merge!(cleanup): tidyup examples to work after merge and bump to 0.7.0
1 parent 91e22b8 commit 3707a51

11 files changed

Lines changed: 110 additions & 46 deletions

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gemini_client_rs"
3-
version = "0.6.2"
3+
version = "0.7.0"
44
edition = "2021"
55
description = "A Rust client for Google Gemini API"
66
license = "MIT"

examples/basic.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3434
let response = client.generate_content(model_name, &request).await?;
3535

3636
for candidate in &response.candidates {
37-
for part in &candidate.content.parts {
38-
match &part.data {
39-
ContentData::Text(text) => println!("{text}"),
40-
_ => { /* Ignore other part types as we are not using tools */ }
37+
if let Some(content_data) = &candidate.content {
38+
for part in &content_data.parts {
39+
match &part.data {
40+
ContentData::Text(text) => {
41+
println!("Text: {}", text);
42+
}
43+
_ => {
44+
println!("Unsupported content data type");
45+
}
46+
}
4147
}
48+
} else {
49+
println!("No content data");
4250
}
4351
}
4452

examples/code_execution.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
.await?;
4242

4343
for candidate in &response.candidates {
44-
for part in &candidate.content.parts {
45-
match &part.data {
46-
ContentData::Text(text) => println!("{text}"),
47-
_ => { /* Ignore other part types as we are not using tools */ }
44+
if let Some(content_data) = &candidate.content {
45+
for part in &content_data.parts {
46+
match &part.data {
47+
ContentData::Text(text) => {
48+
println!("Text: {}", text);
49+
}
50+
_ => {
51+
println!("Unsupported content data type");
52+
}
53+
}
4854
}
55+
} else {
56+
println!("No content data");
4957
}
5058
}
5159

examples/custom_tool.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7575

7676
let first_candidate = response.candidates.first().unwrap();
7777

78-
let first_part = first_candidate.content.parts.first().unwrap();
79-
80-
let weather = match &first_part.data {
81-
ContentData::Text(text) => text,
82-
ContentData::FunctionCall(_) => "Function call found",
83-
ContentData::FunctionResponse(_) => "Function response found",
84-
ContentData::ExecutableCode(_) => "Executable code found",
85-
ContentData::CodeExecutionResult(_) => "Code execution result found",
86-
ContentData::InlineData(_) => "Inline data found",
87-
ContentData::FileData(_) => "File data found",
88-
};
89-
90-
println!("{weather}");
78+
if let Some(content_data) = &first_candidate.content {
79+
for part in &content_data.parts {
80+
match &part.data {
81+
ContentData::Text(text) => {
82+
println!("Text: {}", text);
83+
}
84+
_ => {
85+
println!("Unsupported content data type");
86+
}
87+
}
88+
}
89+
} else {
90+
println!("No content data");
91+
}
9192

9293
Ok(())
9394
}

examples/generation_config.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
let response = client.generate_content(model_name, &request).await?;
5050

5151
for candidate in &response.candidates {
52-
for part in &candidate.content.parts {
53-
match &part.data {
54-
ContentData::Text(text) => println!("Response: {text}"),
55-
_ => println!("Unexpected response type"),
52+
if let Some(content_data) = &candidate.content {
53+
for part in &content_data.parts {
54+
match &part.data {
55+
ContentData::Text(text) => {
56+
println!("Text: {}", text);
57+
}
58+
_ => {
59+
println!("Unsupported content data type");
60+
}
61+
}
5662
}
63+
} else {
64+
println!("No content data");
5765
}
5866
}
5967

examples/grounded.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4646
.await?;
4747

4848
for candidate in &response.candidates {
49-
for part in &candidate.content.parts {
50-
match &part.data {
51-
ContentData::Text(text) => println!("{text}"),
52-
_ => { /* Ignore other part types as we are not using tools */ }
49+
if let Some(content_data) = &candidate.content {
50+
for part in &content_data.parts {
51+
match &part.data {
52+
ContentData::Text(text) => {
53+
println!("Text: {}", text);
54+
}
55+
_ => {
56+
println!("Unsupported content data type");
57+
}
58+
}
5359
}
60+
} else {
61+
println!("No content data");
5462
}
5563
}
5664

examples/grounded_2.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
.await?;
4242

4343
for candidate in &response.candidates {
44-
for part in &candidate.content.parts {
45-
match &part.data {
46-
ContentData::Text(text) => println!("{text}"),
47-
_ => { /* Ignore other part types as we are not using tools */ }
44+
if let Some(content_data) = &candidate.content {
45+
for part in &content_data.parts {
46+
match &part.data {
47+
ContentData::Text(text) => {
48+
println!("Text: {}", text);
49+
}
50+
_ => {
51+
println!("Unsupported content data type");
52+
}
53+
}
4854
}
55+
} else {
56+
println!("No content data");
4957
}
5058
}
5159

examples/inline_data.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4242
let response = client.generate_content(model_name, &request).await?;
4343

4444
for candidate in &response.candidates {
45-
for part in &candidate.content.parts {
46-
match &part.data {
47-
ContentData::Text(text) => println!("{text}"),
48-
_ => { /* Ignore other part types as we are not using tools */ }
45+
if let Some(content_data) = &candidate.content {
46+
for part in &content_data.parts {
47+
match &part.data {
48+
ContentData::Text(text) => {
49+
println!("Text: {}", text);
50+
}
51+
_ => {
52+
println!("Unsupported content data type");
53+
}
54+
}
4955
}
56+
} else {
57+
println!("No content data");
5058
}
5159
}
5260

examples/system_instruction.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4545
let response = client.generate_content(model_name, &request).await?;
4646

4747
for candidate in &response.candidates {
48-
for part in &candidate.content.parts {
49-
match &part.data {
50-
ContentData::Text(text) => println!("{text}"),
51-
_ => { /* Ignore other part types as we are not using tools */ }
48+
if let Some(content_data) = &candidate.content {
49+
for part in &content_data.parts {
50+
match &part.data {
51+
ContentData::Text(text) => {
52+
println!("Text: {}", text);
53+
}
54+
_ => {
55+
println!("Unsupported content data type");
56+
}
57+
}
5258
}
59+
} else {
60+
println!("No content data");
5361
}
5462
}
5563

0 commit comments

Comments
 (0)