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