conjure-codegen 5.11.0

Rust code generation for Conjure definitions
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Copyright 2018 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Code generation for Conjure definitions.
//!
//! # Examples
//!
//! Code generation via a build script, assuming we have a `service-api.conjure.json` file in the crate root:
//!
//! build.rs:
//!
//! ```no_run
//! use std::env;
//! use std::path::Path;
//!
//! fn main() {
//!     let input = "service-api.conjure.json";
//!     let output = Path::new(&env::var_os("OUT_DIR").unwrap()).join("service_api");
//!
//!     println!("cargo:rerun-if-changed={}", input);
//!     conjure_codegen::Config::new()
//!         .strip_prefix("com.foobar.service".to_string())
//!         .generate_files(input, output)
//!         .unwrap();
//! }
//! ```
//!
//! src/lib.rs:
//!
//! ```ignore
//! mod service_api {
//!     include!(concat!(env!("OUT_DIR"), "/service_api/mod.rs"));
//! }
//! ```
//!
//! # Types
//!
//! ## Builtin
//!
//! Builtin types map directly to existing Rust types:
//!
//! | Conjure       | Rust                                 |
//! | ------------- | ------------------------------------ |
//! | `string`      | `String`                             |
//! | `datetime`    | `chrono::DateTime<Utc>`              |
//! | `integer`     | `i32`                                |
//! | `double`      | `f64`                                |
//! | `safelong`    | `conjure_object::SafeLong`           |
//! | `binary`      | `bytes::Bytes`                       |
//! | `any`         | `conjure_object::Any`                |
//! | `boolean`     | `bool`                               |
//! | `uuid`        | `uuid::Uuid`                         |
//! | `rid`         | `conjure_object::ResourceIdentifier` |
//! | `bearertoken` | `conjure_object::BearerToken`        |
//! | `optional<T>` | `Option<T>`                          |
//! | `list<T>`     | `Vec<T>`                             |
//! | `set<T>`      | `BTreeSet<T>`                        |
//! | `map<K, V>`   | `BTreeMap<K, V>`                     |
//!
//! Many of these are exposed by the `conjure-object` crate, which is a required dependency of crates containing the
//! generated code.
//!
//! ### `double`
//!
//! Rust's `f64` type does not implement `Ord`, `Eq`, or `Hash`, which requires some special casing. Sets and maps keyed
//! directly by `double` are represented as `BTreeSet<DoubleKey>` and `BTreeMap<DoubleKey, T>`, where the
//! [`DoubleKey`](conjure_object::DoubleKey) type wraps `f64` and implements those traits by ordering `NaN` greater
//! than all other values and equal to itself.
//!
//! Conjure aliases, objects, and unions wrapping `double` types have trait implementations which use the same logic.
//!
//! ## Objects
//!
//! Conjure objects turn into Rust structs along with builders used to construct them:
//!
//! ```
//! # use conjure_codegen::example_types::objects::product::{ManyFieldExample, StringAliasExample};
//! let object = ManyFieldExample::builder()
//!     .string("foo")
//!     .integer(123)
//!     .double_value(3.14)
//!     .alias(StringAliasExample("foobar".to_string()))
//!     .optional_item("bar".to_string())
//!     .items(vec!["hello".to_string(), "world".to_string()])
//!     .build();
//!
//! assert_eq!(object.string(), "foo");
//! assert_eq!(object.optional_item(), Some("bar"));
//! ```
//!
//! The builder types are by-value and infallible - the compiler will prevent code from compiling if all required
//! fields are not set. The API requires that all required fields be set first strictly in declaration order,
//! after which optional fields can be set in any order.
//!
//! Objects with 3 or fewer required fields also have an explicit constructor:
//!
//! ```rust
//! # use conjure_codegen::example_types::objects::product::BooleanExample;
//! let object = BooleanExample::new(true);
//!
//! assert_eq!(object.coin(), true);
//! ```
//!
//! By default, struct fields are private and have accessor methods. The [`Config::public_fields`] option can be set to
//! make the fields public.
//!
//! The generated structs implement `Debug`, `Clone`, `PartialEq`, Eq, `PartialOrd`, `Ord`, `Hash`, `Serialize`, and
//! `Deserialize`. They implement `Copy` if they consist entirely of copyable primitive types.
//!
//! ## Unions
//!
//! Conjure unions turn into Rust enums. By default, unions are *extensible* through an additional `Unknown` variant.
//! This allows unions to be forward-compatible by allowing clients to deserialize variants they don't yet know about
//! and reserialize them properly:
//!
//! ```
//! # use conjure_codegen::example_types::objects::product::UnionTypeExample;
//! # let union_value = UnionTypeExample::If(0);
//! match union_value {
//!     UnionTypeExample::StringExample(string) => {
//!         // ...
//!     }
//!     UnionTypeExample::Set(set) => {
//!         // ...
//!     }
//!     // ...
//!     UnionTypeExample::Unknown(unknown) => {
//!         println!("got unknown variant: {}", unknown.type_());
//!     }
//!     # _ => {}
//! }
//! ```
//!
//! The generated enums implement `Debug`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Serialize`, and
//! `Deserialize`. Union variants which are themselves unions are boxed in the generated enum to avoid self-referential
//! type definitions.
//!
//! ## Enums
//!
//! Conjure enums turn into Rust enums. By default, enums are *extensible*. This allows enums to be forward-compatible
//! by allowing clients to deserialize variants they don't yet know about and reserialize them properly:
//!
//! ```
//! # use conjure_codegen::example_types::objects::product::EnumExample;
//! # let enum_value = EnumExample::One;
//! match enum_value {
//!     EnumExample::One => println!("found one"),
//!     EnumExample::Two => println!("found two"),
//!     EnumExample::Unknown(unknown) => println!("got unknown variant: {}", unknown),
//! }
//! ```
//!
//! The generated enums implement `Debug`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Display`,
//! `Serialize`, and `Deserialize`.
//!
//! ## Aliases
//!
//! Conjure aliases turn into Rust newtype structs that act like their inner value:
//!
//! ```
//! # use conjure_codegen::example_types::objects::product::StringAliasExample;
//! let alias_value = StringAliasExample("hello world".to_string());
//! assert!(alias_value.starts_with("hello"));
//! ```
//!
//! The generated structs implement `Deref`, `DerefMut`, `Debug`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`,
//! `Hash`, `Serialize`, and `Deserialize`. They implement `Copy` if they wrap a copyable primitive type, `Default`
//! if they wrap a type implementing `Default`, `FromIterator` if they wrap a type implementing `FromIterator`, and
//! `Display` if they wrap a type implementing `Display`. They also implement `From<T>`, where `T` is the fully
//! de-aliased inner type.
//!
//! ## Errors
//!
//! Conjure errors turn into Rust structs storing the error's parameters as if it were a Conjure object. The struct
//! additionally implements the `conjure_error::ErrorType` trait which encodes the extra error metadata:
//!
//! ```
//! # use conjure_codegen::example_types::errors::product::InvalidServiceDefinition;
//! # let (name, definition) = ("", "");
//! use conjure_error::{ErrorType, ErrorCode};
//!
//! assert_eq!(InvalidServiceDefinition::code(), ErrorCode::InvalidArgument);
//! assert_eq!(InvalidServiceDefinition::name(), "Conjure:InvalidServiceDefinition");
//! ```
//!
//! ## Services
//!
//! Conjure services turn into client- and server-side interfaces:
//!
//! ### Clients
//!
//! The client object wraps a raw HTTP client and provides methods to interact with the service's endpoints. Both
//! synchronous and asynchronous clients are provided:
//!
//! Synchronous:
//! ```
//! use conjure_http::client::Service;
//! # use conjure_codegen::example_types::clients::another::{TestService, TestServiceClient};
//! # fn foo<T: conjure_http::client::Client>(http_client: T, runtime: &std::sync::Arc<conjure_http::client::ConjureRuntime>) -> Result<(), conjure_error::Error> {
//! # let auth_token = "foobar".parse().unwrap();
//! let client = TestServiceClient::new(http_client, runtime);
//! let file_systems = client.get_file_systems(&auth_token)?;
//! # Ok(())
//! # }
//! ```
//!
//! Asynchronous:
//! ```
//! use conjure_http::client::AsyncService;
//! # use conjure_codegen::example_types::clients::another::{AsyncTestService, AsyncTestServiceClient};
//! # async fn foo<T>(http_client: T, runtime: &std::sync::Arc<conjure_http::client::ConjureRuntime>) -> Result<(), conjure_error::Error> where T: conjure_http::client::AsyncClient + Sync + Send, T::ResponseBody: 'static + Send {
//! # let auth_token = "foobar".parse().unwrap();
//! let client = AsyncTestServiceClient::new(http_client, runtime);
//! let file_systems = client.get_file_systems(&auth_token).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Servers
//!
//! Conjure generates a trait and accompanying wrapper resource which are used to implement the service's endpoints.
//! Both synchronous and asynchronous servers are supported:
//!
//! Synchronous:
//! ```ignore
//! struct TestServiceHandler;
//!
//! impl<T> TestService<T> for TestServiceHandler
//! where
//!     T: Read
//! {
//!     fn get_file_systems(
//!         &self,
//!         auth: AuthToken,
//!     ) -> Result<BTreeMap<String, BackingFileSystem>, Error> {
//!         // ...
//!     }
//!
//!     // ...
//! }
//!
//! let resource = TestServiceResource::new(TestServiceHandler);
//! http_server.register(resource);
//! ```
//!
//! Asynchronous:
//! ```ignore
//! struct TestServiceHandler;
//!
//! impl<T> AsyncTestService<T> for TestServiceHandler
//! where
//!     T: AsyncRead + 'static + Send
//! {
//!     async fn get_file_systems(
//!         &self,
//!         auth: AuthToken,
//!     ) -> Result<BTreeMap<String, BackingFileSystem>, Error> {
//!         // ...
//!     }
//!
//!     // ...
//! }
//!
//! let resource = TestServiceResource::new(TestServiceHandler);
//! http_server.register(resource);
//! ```
//!
//! ### Endpoint Tags
//!
//! * `server-request-context` - The generated server trait method will have an additional
//!   `RequestContext` argument providing lower level access to request and response information.
//! * `server-limit-request-size: <size>` - Sets the maximum request body size for endpoints with
//!   serializable request bodies. `<size>` should be a human-readable byte count (e.g. `50Mi` or
//!   `100Ki`). Defaults to `50Mi`.
#![warn(clippy::all, missing_docs)]
#![allow(clippy::needless_doctest_main)]
#![recursion_limit = "256"]

