|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fmt::Write; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | +use std::{env, fs}; |
| 5 | + |
| 6 | +use mdbook_preprocessor::book::Chapter; |
| 7 | + |
| 8 | +use crate::target::{ByTier, Target}; |
| 9 | + |
| 10 | +pub mod target; |
| 11 | + |
| 12 | +const TIER_1_HOST_MARKER: &str = "{{TIER_1_HOST_TABLE}}"; |
| 13 | +const TIER_1_NOHOST_MARKER: &str = "{{TIER_1_NOHOST_TABLE}}"; |
| 14 | +const TIER_2_HOST_MARKER: &str = "{{TIER_2_HOST_TABLE}}"; |
| 15 | +const TIER_2_NOHOST_MARKER: &str = "{{TIER_2_NOHOST_TABLE}}"; |
| 16 | +const TIER_3_MARKER: &str = "{{TIER_3_TABLE}}"; |
| 17 | + |
| 18 | +const EMPTY_TIER_1_NOHOST_MSG: &str = |
| 19 | + "At this time, all Tier 1 targets are [Tier 1 with Host Tools](#tier-1-with-host-tools).\n"; |
| 20 | +const EMPTY_TIER_2_NOHOST_MSG: &str = |
| 21 | + "At this time, all Tier 2 targets are [Tier 2 with Host Tools](#tier-2-with-host-tools).\n"; |
| 22 | + |
| 23 | +pub fn process_main_chapter( |
| 24 | + chapter: &mut Chapter, |
| 25 | + rustc_targets: &[Target], |
| 26 | + target_chapters: &HashMap<String, Chapter>, |
| 27 | +) { |
| 28 | + let targets = ByTier::from(rustc_targets); |
| 29 | + let mut new_content = String::new(); |
| 30 | + |
| 31 | + for line in chapter.content.lines() { |
| 32 | + match line.trim() { |
| 33 | + TIER_1_HOST_MARKER => { |
| 34 | + write_host_table(&mut new_content, &targets.tier1_host, &target_chapters) |
| 35 | + } |
| 36 | + TIER_1_NOHOST_MARKER => write_nohost_table( |
| 37 | + &mut new_content, |
| 38 | + &targets.tier1_nohost, |
| 39 | + &target_chapters, |
| 40 | + EMPTY_TIER_1_NOHOST_MSG, |
| 41 | + ), |
| 42 | + TIER_2_HOST_MARKER => { |
| 43 | + write_host_table(&mut new_content, &targets.tier2_host, &target_chapters) |
| 44 | + } |
| 45 | + TIER_2_NOHOST_MARKER => write_nohost_table( |
| 46 | + &mut new_content, |
| 47 | + &targets.tier2_nohost, |
| 48 | + &target_chapters, |
| 49 | + EMPTY_TIER_2_NOHOST_MSG, |
| 50 | + ), |
| 51 | + TIER_3_MARKER => write_tier3_table(&mut new_content, &targets.tier3, &target_chapters), |
| 52 | + _ => { |
| 53 | + new_content.push_str(line); |
| 54 | + new_content.push_str("\n"); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + debug_dump("platform-support.md", &new_content); |
| 60 | + |
| 61 | + chapter.content = new_content; |
| 62 | +} |
| 63 | + |
| 64 | +fn write_host_table( |
| 65 | + out: &mut String, |
| 66 | + targets: &[&Target], |
| 67 | + target_chapters: &HashMap<String, Chapter>, |
| 68 | +) { |
| 69 | + out.push_str("target | notes\n-------|-------\n"); |
| 70 | + for target in targets { |
| 71 | + write_target_tuple(out, target, target_chapters); |
| 72 | + _ = writeln!(out, " | {}", target.metadata.description.as_deref().unwrap_or("")); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn write_nohost_table( |
| 77 | + out: &mut String, |
| 78 | + targets: &[&Target], |
| 79 | + target_chapters: &HashMap<String, Chapter>, |
| 80 | + empty_msg: &str, |
| 81 | +) { |
| 82 | + if targets.is_empty() { |
| 83 | + out.push_str(empty_msg); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + out.push_str("target | std | notes\n-------|:---:|-------\n"); |
| 88 | + for target in targets { |
| 89 | + write_target_tuple(out, target, target_chapters); |
| 90 | + _ = writeln!( |
| 91 | + out, |
| 92 | + " | {} | {}", |
| 93 | + std_support_symbol(target.metadata.std), |
| 94 | + target.metadata.description.as_deref().unwrap_or("") |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn write_tier3_table( |
| 100 | + out: &mut String, |
| 101 | + targets: &[&Target], |
| 102 | + target_chapters: &HashMap<String, Chapter>, |
| 103 | +) { |
| 104 | + out.push_str("target | std | host | notes\n-------|:---:|:----:|-------\n"); |
| 105 | + for target in targets { |
| 106 | + write_target_tuple(out, target, target_chapters); |
| 107 | + _ = writeln!( |
| 108 | + out, |
| 109 | + " | {} | {} | {}", |
| 110 | + std_support_symbol(target.metadata.std), |
| 111 | + host_support_symbol(target.metadata.host_tools), |
| 112 | + target.metadata.description.as_deref().unwrap_or("") |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +fn write_target_tuple( |
| 118 | + out: &mut String, |
| 119 | + target: &Target, |
| 120 | + target_chapters: &HashMap<String, Chapter>, |
| 121 | +) { |
| 122 | + let doc_chapter = target.metadata.doc_chapter.as_deref().unwrap_or(&*target.tuple); |
| 123 | + |
| 124 | + if target_chapters.contains_key(doc_chapter) { |
| 125 | + _ = write!(out, "[`{}`](platform-support/{}.md)", target.tuple, doc_chapter); |
| 126 | + } else { |
| 127 | + _ = write!(out, "`{}`", target.tuple); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +pub fn std_support_symbol(support: Option<bool>) -> &'static str { |
| 132 | + match support { |
| 133 | + Some(true) => "✓", |
| 134 | + Some(false) => "*", |
| 135 | + None => "?", |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +pub fn host_support_symbol(support: Option<bool>) -> &'static str { |
| 140 | + match support { |
| 141 | + Some(true) => "✓", |
| 142 | + Some(false) => "", |
| 143 | + None => "?", |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +pub fn debug_dump<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) { |
| 148 | + if let Some(dir) = env::var_os("MDBOOK_RUSTC_DEBUG_DUMP_DIR") |
| 149 | + && !dir.is_empty() |
| 150 | + { |
| 151 | + let mut dump_path = PathBuf::from(dir); |
| 152 | + dump_path.push(path.as_ref()); |
| 153 | + fs::create_dir_all(dump_path.parent().unwrap()).unwrap(); |
| 154 | + fs::write(dump_path, contents).unwrap(); |
| 155 | + } |
| 156 | +} |
0 commit comments