blob: a4b416ad9c9afe8ee52b81c4559674c0c8b0e302 [file] [log] [blame]
David Tolnay9c68b1a2020-03-06 11:12:55 -08001use std::fmt::{self, Display};
2
David Tolnay736cbca2020-03-11 16:49:18 -07003pub static HEADER: &str = include_str!("include/cxx.h");
David Tolnay7db73692019-10-20 14:51:12 -04004
5pub fn get(guard: &str) -> &'static str {
David Tolnay2a1eaac2020-02-24 02:01:47 -08006 let ifndef = format!("#ifndef {}", guard);
7 let endif = format!("#endif // {}", guard);
David Tolnay7db73692019-10-20 14:51:12 -04008 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 Tolnay736cbca2020-03-11 16:49:18 -070013 panic!("not found in cxx.h header: {}", guard)
David Tolnay7db73692019-10-20 14:51:12 -040014 }
15}
David Tolnay9c68b1a2020-03-06 11:12:55 -080016
17#[derive(Default)]
18pub 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
26impl 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
36impl 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}