Skip to main content

protify/
service.rs

1use crate::*;
2
3/// A trait that enables the generation of a protobuf service schema.
4///
5/// Implemented by the enums annotated with the [`proto_service`] macro.
6pub trait ProtoService {
7	fn proto_schema() -> Service;
8}
9
10/// A struct representing a protobuf service.
11#[derive(Debug, PartialEq, Builder)]
12#[cfg_attr(feature = "std", derive(Template))]
13#[cfg_attr(feature = "std", template(path = "service.proto.j2"))]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[non_exhaustive]
16pub struct Service {
17	pub name: FixedStr,
18	pub file: FixedStr,
19	pub options: Vec<ProtoOption>,
20	pub handlers: Vec<ServiceHandler>,
21	pub package: FixedStr,
22}
23
24impl Service {
25	/// Renders the schema representation.
26	#[cfg(feature = "std")]
27	pub fn render_schema(&self) -> Result<String, askama::Error> {
28		self.render()
29	}
30}
31
32/// A struct that represents a protobuf service handler.
33#[derive(Debug, PartialEq, Builder)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35#[non_exhaustive]
36pub struct ServiceHandler {
37	pub name: FixedStr,
38	pub options: Vec<ProtoOption>,
39	pub request: ProtoPath,
40	pub response: ProtoPath,
41}