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