use crate::context::Context;
use crate::merge_toml::left_merge;
use crate::types::objects::{ConjureDefinition, TypeDefinition};
use anyhow::{bail, Context as _, Error};
use context::BaseModule;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::Path;
use toml::Value;

mod aliases;
mod cargo_toml;
mod clients;
mod context;
mod enums;
mod errors;
mod merge_toml;
mod objects;
mod servers;
#[allow(dead_code, clippy::all)]
#[rustfmt::skip]
mod types;
mod human_size;
mod unions;

/// Examples of generated Conjure code.
///
/// This module is only intended to be present in documentation; it shouldn't be relied on by any library code.
#[cfg(feature = "example-types")]
#[allow(warnings)]
#[rustfmt::skip]
pub mod example_types;

struct CrateInfo {
    name: String,
    version: String,
}

/// Codegen configuration.
pub struct Config {
    exhaustive: bool,
    serialize_empty_collections: bool,
    use_legacy_error_serialization: bool,
    public_fields: bool,
    strip_prefix: Option<String>,
    version: Option<String>,
    build_crate: Option<CrateInfo>,
    extra_manifest_config: Option<Value>,
}

impl Default for Config {
    fn default() -> Config {
        Config::new()
    }
}

impl Config {
    /// Creates a new `Config` with default settings.
    pub fn new() -> Config {
        Config {
            exhaustive: false,
            serialize_empty_collections: false,
            use_legacy_error_serialization: true,
            public_fields: false,
            strip_prefix: None,
            version: None,
            build_crate: None,
            extra_manifest_config: None,
        }
    }

