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