tfschema_bindgen/
config.rs

1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::collections::BTreeMap;
5
6/// Code generation options meant to be supported by all languages.
7#[derive(Clone, Debug)]
8pub struct CodeGeneratorConfig {
9    pub(crate) module_name: String,
10    pub(crate) external_definitions: ExternalDefinitions,
11    pub(crate) comments: DocComments,
12}
13
14/// Track types definitions provided by external modules.
15pub type ExternalDefinitions =
16    std::collections::BTreeMap</* module */ String, /* type names */ Vec<String>>;
17
18/// Track documentation to be attached to particular definitions.
19pub type DocComments =
20    std::collections::BTreeMap</* qualified name */ Vec<String>, /* comment */ String>;
21
22impl CodeGeneratorConfig {
23    /// Default config for the given module name.
24    pub fn new(module_name: String) -> Self {
25        Self {
26            module_name,
27            external_definitions: BTreeMap::new(),
28            comments: BTreeMap::new(),
29        }
30    }
31
32    /// Container names provided by external modules.
33    pub fn with_external_definitions(mut self, external_definitions: ExternalDefinitions) -> Self {
34        self.external_definitions = external_definitions;
35        self
36    }
37
38    /// Comments attached to particular entity.
39    pub fn with_comments(mut self, mut comments: DocComments) -> Self {
40        // Make sure comments end with a (single) newline.
41        for comment in comments.values_mut() {
42            *comment = format!("{}\n", comment.trim());
43        }
44        self.comments = comments;
45        self
46    }
47}