| Crates.io | texnouz-contracts |
| lib.rs | texnouz-contracts |
| version | 0.1.2 |
| created_at | 2026-01-10 06:31:47.367878+00 |
| updated_at | 2026-01-10 06:31:47.367878+00 |
| description | gRPC contracts library with auto-generated Rust code from proto files |
| homepage | |
| repository | https://github.com/ali-0211-tr/contracts |
| max_upload_size | |
| id | 2033598 |
| size | 165,699 |
A Rust library for gRPC contracts with auto-generated code from Protocol Buffer definitions using tonic and prost.
.proto filesAdd this to your Cargo.toml:
[dependencies]
contracts = "0.1.0"
Or install from GitHub:
[dependencies]
contracts = { git = "https://github.com/yourusername/contracts" }
.proto files to the proto/ directorycargo build to generate Rust codeExample proto file structure:
proto/
├── user.proto
├── auth.proto
└── common.proto
use contracts::generated;
use contracts::tonic;
// Use generated types and services
// The generated modules will be available based on your proto package names
// Example: Using helpers
use contracts::utils::{current_timestamp, add_auth_token};
fn main() {
let timestamp = current_timestamp();
println!("Current timestamp: {:?}", timestamp);
}
After building, generated code will be included in the generated module. To use specific proto packages:
// In your lib.rs, add:
pub mod generated {
tonic::include_proto!("user");
tonic::include_proto!("auth");
tonic::include_proto!("common");
}
use contracts::generated::user::user_service_client::UserServiceClient;
use contracts::generated::user::GetUserRequest;
use contracts::tonic::transport::Channel;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to the server
let channel = Channel::from_static("http://[::1]:50051")
.connect()
.await?;
let mut client = UserServiceClient::new(channel);
// Make a request
let request = tonic::Request::new(GetUserRequest {
id: "123".to_string(),
});
let response = client.get_user(request).await?;
println!("Response: {:?}", response.into_inner());
Ok(())
}
use contracts::generated::user::{
user_service_server::{UserService, UserServiceServer},
GetUserRequest, GetUserResponse, User,
};
use contracts::tonic::{transport::Server, Request, Response, Status};
#[derive(Debug, Default)]
pub struct MyUserService {}
#[tonic::async_trait]
impl UserService for MyUserService {
async fn get_user(
&self,
request: Request<GetUserRequest>,
) -> Result<Response<GetUserResponse>, Status> {
let req = request.into_inner();
// Your business logic here
let user = User {
id: req.id,
username: "john_doe".to_string(),
email: "john@example.com".to_string(),
full_name: "John Doe".to_string(),
created_at: Some(contracts::current_timestamp()),
updated_at: Some(contracts::current_timestamp()),
is_active: true,
role: "user".to_string(),
};
Ok(Response::new(GetUserResponse { user: Some(user) }))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let service = MyUserService::default();
Server::builder()
.add_service(UserServiceServer::new(service))
.serve(addr)
.await?;
Ok(())
}
protoc)Install protoc:
Ubuntu/Debian:
sudo apt-get install protobuf-compiler
macOS:
brew install protobuf
Windows: Download from GitHub releases
cargo build
This will:
.proto files in the proto/ directorycargo test
cargo fmt
cargo clippy
contracts/
├── .github/
│ └── workflows/
│ ├── generate-proto.yml # Auto-generate code on proto changes
│ └── publish.yml # Publish to crates.io
├── proto/ # Proto definition files
│ ├── user.proto
│ ├── auth.proto
│ └── common.proto
├── src/
│ └── lib.rs # Main library file
├── build.rs # Build script for code generation
├── Cargo.toml
└── README.md
This project includes two GitHub Actions workflows:
generate-proto.yml)Automatically runs on:
main or develop branchesmain or developActions:
publish.yml)Runs on:
Actions:
CARGO_REGISTRY_TOKEN secret)Cargo.tomlREADME.md and CHANGELOG.mdgit tag v0.1.0
git push origin v0.1.0
Or manually:
cargo publish
The library can also be used directly from GitHub:
[dependencies]
contracts = { git = "https://github.com/yourusername/contracts", tag = "v0.1.0" }
default: Standard featuresserde: Enable serde serialization for generated typesEnable serde support:
[dependencies]
contracts = { version = "0.1.0", features = ["serde"] }
The build.rs script can be customized:
tonic_build::configure()
.build_server(true) // Generate server code
.build_client(true) // Generate client code
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile(&proto_files, &["proto"])?;
The library includes helpful utilities:
use contracts::utils::*;
// Add authentication token to requests
let request = add_auth_token(request, "your-token");
// Get current timestamp
let timestamp = current_timestamp();
// Validate required fields
let value = require_field(optional_value, "field_name")?;
See the examples directory for complete working examples:
client.rs - gRPC client exampleserver.rs - gRPC server exampleContributions are welcome! Please:
This project is licensed under MIT OR Apache-2.0.