Skip to main content

udb_client/
lib.rs

1//! Rust client for [UDB](https://github.com/fahara02/udb) — a proto-driven gRPC
2//! broker over multiple databases.
3//!
4//! ```no_run
5//! use udb_client::{Metadata, UdbClient};
6//!
7//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
8//! let meta = Metadata::new("tenant-1")
9//!     .with_project("default")
10//!     .with_bearer_token(std::env::var("UDB_TOKEN")?);
11//!
12//! let mut udb = UdbClient::connect("http://127.0.0.1:50051", meta).await?;
13//! let set = udb
14//!     .select(udb_client::proto::udb::entity::v1::SelectRequest {
15//!         message_type: "myapp.v1.Invoice".into(),
16//!         ..Default::default()
17//!     })
18//!     .await?;
19//! println!("{} row(s)", set.rows.len());
20//! # Ok(())
21//! # }
22//! ```
23//!
24//! # Two listeners, two authorization models
25//!
26//! UDB serves its data plane and its native services on SEPARATE listeners with
27//! DIFFERENT authorization models — the data plane authorizes through Casbin,
28//! the native services through scope-based endpoint security. A credential
29//! accepted by one is not automatically accepted by the other, and the mismatch
30//! surfaces as a permissions error rather than a wrong-address error. [`UdbClient`]
31//! speaks to the data plane.
32//!
33//! # Generated types
34//!
35//! The stubs under [`proto`] are generated at build time from the protos rather
36//! than committed, so this crate cannot drift from the contract it ships with.
37//! They are laid out by proto package: `udb.entity.v1` is
38//! [`proto::udb::entity::v1`].
39
40#![forbid(unsafe_code)]
41#![warn(missing_debug_implementations)]
42// `tonic::Status` is a large error type, and clippy flags every fallible gRPC
43// call for it. Boxing it would make this client's errors differ from the type
44// every tonic user already handles, which is a worse trade than the move cost.
45#![allow(clippy::result_large_err)]
46
47pub mod auth;
48pub mod client;
49pub mod error;
50/// The RPC registry generated from the proto descriptor set.
51///
52/// Regenerate with `udb sdk generate --lang rust --out sdk`; CI fails if the
53/// committed copy differs from what the descriptor produces.
54pub mod generated_rpcs;
55pub mod metadata;
56
57/// Generated protobuf and tonic client types, nested by proto package.
58///
59/// The module tree is emitted by `build.rs` from the packages tonic actually
60/// generated, so adding a service to the contract does not require editing a
61/// hand-maintained list here.
62pub mod proto {
63    // Generated code: hold it to its own standards, not ours.
64    #![allow(clippy::all, clippy::pedantic, rustdoc::all)]
65    #![allow(missing_debug_implementations)]
66    include!(concat!(env!("OUT_DIR"), "/udb_modules.rs"));
67}
68
69pub use auth::{Token, TokenManager};
70pub use client::UdbClient;
71pub use error::{CallPolicy, UdbError};
72pub use generated_rpcs::{is_retry_safe, spec_for_path, RpcSpec};
73pub use metadata::Metadata;