blob: 96a9a6ee0cbcb9d6a4a7f083cb9170ebd764f37b [file] [log] [blame]
David Tolnay0c033e32020-11-01 15:15:48 -08001use proc_macro2::Ident;
2
David Tolnayf02146e2020-11-01 15:35:42 -08003#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnay97c5b862020-11-01 14:59:01 -08004pub enum Block<'a> {
David Tolnay0c033e32020-11-01 15:15:48 -08005 AnonymousNamespace,
6 Namespace(&'static str),
David Tolnay97c5b862020-11-01 14:59:01 -08007 UserDefinedNamespace(&'a Ident),
David Tolnay0c033e32020-11-01 15:15:48 -08008 InlineNamespace(&'static str),
9 ExternC,
10}
11
David Tolnay97c5b862020-11-01 14:59:01 -080012impl<'a> Block<'a> {
David Tolnay2a160e42020-11-01 15:40:20 -080013 pub fn write_begin(self, out: &mut String) {
David Tolnay0c033e32020-11-01 15:15:48 -080014 if let Block::InlineNamespace(_) = self {
15 out.push_str("inline ");
16 }
17 self.write_common(out);
18 out.push_str(" {\n");
19 }
20
David Tolnay2a160e42020-11-01 15:40:20 -080021 pub fn write_end(self, out: &mut String) {
David Tolnay0c033e32020-11-01 15:15:48 -080022 out.push_str("} // ");
23 self.write_common(out);
24 out.push('\n');
25 }
26
David Tolnay2a160e42020-11-01 15:40:20 -080027 fn write_common(self, out: &mut String) {
David Tolnay0c033e32020-11-01 15:15:48 -080028 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}