cxx_gen/gen/
block.rs

1use proc_macro2::Ident;
2
3#[derive(Copy, Clone, PartialEq, Debug)]
4pub(crate) enum Block<'a> {
5    AnonymousNamespace,
6    Namespace(&'static str),
7    UserDefinedNamespace(&'a Ident),
8    InlineNamespace(&'static str),
9    ExternC,
10}
11
12impl<'a> Block<'a> {
13    pub(crate) fn write_begin(self, out: &mut String) {
14        if let Block::InlineNamespace(_) = self {
15            out.push_str("inline ");
16        }
17        self.write_common(out);
18        out.push_str(" {\n");
19    }
20
21    pub(crate) fn write_end(self, out: &mut String) {
22        out.push_str("} // ");
23        self.write_common(out);
24        out.push('\n');
25    }
26
27    fn write_common(self, out: &mut String) {
28        match self {
29            Block::AnonymousNamespace => out.push_str("namespace"),
30            Block::Namespace(name) => {
31                out.push_str("namespace ");
32                out.push_str(name);
33            }
34            Block::UserDefinedNamespace(name) => {
35                out.push_str("namespace ");
36                out.push_str(&name.to_string());
37            }
38            Block::InlineNamespace(name) => {
39                out.push_str("namespace ");
40                out.push_str(name);
41            }
42            Block::ExternC => out.push_str("extern \"C\""),
43        }
44    }
45}