    /// Controls exhaustive matchability of unions and enums.
    ///
    /// Non-exhaustive unions and enums have the ability to deserialize and reserialize unknown variants. This enables
    /// clients to be more forward-compatible with changes made by newer servers. Note that this is distinct from the
    /// Rust concept of exhaustive matching.
    ///
    /// Defaults to `false`.
    pub fn exhaustive(&mut self, exhaustive: bool) -> &mut Config {
        self.exhaustive = exhaustive;
        self
    }

    /// Controls serialization of empty collection fields in objects.
    ///
    /// Some Conjure implementations don't properly handle deserialization of objects when empty collections are
    /// omitted. Enabling this option will cause empty optional, set, list, and map fields to be included in the
    /// serialized output.
    ///
    /// Defaults to `false`.
    pub fn serialize_empty_collections(
        &mut self,
        serialize_empty_collections: bool,
    ) -> &mut Config {
        self.serialize_empty_collections = serialize_empty_collections;
        self
    }

    /// Parameters of service errors were historically stringified when serialized into response bodies.
    ///
    /// Setting this to `false` will cause error parameters to be serialized directly as their underlying types would
    /// be.
    ///
    /// Defaults to `true`.
    pub fn use_legacy_error_serialization(
        &mut self,
        use_legacy_error_serialization: bool,
    ) -> &mut Config {
        self.use_legacy_error_serialization = use_legacy_error_serialization;
        self
    }

