Enhanced Protocol Buffer types with additional functionality for runtime use
The bomboni_proto crate provides enhanced implementations of Google’s well-known Protocol Buffer types with additional functionality beyond the standard prost-types.
Use from_msg to convert any protobuf message to an Any type:
use bomboni_proto::google::protobuf::Any;use bomboni_proto::google::rpc::ErrorInfo;let error_info = ErrorInfo { reason: "INVALID_ARGUMENT".to_string(), domain: "my.api".to_string(), metadata: Default::default(),};// Convert to Any with automatic type URLlet any_msg = Any::from_msg(&error_info).unwrap();assert_eq!(any_msg.type_url, "type.googleapis.com/google.rpc.ErrorInfo");
For convenience, you can implement automatic TryFrom conversions:
use bomboni_proto::google::protobuf::Any;use bomboni_proto::google::rpc::ErrorInfo;use bomboni_proto::impl_proto_any_convert;// Implement bidirectional conversionsimpl_proto_any_convert![ErrorInfo];// Now you can use TryFromlet error_info = ErrorInfo::default();let any_msg: Any = error_info.try_into().unwrap();let decoded: ErrorInfo = any_msg.try_into().unwrap();
The Code enum provides all standard gRPC status codes:
use bomboni_proto::google::rpc::Code;match result { Ok(_) => Code::Ok, Err(e) if e.is_not_found() => Code::NotFound, Err(e) if e.is_invalid() => Code::InvalidArgument, Err(e) if e.is_unauthorized() => Code::Unauthenticated, Err(_) => Code::Internal,}
use bomboni_proto::google::protobuf::FieldMask;let mask = FieldMask { paths: vec![ "user.name".to_string(), "user.email".to_string(), ],};// Or use From traitlet mask = FieldMask::from(vec!["user.name", "user.email"]);
use bomboni_proto::google::protobuf::FieldMask;let mask = FieldMask::new(vec![ "user.profile".to_string(), "user.settings".to_string(),]);// Check if mask contains a pathassert!(mask.contains("user.profile"));assert!(!mask.contains("user.name"));// Check if mask covers a nested fieldassert!(mask.masks("user.profile.avatar")); // true - parent is maskedassert!(!mask.masks("user.name")); // false - not in mask
The masks method checks if a field path is covered by any of the mask’s paths, including parent paths.
Enable integration with the tonic gRPC library. Provides conversions between Bomboni’s Status and tonic’s Status, as well as HTTP status code conversions.