cxx_build/gen/
block.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
use proc_macro2::Ident;

#[derive(Copy, Clone, PartialEq, Debug)]
pub(crate) enum Block<'a> {
    AnonymousNamespace,
    Namespace(&'static str),
    UserDefinedNamespace(&'a Ident),
    InlineNamespace(&'static str),
    ExternC,
}

impl<'a> Block<'a> {
    pub(crate) fn write_begin(self, out: &mut String) {
        if let Block::InlineNamespace(_) = self {
            out.push_str("inline ");
        }
        self.write_common(out);
        out.push_str(" {\n");
    }

    pub(crate) fn write_end(self, out: &mut String) {
        out.push_str("} // ");
        self.write_common(out);
        out.push('\n');
    }

    fn write_common(self, out: &mut String) {
        match self {
            Block::AnonymousNamespace => out.push_str("namespace"),
            Block::Namespace(name) => {
                out.push_str("namespace ");
                out.push_str(name);
            }
            Block::UserDefinedNamespace(name) => {
                out.push_str("namespace ");
                out.push_str(&name.to_string());
            }
            Block::InlineNamespace(name) => {
                out.push_str("namespace ");
                out.push_str(name);
            }
            Block::ExternC => out.push_str("extern \"C\""),
        }
    }
}