    /// Controls visibility of fields in objects.
    ///
    /// If enabled, fields will be public, but the struct will be annotated `#[non_exhaustive]`. If disabled, fields
    /// will be private and accessor methods will be defined. Note that this is separate from the Conjure concept of
    /// non-exhaustive deserialization.
    ///
    /// Defaults to `false`.
    pub fn public_fields(&mut self, public_fields: bool) -> &mut Config {
        self.public_fields = public_fields;
        self
    }

    /// Sets a prefix that will be stripped from package names.
    ///
    /// Defaults to `None`.
    pub fn strip_prefix<T>(&mut self, strip_prefix: T) -> &mut Config
    where
        T: Into<Option<String>>,
    {
        self.strip_prefix = strip_prefix.into();
        self
    }

    /// Sets the version included in endpoint metadata for generated client bindings.
    ///
    /// Defaults to the version passed to [`Self::build_crate`], or `None` otherwise.
    pub fn version<T>(&mut self, version: T) -> &mut Config
    where
        T: Into<Option<String>>,
    {
        self.version = version.into();
        self
    }

    /// Sets extra manifest configuration to be merged into the generated Cargo.toml.
    ///
    /// Defaults to `None`
    pub fn extra_manifest_config<T>(&mut self, config: T) -> &mut Config
    where
        T: Into<Option<toml::Table>>,
    {
        self.extra_manifest_config = config.into().map(Value::Table);
        self
    }

    /// Switches generation to create a full crate.
    ///
    /// Defaults to just generating a single module.
    pub fn build_crate(&mut self, name: &str, version: &str) -> &mut Config {
        self.build_crate = Some(CrateInfo {
            name: name.to_string(),
            version: version.to_string(),
        });
        self
    }

    /// Generates Rust source files from a JSON-encoded Conjure IR file.
    pub fn generate_files<P, Q>(&self, ir_file: P, out_dir: Q) -> Result<(), Error>
    where
        P: AsRef<Path>,
        Q: AsRef<Path>,
    {
        self.generate_files_inner(ir_file.as_ref(), out_dir.as_ref())
    }

    fn generate_files_inner(&self, ir_file: &Path, out_dir: &Path) -> Result<(), Error> {
        let defs = self.parse_ir(ir_file)?;

        if defs.version() != 1 {
            bail!("unsupported IR version {}", defs.version());
        }

        let modules = self.create_modules(&defs);
        let (src_dir, lib_root) = if self.build_crate.is_some() {
            (out_dir.join("src"), true)
        } else {
            (out_dir.to_path_buf(), false)
        };

        if let Some(info) = &self.build_crate {
            self.write_cargo_toml(out_dir, info, &defs)?;
            self.write_rustfmt_toml(out_dir)?;
        }

        modules.render(&src_dir, lib_root)?;

        Ok(())
    }

