| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | use std::fmt::{self, Arguments, Write}; |
| 2 | |
| 3 | pub(crate) struct OutFile { |
| 4 | pub namespace: Vec<String>, |
| 5 | pub header: bool, |
| 6 | content: Vec<u8>, |
| 7 | section_pending: bool, |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 8 | blocks_pending: Vec<&'static str>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | impl OutFile { |
| 12 | pub fn new(namespace: Vec<String>, header: bool) -> Self { |
| 13 | OutFile { |
| 14 | namespace, |
| 15 | header, |
| 16 | content: Vec::new(), |
| 17 | section_pending: false, |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 18 | blocks_pending: Vec::new(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 19 | } |
| 20 | } |
| 21 | |
| 22 | // Write a blank line if the preceding section had any contents. |
| 23 | pub fn next_section(&mut self) { |
| 24 | self.section_pending = true; |
| 25 | } |
| 26 | |
| 27 | pub fn begin_block(&mut self, block: &'static str) { |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 28 | self.blocks_pending.push(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 31 | pub fn end_block(&mut self, block: &'static str) { |
| 32 | if self.blocks_pending.pop().is_none() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 33 | self.content.extend_from_slice(b"} // "); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 34 | self.content.extend_from_slice(block.as_bytes()); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 35 | self.content.push(b'\n'); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 36 | self.section_pending = true; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn write_fmt(&mut self, args: Arguments) { |
| 41 | Write::write_fmt(self, args).unwrap(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl Write for OutFile { |
| 46 | fn write_str(&mut self, s: &str) -> fmt::Result { |
| 47 | if !s.is_empty() { |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 48 | if !self.blocks_pending.is_empty() { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 49 | self.content.push(b'\n'); |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 50 | for block in self.blocks_pending.drain(..) { |
| David Tolnay | b92e66f | 2020-03-01 13:36:55 -0800 | [diff] [blame] | 51 | self.content.extend_from_slice(block.as_bytes()); |
| 52 | self.content.extend_from_slice(b" {\n"); |
| 53 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 54 | self.section_pending = false; |
| 55 | } else if self.section_pending { |
| 56 | self.content.push(b'\n'); |
| 57 | self.section_pending = false; |
| 58 | } |
| 59 | self.content.extend_from_slice(s.as_bytes()); |
| 60 | } |
| 61 | Ok(()) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl AsRef<[u8]> for OutFile { |
| 66 | fn as_ref(&self) -> &[u8] { |
| 67 | &self.content |
| 68 | } |
| 69 | } |