| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 1 | use crate::gen::include::Includes; |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 2 | use std::cell::RefCell; |
| 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 | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 6 | pub header: bool, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 7 | pub include: Includes, |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 8 | pub front: Content, |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 9 | content: RefCell<Content>, |
| 10 | } |
| 11 | |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 12 | pub struct Content { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 13 | bytes: String, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | section_pending: bool, |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 15 | blocks_pending: Vec<&'static str>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | impl OutFile { |
| Adrian Taylor | c871343 | 2020-10-21 18:20:55 -0700 | [diff] [blame] | 19 | pub fn new(header: bool) -> Self { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 20 | OutFile { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 21 | header, |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 22 | include: Includes::new(), |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 23 | front: Content::new(), |
| 24 | content: RefCell::new(Content::new()), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
| 28 | // Write a blank line if the preceding section had any contents. |
| 29 | pub fn next_section(&mut self) { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 30 | let content = self.content.get_mut(); |
| 31 | content.section_pending = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | pub fn begin_block(&mut self, block: &'static str) { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 35 | let content = self.content.get_mut(); |
| 36 | content.blocks_pending.push(block); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 39 | pub fn end_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 | if content.blocks_pending.pop().is_none() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 42 | content.bytes.push_str("} // "); |
| 43 | content.bytes.push_str(block); |
| 44 | content.bytes.push('\n'); |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 45 | content.section_pending = true; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 49 | pub fn write_fmt(&self, args: Arguments) { |
| 50 | let content = &mut *self.content.borrow_mut(); |
| 51 | Write::write_fmt(content, args).unwrap(); |
| 52 | } |
| 53 | |
| 54 | pub fn content(&self) -> Vec<u8> { |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 55 | let front = &self.front.bytes; |
| 56 | let content = &self.content.borrow().bytes; |
| David Tolnay | 91489ec | 2020-09-03 12:48:46 -0700 | [diff] [blame] | 57 | let len = front.len() + content.len() + 1; |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 58 | let mut out = String::with_capacity(len); |
| 59 | out.push_str(front); |
| David Tolnay | 91489ec | 2020-09-03 12:48:46 -0700 | [diff] [blame] | 60 | if !front.is_empty() && !content.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 61 | out.push('\n'); |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 62 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 63 | out.push_str(content); |
| David Tolnay | 91489ec | 2020-09-03 12:48:46 -0700 | [diff] [blame] | 64 | if out.is_empty() { |
| 65 | out.push_str("// empty\n"); |
| 66 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 67 | out.into_bytes() |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 71 | impl Write for Content { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 72 | fn write_str(&mut self, s: &str) -> fmt::Result { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 73 | self.write(s); |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 74 | Ok(()) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl Content { |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 79 | pub fn write_fmt(&mut self, args: Arguments) { |
| 80 | Write::write_fmt(self, args).unwrap(); |
| 81 | } |
| 82 | |
| 83 | fn new() -> Self { |
| 84 | Content { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 85 | bytes: String::new(), |
| David Tolnay | 54702b9 | 2020-07-31 11:50:09 -0700 | [diff] [blame] | 86 | section_pending: false, |
| 87 | blocks_pending: Vec::new(), |
| 88 | } |
| 89 | } |
| 90 | |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 91 | fn write(&mut self, b: &str) { |
| David Tolnay | f4632de | 2020-07-31 10:46:55 -0700 | [diff] [blame] | 92 | if !b.is_empty() { |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 93 | if !self.blocks_pending.is_empty() { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 94 | if !self.bytes.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 95 | self.bytes.push('\n'); |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 96 | } |
| David Tolnay | 9ad1fbc | 2020-03-01 14:01:24 -0800 | [diff] [blame] | 97 | for block in self.blocks_pending.drain(..) { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 98 | self.bytes.push_str(block); |
| 99 | self.bytes.push_str(" {\n"); |
| David Tolnay | b92e66f | 2020-03-01 13:36:55 -0800 | [diff] [blame] | 100 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 101 | self.section_pending = false; |
| 102 | } else if self.section_pending { |
| David Tolnay | 7ece56f | 2020-03-29 21:21:38 -0700 | [diff] [blame] | 103 | if !self.bytes.is_empty() { |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 104 | self.bytes.push('\n'); |
| David Tolnay | 3577d45 | 2020-03-17 21:48:13 -0700 | [diff] [blame] | 105 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 106 | self.section_pending = false; |
| 107 | } |
| David Tolnay | dab7e80 | 2020-08-28 18:54:48 -0700 | [diff] [blame] | 108 | self.bytes.push_str(b); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 109 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 110 | } |
| 111 | } |