    fn parse_ir(&self, ir_file: &Path) -> Result<ConjureDefinition, Error> {
        let ir = fs::read_to_string(ir_file)
            .with_context(|| format!("error reading file {}", ir_file.display()))?;

        let defs = conjure_serde::json::client_from_str(&ir)
            .with_context(|| format!("error parsing Conjure IR file {}", ir_file.display()))?;

        Ok(defs)
    }

    fn create_modules(&self, defs: &ConjureDefinition) -> ModuleTrie {
        let context = Context::new(
            defs,
            self.exhaustive,
            self.serialize_empty_collections,
            self.use_legacy_error_serialization,
            self.public_fields,
            self.strip_prefix.as_deref(),
            self.version
                .as_deref()
                .or_else(|| self.build_crate.as_ref().map(|v| &*v.version)),
        );

        let mut root = ModuleTrie::new();

        for def in defs.types() {
            let (type_name, contents) = match def {
                TypeDefinition::Enum(def) => (def.type_name(), enums::generate(&context, def)),
                TypeDefinition::Alias(def) => (def.type_name(), aliases::generate(&context, def)),
                TypeDefinition::Union(def) => (def.type_name(), unions::generate(&context, def)),
                TypeDefinition::Object(def) => (
                    def.type_name(),
                    objects::generate(&context, BaseModule::Objects, def),
                ),
            };

            let type_ = Type {
                module_name: context.module_name(type_name),
                type_names: vec![context.type_name(type_name.name()).to_string()],
                contents,
            };
            root.insert(&context.module_path(BaseModule::Objects, type_name), type_);
        }

        for def in defs.errors() {
            let type_ = Type {
                module_name: context.module_name(def.error_name()),
                type_names: vec![context.type_name(def.error_name().name()).to_string()],
                contents: errors::generate(&context, def),
            };
            root.insert(
                &context.module_path(BaseModule::Errors, def.error_name()),
                type_,
            );
        }

        for def in defs.services() {
            let client = clients::generate(&context, def);

            let contents = quote! {
                #client
            };
            let type_ = Type {
                module_name: context.module_name(def.service_name()),
                type_names: vec![
                    format!("{}", def.service_name().name()),
                    format!("{}Client", def.service_name().name()),
                    format!("Async{}", def.service_name().name()),
                    format!("Async{}Client", def.service_name().name()),
                ],
                contents,
            };
            root.insert(
                &context.module_path(BaseModule::Clients, def.service_name()),
                type_,
            );

            let server = servers::generate(&context, def);

            let contents = quote! {
                #server
            };
            let type_ = Type {
                module_name: context.module_name(def.service_name()),
                type_names: vec![
                    context.type_name(def.service_name().name()).to_string(),
                    format!("Async{}", def.service_name().name()),
                    format!("{}Endpoints", def.service_name().name()),
                    format!("Async{}Endpoints", def.service_name().name()),
                ],
                contents,
            };
            root.insert(
                &context.module_path(BaseModule::Endpoints, def.service_name()),
                type_,
            );
        }

        root.deconflict();

        root
    }

    fn write_cargo_toml(
        &self,
        dir: &Path,
        info: &CrateInfo,
        def: &ConjureDefinition,
    ) -> Result<(), Error> {
        fs::create_dir_all(dir)
            .with_context(|| format!("error creating directory {}", dir.display()))?;

        let metadata = def
            .extensions()
            .get("recommended-product-dependencies")
            .map(|deps| cargo_toml::Metadata {
                sls: cargo_toml::Sls {
                    recommended_product_dependencies: deps,
                },
            });

        let mut needs_object = false;
        let mut needs_error = false;
        let mut needs_http = false;

        if !def.types().is_empty() {
            needs_object = true;
        }

        if !def.errors().is_empty() {
            needs_object = true;
            needs_error = true;
        }

        if !def.services().is_empty() {
            needs_http = true;
            needs_object = true;
        }

        let conjure_version = env!("CARGO_PKG_VERSION");
        let mut dependencies = BTreeMap::new();
        if needs_object {
            dependencies.insert("conjure-object", conjure_version);
        }
        if needs_error {
            dependencies.insert("conjure-error", conjure_version);
        }
        if needs_http {
            dependencies.insert("conjure-http", conjure_version);
        }

        let manifest = cargo_toml::Manifest {
            package: cargo_toml::Package {
                name: &info.name,
                version: &info.version,
                edition: "2018",
                metadata,
            },
            dependencies,
        };

        let manifest = if let Some(extra_manifest_toml) = self.extra_manifest_config.as_ref() {
            let mut manifest_toml = toml::Value::Table(toml::Table::try_from(&manifest)?);
            left_merge(&mut manifest_toml, extra_manifest_toml)?;
            toml::to_string_pretty(&manifest_toml).unwrap()
        } else {
            toml::to_string_pretty(&manifest).unwrap()
        };

        let file = dir.join("Cargo.toml");

        fs::write(&file, manifest)
            .with_context(|| format!("error writing manifest file {}", file.display()))?;

        Ok(())
    }

