twurst_build/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#![doc = include_str!("../README.md")]
#![doc(test(attr(deny(warnings))))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

pub use prost_build as prost;
use prost_build::{Config, Module, Service, ServiceGenerator};
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use std::io::{Error, Result};
use std::path::{Path, PathBuf};
use std::{env, fs};

/// Builds protobuf bindings for Twirp.
///
/// Client and server are not enabled by defaults and must be enabled with the [`with_client`](Self::with_client) and [`with_server`](Self::with_server) methods.
#[derive(Default)]
pub struct TwirpBuilder {
    config: Config,
    generator: TwirpServiceGenerator,
    type_name_domain: Option<String>,
}

impl TwirpBuilder {
    /// Builder with the default prost [`Config`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Builder with a custom prost [`Config`].
    pub fn from_prost(config: Config) -> Self {
        Self {
            config,
            generator: TwirpServiceGenerator::new(),
            type_name_domain: None,
        }
    }

    /// Generates code for the Twirp client.
    pub fn with_client(mut self) -> Self {
        self.generator = self.generator.with_client();
        self
    }

    /// Generates code for the Twirp server.
    pub fn with_server(mut self) -> Self {
        self.generator = self.generator.with_server();
        self
    }

    /// Adds an extra parameter to generated server methods that implements [`axum::FromRequestParts`](https://docs.rs/axum/latest/axum/extract/trait.FromRequestParts.html).
    ///
    /// For example
    /// ```proto
    /// message Service {
    ///     rpc Test(TestRequest) returns (TestResponse) {}
    /// }
    /// ```
    /// Compiled with option `.with_axum_request_extractor("headers", "::axum::http::HeaderMap")`
    /// will generate the following code allowing to extract the request headers:
    /// ```ignore
    /// trait Service {
    ///     async fn test(request: TestRequest, headers: ::axum::http::HeaderMap) -> Result<TestResponse, TwirpError>;
    /// }
    /// ```
    ///
    /// Note that the parameter type must implement [`axum::FromRequestParts`](https://docs.rs/axum/latest/axum/extract/trait.FromRequestParts.html).
    pub fn with_axum_request_extractor(
        mut self,
        name: impl Into<String>,
        type_name: impl Into<String>,
    ) -> Self {
        self.generator = self.generator.with_axum_request_extractor(name, type_name);
        self
    }

    /// Customizes the type name domain.
    ///
    /// By default, 'type.googleapis.com' is used.
    pub fn with_type_name_domain(mut self, domain: impl Into<String>) -> Self {
        self.type_name_domain = Some(domain.into());
        self
    }

    /// Do compile the protos.
    pub fn compile_protos(
        mut self,
        protos: &[impl AsRef<Path>],
        includes: &[impl AsRef<Path>],
    ) -> Result<()> {
        let out_dir = PathBuf::from(
            env::var_os("OUT_DIR").ok_or_else(|| Error::other("OUT_DIR is not set"))?,
        );

        // We make sure the script is executed again if a file changed
        for proto in protos {
            println!("cargo:rerun-if-changed={}", proto.as_ref().display());
        }
        self.config
            .enable_type_names()
            .type_name_domain(
                ["."],
                self.type_name_domain
                    .as_deref()
                    .unwrap_or("type.googleapis.com"),
            )
            .service_generator(Box::new(self.generator));

        // We configure with prost reflect
        prost_reflect_build::Builder::new()
            .file_descriptor_set_bytes("self::FILE_DESCRIPTOR_SET_BYTES")
            .configure(&mut self.config, protos, includes)?;

        // We do the build itself while saving the list of modules
        let config = self.config.skip_protoc_run();
        let file_descriptor_set = config.load_fds(protos, includes)?;
        let modules = file_descriptor_set
            .file
            .iter()
            .map(|fd| Module::from_protobuf_package_name(fd.package()))
            .collect::<HashSet<_>>();

        // We check there is no streaming
        for file_descriptor in &file_descriptor_set.file {
            for service in &file_descriptor.service {
                for method in &service.method {
                    if method.client_streaming() {
                        return Err(Error::other(format!(
                            "Client streaming is not supported in method {} of service {} in file {}",
                            method.name(), service.name(), file_descriptor.name()
                        )));
                    }
                    if method.server_streaming() {
                        return Err(Error::other(format!(
                            "Server streaming is not supported in method {} of service {} in file {}",
                            method.name(), service.name(), file_descriptor.name()
                        )));
                    }
                }
            }
        }

        // We generate the files
        config.compile_fds(file_descriptor_set)?;

        // TODO(vsiles) consider proper AST parsing in case we need to do something
        // more robust
        //
        // prepare a regex to match `pub mod <module-name> {`
        let re = Regex::new(r"^(\s*)pub mod \w+ \{\s*$").expect("Failed to compile regex");

        // We add the file descriptor to every file to make reflection work automatically
        for module in modules {
            let file_path = Path::new(&out_dir).join(module.to_file_name_or("_"));
            if !file_path.exists() {
                continue; // We ignore not built files
            }
            let original_content = fs::read_to_string(&file_path)?;

            // scan for nested modules and insert the right FILE_DESCRIPTOR_SET_BYTES definition
            let mut modified_content = original_content
                .lines()
                .flat_map(|line| {
                    if let Some(captures) = re.captures(line) {
                        let indentation = captures.get(1).map_or("", |m| m.as_str());
                        vec![
                            line.to_string(),
                            // if there is no nested type, the next line would generate a warning
                            format!("    {}{}", indentation, "#[allow(unused_imports)]"),
                            format!(
                                "    {}{}",
                                indentation, "use super::FILE_DESCRIPTOR_SET_BYTES;"
                            ),
                        ]
                    } else {
                        vec![line.to_string()]
                    }
                })
                .collect::<Vec<_>>();

            modified_content.push("const FILE_DESCRIPTOR_SET_BYTES: &[u8] = include_bytes!(\"file_descriptor_set.bin\");\n".to_string());
            let file_content = modified_content.join("\n");

            fs::write(&file_path, &file_content)?;
        }

        Ok(())
    }
}

