Jump to content

Google Cloud Pub/Sub with the Rust SDK and Gemini CLI

From JOHNWICK
Revision as of 15:01, 14 November 2025 by PC (talk | contribs) (Created page with "This article leverages the Gemini CLI and the underlying Gemini LLM to develop native compiled code built in the Rust Language for using Pub/Sub messaging with Google Cloud. A minimally viable Pub/Sub connection is built in native Rust code for testing remote messaging to Google Cloud Pub/Sub using the official Google Cloud Rust SDK. What is Rust? Rust is a high performance, memory safe, compiled language: Rust A language empowering everyone to build reliable and effici...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This article leverages the Gemini CLI and the underlying Gemini LLM to develop native compiled code built in the Rust Language for using Pub/Sub messaging with Google Cloud. A minimally viable Pub/Sub connection is built in native Rust code for testing remote messaging to Google Cloud Pub/Sub using the official Google Cloud Rust SDK.

What is Rust? Rust is a high performance, memory safe, compiled language: Rust A language empowering everyone to build reliable and efficient software. www.rust-lang.org

Rust provides memory safe operations beyond C/C++ and also can provide exceptional performance gains as it is compiled directly to native binaries. Initial Environment Setup

The environment is meant to be run from a Bash like shell. You can run this from a Linux VM, ChromeOS Linux VM, Firebase Studio environment, or any environment that provides a basic shell. Rust Setup

Instructions to install Rust are available here: Getting started A language empowering everyone to build reliable and efficient software. www.rust-lang.org

For a Linux like environment the command looks like this: curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh

Gemini CLI If not pre-installed you can download the Gemini CLI to interact with the source files and provide real-time assistance: sudo npm install -g @google/gemini-cli

Note- if you are an a non standard environment — you will need to make sure to have at least Node version 20 available in order to run Gemini CLI. Testing the CLI Environment from the VM Once you have all the tools and the correct Node.js version in place- you can test the startup of Gemini CLI. You will need to authenticate with a Key or your Google Account:

Google Cloud Pub/Sub As part of the Google Cloud platform — extensive logging options are provided. Both system level and application level logs can be created. A summary of Google Cloud Pub/Sub is here: Pub/Sub documentation | Google Cloud Provides reliable, many-to-many, asynchronous messaging between applications. cloud.google.com

Official Google Cloud Rust SDK Google Cloud provides a supported version of the Rust SDK. This SDK allows making client calls in Rust to Google Cloud Endpoints natively from Rust code: GitHub — googleapis/google-cloud-rust: Google Cloud Platform Rust Client Libraries Google Cloud Platform Rust Client Libraries. Contribute to googleapis/google-cloud-rust development by creating an… github.com

A sample getting started guide is available here: Google Cloud Client Libraries for Rust The Google Cloud Client Libraries for Rust is a collection of Rust crates to interact with Google Cloud Services. This… googleapis.github.io

Rust Deployment Strategy Rust has not been the typical deployment path for applications using Google Cloud — so a step by step approach is used with validation at each milestone.

First Steps — Basic Google API Client Call in Rust The first step is to build a minimal Rust sample that makes a Google Cloud API Client call. The most common pattern for using the Google Cloud Rust SDK is to create a client- then build the arguments matching the correct types, and finally perform the client SDK call to the Google Cloud backend service.

Clone the Samples Repo and Setup the Basic Environment Now that the Gemini CLI and the Rust installation has been validated from the base shell — you can clone the GitHub Repo with support scripts: cd ~ git clone https://github.com/xbill9/gemini-cli-codeassist

Then run init.sh from the cloned directory. The script will attempt to determine your shell environment and set the correct variables: cd gemini-cli-codeassist source init.sh

Setup Pub/Sub Environment

A sample script is provided to create a sample topic and set the required environment variables for connecting to Pub/Sub: cd gemini-cli-codeassist/pubsub-client-rust source pubsub.sh

Debugging API Permission Errors

If your application default credentials expires or your Google Cloud Authentication expires you will get an error similar to this: Finished `dev` profile [unoptimized + debuginfo] target(s) in 27.94s

    Running `target/debug/pubsub-client-rust`

Starting client API call Project comglitn Error: Error { kind: Authentication, source: Some(CredentialsError { is_transient: false, message: Some("Request to fetch the token failed. Subsequent calls with this credential will also fail."), source: Some(Error { kind: Authentication, source: Some(CredentialsError { is_transient: false, message: Some("failed to refresh user access token, body=<{\n \"error\": \"invalid_grant\",\n \"error_description\": \"reauth related error (invalid_rapt)\",\n \"error_uri\": \"https://support.google.com/a/answer/9368756\",\n \"error_subtype\": \"invalid_rapt\"\n}>"), source: Some(reqwest::Error { kind: Status(400, None), url: "https://oauth2.googleapis.com/token" }) }) }) }) }

The workaround is to re-authenticate: gcloud auth login gcloud auth application-default login

Another common error is that the environment variables are not set correctly. Go the the root directory and re-run the pubsub.sh to set the variables: cd ~/gemini-cli-codeassist/pubsub-client-rust source pubsub.sh

Rust Debugging and Logging

The environment variable RUST_LOG controls how verbose the logging from the Rust code will produce messages. Initially — set the variable to info: export RUST_LOG=info echo $RUST_LOG info

Rust Log Levels

These levels filter messages based on their severity, from most critical to least:

  • ERROR: Only error messages are logged.
  • WARN: Warning messages and higher (error) are logged.
  • INFO: Informational messages and higher (warn, error) are logged.
  • DEBUG: Debug messages and higher (info, warn, error) are logged.
  • TRACE: Trace messages and higher (debug, info, warn, error) are logged. This is the most verbose level.
  • OFF: Disables all logging.

Next Steps- Build Rust Sample Client Code

A basic native Rust client sample the makes a direct connection to Google Cloud Logging has been included in the GitHub repo. Change to the sample directory and validate the Cargo.toml file for the minimal sample: cd ~/gemini-cli-codeassist/pubsub-client-rust pwd /home/xbill/gemini-cli-codeassist/pubsub-client-rust

The Rust standard format file Cargo.toml contains details about the code and the required Rust crates: [package] name = "pubsub-client-rust" version = "0.1.0" edition = "2024" description = "Minimal Rust pubsub client call to Google Cloud" license = "MIT" repository = "https://github.com/xbill9/gemini-cli-codeassist"


[dependencies] serde = { version = "^1.0.219", features = ["derive"] } google-cloud-pubsub = "^0.30.0" tokio = { version = "^1.37.0", features = ["full"] } tracing = "0.1.40" anyhow = "1.0.86" log = "0.4.21" pretty_env_logger = "0.5.0" env_logger = "0.11.3"

Build the GCP Pub/sub client sample with the default Makefile: xbill@penguin:~/gemini-cli-codeassist/pubsub-client-rust$ make release Building Release...

   Finished `release` profile [optimized] target(s) in 0.08s

xbill@penguin:~/gemini-cli-codeassist/pubsub-client-rust$

Then run the generated Rust binary: export RUST_LOG=info ./target/release/pubsub-client-rust

At the info logging level — basic information is returned from the Google Cloud Pub/Sub topics: xbill@penguin:~/gemini-cli-codeassist/pubsub-client-rust$ ./target/release/pubsub-client-rust

INFO  pubsub_client_rust > Starting pubsub client
INFO  pubsub_client_rust > Fetching topics for project `comglitn`
INFO  pubsub_client_rust > Found 11 topics in project `comglitn`:
INFO  pubsub_client_rust >   - projects/comglitn/topics/glitnir
INFO  pubsub_client_rust >   - projects/comglitn/topics/repository-changes.default
INFO  pubsub_client_rust >   - projects/comglitn/topics/cloud-builds
INFO  pubsub_client_rust >   - projects/comglitn/topics/container-analysis-occurrences-v1
INFO  pubsub_client_rust >   - projects/comglitn/topics/container-analysis-notes-v1beta1
INFO  pubsub_client_rust >   - projects/comglitn/topics/container-analysis-occurrences-v1beta1
INFO  pubsub_client_rust >   - projects/comglitn/topics/container-analysis-notes-v1
INFO  pubsub_client_rust >   - projects/comglitn/topics/stop-instance-event
INFO  pubsub_client_rust >   - projects/comglitn/topics/start-instance-event
INFO  pubsub_client_rust >   - projects/comglitn/topics/transfer
INFO  pubsub_client_rust >   - projects/comglitn/topics/my-topic
INFO  pubsub_client_rust > Pubsub client finished successfully

xbill@penguin:~/gemini-cli-codeassist/pubsub-client-rust$

In this example 11 topics were found and listed by the Rust client code. Validate the Pub/Sub Topics

Go to the Google Cloud console and search on Pub/Sub to verify the list of topics retrieved by the Rust client:

The count of active topics on the Google Cloud console match the count returned by the Rust client code. You can also drill into the sample topic — mytopic which was created from the setup scripts :

Use Gemini CLI to Validate the Topics Gemini CLI can also be used to cross check the topics returned to the Rust client with an equivalent gcloud CLI command:

This also validates that the client returned 11 Pub/Sub Topics which matches the count returned in the native Rust client. Package Information on crates.io

The complete pubsub-client-rust is available on the Rust crates.io site: crates.io: Rust Package Registry Edit description crates.io


Summary A complete Rust native Pub/Sub client was built and tested using the Gemini CLI along with Rust libraries for a direct connection to Google Cloud Pub/Sub. This compiled Rust code was then called directly from the local shell and performed client operations using the Google Cloud SDK for Rust. Then, the Google Cloud console and Gemini CLI were used to validate that the count of Google Cloud Topics matched the output from the Rust client code. The overall goal was to validate the Rust client SDK libraries with Pub/Sub and the environment needed to successfully connect to Google Cloud from Rust native compiled code. Next Steps Further operations can be built and tested from the Rust client code — including creating topics, sending messages, and reading queued messages.