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