| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 1 | use std::fmt::{self, Display}; |
| 2 | |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame^] | 3 | pub static HEADER: &str = include_str!("include/cxx.h"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | |
| 5 | pub fn get(guard: &str) -> &'static str { |
| David Tolnay | 2a1eaac | 2020-02-24 02:01:47 -0800 | [diff] [blame] | 6 | let ifndef = format!("#ifndef {}", guard); |
| 7 | let endif = format!("#endif // {}", guard); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 8 | let begin = HEADER.find(&ifndef); |
| 9 | let end = HEADER.find(&endif); |
| 10 | if let (Some(begin), Some(end)) = (begin, end) { |
| 11 | &HEADER[begin..end + endif.len()] |
| 12 | } else { |
| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame^] | 13 | panic!("not found in cxx.h header: {}", guard) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | } |
| 15 | } |
| David Tolnay | 9c68b1a | 2020-03-06 11:12:55 -0800 | [diff] [blame] | 16 | |
| 17 | #[derive(Default)] |
| 18 | pub struct Includes { |
| 19 | custom: Vec<String>, |
| 20 | pub cstdint: bool, |
| 21 | pub memory: bool, |
| 22 | pub string: bool, |
| 23 | pub type_traits: bool, |
| 24 | } |
| 25 | |
| 26 | impl Includes { |
| 27 | pub fn new() -> Self { |
| 28 | Includes::default() |
| 29 | } |
| 30 | |
| 31 | pub fn insert(&mut self, include: String) { |
| 32 | self.custom.push(include); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Display for Includes { |
| 37 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 38 | for include in &self.custom { |
| 39 | writeln!(f, "#include \"{}\"", include.escape_default())?; |
| 40 | } |
| 41 | if self.cstdint { |
| 42 | writeln!(f, "#include <cstdint>")?; |
| 43 | } |
| 44 | if self.memory { |
| 45 | writeln!(f, "#include <memory>")?; |
| 46 | } |
| 47 | if self.string { |
| 48 | writeln!(f, "#include <string>")?; |
| 49 | } |
| 50 | if self.type_traits { |
| 51 | writeln!(f, "#include <type_traits>")?; |
| 52 | } |
| 53 | Ok(()) |
| 54 | } |
| 55 | } |