1 use proc_macro2::Ident; 2 3 #[derive(Copy, Clone, PartialEq, Debug)] 4 pub enum Block<'a> { 5 AnonymousNamespace, 6 Namespace(&'static str), 7 UserDefinedNamespace(&'a Ident), 8 InlineNamespace(&'static str), 9 ExternC, 10 } 11 12 impl<'a> Block<'a> { write_begin(self, out: &mut String)13 pub 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 write_end(self, out: &mut String)21 pub fn write_end(self, out: &mut String) { 22 out.push_str("} // "); 23 self.write_common(out); 24 out.push('\n'); 25 } 26 write_common(self, out: &mut String)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 } 46