    fn write_rustfmt_toml(&self, dir: &Path) -> Result<(), Error> {
        let contents = "\
disable_all_formatting = true
";

        let file = dir.join("rustfmt.toml");

        fs::write(file, contents).with_context(|| "error writing rustfmt.toml")?;

        Ok(())
    }
}

struct Type {
    module_name: String,
    type_names: Vec<String>,
    contents: TokenStream,
}

struct ModuleTrie {
    submodules: BTreeMap<String, ModuleTrie>,
    types: Vec<Type>,
}

impl ModuleTrie {
    fn new() -> ModuleTrie {
        ModuleTrie {
            submodules: BTreeMap::new(),
            types: vec![],
        }
    }

    fn insert(&mut self, module_path: &[String], type_: Type) {
        match module_path.split_first() {
            Some((first, rest)) => self
                .submodules
                .entry(first.clone())
                .or_insert_with(ModuleTrie::new)
                .insert(rest, type_),
            None => self.types.push(type_),
        }
    }

    fn deconflict(&mut self) {
        for type_ in &mut self.types {
            if self.submodules.contains_key(&type_.module_name) {
                type_.module_name.push('_');
            }
        }

        for submodule in self.submodules.values_mut() {
            submodule.deconflict();
        }
    }

    fn render(&self, dir: &Path, lib_root: bool) -> Result<(), Error> {
        fs::create_dir_all(dir)
            .with_context(|| format!("error creating directory {}", dir.display()))?;

        for type_ in &self.types {
            self.write_module(
                &dir.join(format!("{}.rs", type_.module_name)),
                &type_.contents,
            )?;
        }

        for (name, module) in &self.submodules {
            module.render(&dir.join(name), false)?;
        }

        let root = self.create_root_module(lib_root);
        let file_name = if lib_root { "lib.rs" } else { "mod.rs" };
        self.write_module(&dir.join(file_name), &root)?;

        Ok(())
    }

    fn write_module(&self, path: &Path, contents: &TokenStream) -> Result<(), Error> {
        let file = syn::parse2(contents.clone())?;
        let formatted = prettyplease::unparse(&file);

        fs::write(path, formatted)
            .with_context(|| format!("error writing module {}", path.display()))?;
        Ok(())
    }

    fn create_root_module(&self, lib_root: bool) -> TokenStream {
        let attrs = if lib_root {
            quote! {
                #![allow(warnings)]
            }
        } else {
            quote! {}
        };

        let uses = self.types.iter().map(|m| {
            let module_name = m.module_name.parse::<TokenStream>().unwrap();
            let type_names = m
                .type_names
                .iter()
                .map(|n| n.parse::<TokenStream>().unwrap());
            quote! {
                #[doc(inline)]
                pub use self::#module_name::{#(#type_names),*};
            }
        });

        let type_mods = self.types.iter().map(|m| {
            let module_name = m.module_name.parse::<TokenStream>().unwrap();
            quote! {
                pub mod #module_name;
            }
        });

        let sub_mods = self.submodules.keys().map(|v| {
            let module_name = v.parse::<TokenStream>().unwrap();
            quote! {
                pub mod #module_name;
            }
        });

        quote! {
            #attrs
            #(#uses)*

            #(#type_mods)*
            #(#sub_mods)*
        }
    }
}