/// Low level generator for Twirp related code.
///
/// This only useful if you want to customize builds. For common use cases, please use [`TwirpBuilder`].
///
/// Should be given to [`Config::service_generator`].
///
/// Client and server are not enabled by defaults and must be enabled with the [`with_client`](Self::with_client) and [`with_server`](Self::with_server) methods.
#[derive(Default)]
struct TwirpServiceGenerator {
    client: bool,
    server: bool,
    request_extractors: HashMap<String, String>,
}

impl TwirpServiceGenerator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_client(mut self) -> Self {
        self.client = true;
        self
    }

    pub fn with_server(mut self) -> Self {
        self.server = true;
        self
    }

    pub fn with_axum_request_extractor(
        mut self,
        name: impl Into<String>,
        type_name: impl Into<String>,
    ) -> Self {
        self.request_extractors
            .insert(name.into(), type_name.into());
        self
    }
}

impl ServiceGenerator for TwirpServiceGenerator {
    fn generate(&mut self, service: Service, buf: &mut String) {
        self.do_generate(service, buf)
            .expect("failed to generate Twirp service")
    }
}

impl TwirpServiceGenerator {
    fn do_generate(&mut self, service: Service, buf: &mut String) -> std::fmt::Result {
        if self.client {
            writeln!(buf)?;
            for comment in &service.comments.leading {
                writeln!(buf, "/// {comment}")?;
            }
            if service.options.deprecated.unwrap_or(false) {
                writeln!(buf, "#[deprecated]")?;
            }
            writeln!(buf, "#[derive(Clone)]")?;
            writeln!(
                buf,
                "pub struct {}Client<C: ::twurst_client::TwirpHttpService> {{",
                service.name
            )?;
            writeln!(buf, "    client: ::twurst_client::TwirpHttpClient<C>")?;
            writeln!(buf, "}}")?;
            writeln!(buf)?;
            writeln!(
                buf,
                "impl<C: ::twurst_client::TwirpHttpService> {}Client<C> {{",
                service.name
            )?;
            writeln!(
                buf,
                "    pub fn new(client: impl Into<::twurst_client::TwirpHttpClient<C>>) -> Self {{"
            )?;
            writeln!(buf, "        Self {{ client: client.into() }}")?;
            writeln!(buf, "    }}")?;
            for method in &service.methods {
                for comment in &method.comments.leading {
                    writeln!(buf, "    /// {comment}")?;
                }
                if method.options.deprecated.unwrap_or(false) {
                    writeln!(buf, "#[deprecated]")?;
                }
                writeln!(
                    buf,
                    "    pub async fn {}(&self, request: &{}) -> Result<{}, ::twurst_client::TwirpError> {{",
                    method.name, method.input_type, method.output_type,
                )?;
                writeln!(
                    buf,
                    "        self.client.call(\"/{}.{}/{}\", request).await",
                    service.package, service.proto_name, method.proto_name,
                )?;
                writeln!(buf, "    }}")?;
            }
            writeln!(buf, "}}")?;
        }

        if self.server {
            writeln!(buf)?;
            for comment in &service.comments.leading {
                writeln!(buf, "/// {comment}")?;
            }
            writeln!(buf, "#[::twurst_server::codegen::trait_variant_make(Send)]")?;
            writeln!(buf, "pub trait {} {{", service.name)?;
            for method in &service.methods {
                for comment in &method.comments.leading {
                    writeln!(buf, "    /// {comment}")?;
                }
                write!(
                    buf,
                    "    async fn {}(&self, request: {}",
                    method.name, method.input_type
                )?;
                for (arg_name, arg_type) in &self.request_extractors {
                    write!(buf, ", {arg_name}: {arg_type}")?;
                }
                writeln!(
                    buf,
                    ") -> Result<{}, ::twurst_server::TwirpError>;",
                    method.output_type
                )?;
            }
            writeln!(buf)?;
            writeln!(
                buf,
                "    fn into_router<S: Clone + Send + Sync + 'static>(self) -> ::twurst_server::codegen::Router<S> where Self : Sized + Send + Sync + 'static {{"
            )?;
            writeln!(
                buf,
                "        ::twurst_server::codegen::TwirpRouter::new(::std::sync::Arc::new(self))"
            )?;
            for method in &service.methods {
                write!(
                    buf,
                    "            .route(\"/{}.{}/{}\", |service: ::std::sync::Arc<Self>, request: {}",
                    service.package, service.proto_name, method.proto_name, method.input_type,
                )?;
                if self.request_extractors.is_empty() {
                    write!(buf, ", _: ::twurst_server::codegen::RequestParts, _: S")?;
                } else {
                    write!(
                        buf,
                        ", mut parts: ::twurst_server::codegen::RequestParts, state: S",
                    )?;
                }
                write!(buf, "| {{")?;
                writeln!(buf, "                async move {{")?;
                write!(buf, "                    service.{}(request", method.name)?;
                for _ in self.request_extractors.values() {
                    write!(
                        buf,
                        ", match ::twurst_server::codegen::FromRequestParts::from_request_parts(&mut parts, &state).await {{ Ok(r) => r, Err(e) => {{ return Err(::twurst_server::codegen::twirp_error_from_response(e).await) }} }}"
                    )?;
                }
                writeln!(buf, ").await")?;
                writeln!(buf, "                }}")?;
                writeln!(buf, "            }})")?;
            }
            writeln!(buf, "            .build()")?;
            writeln!(buf, "    }}")?;

            if cfg!(feature = "grpc") {
                writeln!(buf)?;
                writeln!(
                    buf,
                    "    fn into_grpc_router(self) -> ::twurst_server::codegen::Router where Self : Sized + Send + Sync + 'static {{"
                )?;
                writeln!(
                    buf,
                    "        ::twurst_server::codegen::GrpcRouter::new(::std::sync::Arc::new(self))"
                )?;
                for method in &service.methods {
                    write!(
                        buf,
                        "            .route(\"/{}.{}/{}\", |service: ::std::sync::Arc<Self>, request: {}",
                        service.package, service.proto_name, method.proto_name, method.input_type,
                    )?;
                    if self.request_extractors.is_empty() {
                        write!(buf, ", _: ::twurst_server::codegen::RequestParts")?;
                    } else {
                        write!(buf, ", mut parts: ::twurst_server::codegen::RequestParts")?;
                    }
                    write!(buf, "| {{")?;
                    writeln!(buf, "                async move {{")?;
                    write!(buf, "                    service.{}(request", method.name)?;
                    for _ in self.request_extractors.values() {
                        write!(
                            buf,
                            ", match ::twurst_server::codegen::FromRequestParts::from_request_parts(&mut parts, &()).await {{ Ok(r) => r, Err(e) => {{ return Err(::twurst_server::codegen::twirp_error_from_response(e).await) }} }}"
                        )?;
                    }
                    writeln!(buf, ").await")?;
                    writeln!(buf, "                }}")?;
                    writeln!(buf, "            }})")?;
                }
                writeln!(buf, "            .build()")?;
                writeln!(buf, "    }}")?;
            }

            writeln!(buf, "}}")?;
        }

        Ok(())
    }
}