|
| 1 | +use crate::cache::Cache; |
| 2 | +use crate::error::{DiagCtxt, Source}; |
| 3 | +use crate::{Command, CommandKind}; |
| 4 | + |
| 5 | +impl Command<'_> { |
| 6 | + // FIXME: implement all checks! |
| 7 | + // FIXME: move regex parsing etc. into the parser maybe |
| 8 | + pub(crate) fn run(self, cache: &mut Cache<'_>, dcx: &mut DiagCtxt) -> Result<(), ()> { |
| 9 | + let result = self.kind.run(cache, self.source.clone(), dcx)?; |
| 10 | + |
| 11 | + if result == self.negated { |
| 12 | + // FIXME: better diag |
| 13 | + dcx.emit("check failed", self.source, None); |
| 14 | + return Err(()); |
| 15 | + } |
| 16 | + |
| 17 | + Ok(()) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl CommandKind { |
| 22 | + fn run( |
| 23 | + self, |
| 24 | + cache: &mut Cache<'_>, |
| 25 | + source: Source<'_>, |
| 26 | + dcx: &mut DiagCtxt, |
| 27 | + ) -> Result<bool, ()> { |
| 28 | + Ok(match self { |
| 29 | + Self::HasFile { path } => cache.has(path, dcx)?, // FIXME: check if it's actually a file |
| 30 | + Self::HasDir { path } => cache.has(path, dcx)?, // FIXME: check if it's actually a directory |
| 31 | + Self::Has { path, xpath, text } => { |
| 32 | + let _data = cache.load(path, dcx)?; |
| 33 | + _ = xpath; |
| 34 | + _ = text; |
| 35 | + true // FIXME |
| 36 | + } |
| 37 | + Self::HasRaw { path, text } => { |
| 38 | + let data = cache.load(path, dcx)?; |
| 39 | + |
| 40 | + if text.is_empty() { |
| 41 | + // fast path |
| 42 | + return Ok(true); |
| 43 | + } |
| 44 | + |
| 45 | + let text = channel_url::instantiate(&text, dcx)?; |
| 46 | + let text = text.replace(|c: char| c.is_ascii_whitespace(), " "); |
| 47 | + let data = data.replace(|c: char| c.is_ascii_whitespace(), " "); |
| 48 | + |
| 49 | + data.contains(&text) |
| 50 | + } |
| 51 | + Self::Matches { path, xpath, pattern } => { |
| 52 | + let _data = cache.load(path, dcx)?; |
| 53 | + _ = xpath; |
| 54 | + |
| 55 | + let Ok(_pattern) = |
| 56 | + regex::RegexBuilder::new(&pattern).unicode(true).build().map_err(|error| { |
| 57 | + // FIXME: better error message and location |
| 58 | + // FIXME: Use `regex_syntax` directly. Its error type exposes the |
| 59 | + // underlying span which we can then translate/offset. |
| 60 | + _ = error; |
| 61 | + dcx.emit(&format!("malformed regex"), Some(source), None) |
| 62 | + }) |
| 63 | + else { |
| 64 | + return Err(()); |
| 65 | + }; |
| 66 | + |
| 67 | + true // FIXME |
| 68 | + } |
| 69 | + Self::MatchesRaw { path, pattern } => { |
| 70 | + let data = cache.load(path, dcx)?; |
| 71 | + let pattern = channel_url::instantiate(&pattern, dcx)?; |
| 72 | + |
| 73 | + if pattern.is_empty() { |
| 74 | + // fast path |
| 75 | + return Ok(true); |
| 76 | + } |
| 77 | + |
| 78 | + let Ok(pattern) = |
| 79 | + regex::RegexBuilder::new(&pattern).unicode(true).build().map_err(|error| { |
| 80 | + // FIXME: better error message and location |
| 81 | + // FIXME: Use `regex_syntax` directly. Its error type exposes the |
| 82 | + // underlying span which we can then translate/offset. |
| 83 | + _ = error; |
| 84 | + dcx.emit(&format!("malformed regex"), Some(source), None) |
| 85 | + }) |
| 86 | + else { |
| 87 | + return Err(()); |
| 88 | + }; |
| 89 | + |
| 90 | + pattern.is_match(data) |
| 91 | + } |
| 92 | + Self::Count { path, xpath, text, count } => { |
| 93 | + let _data = cache.load(path, dcx)?; |
| 94 | + _ = xpath; |
| 95 | + _ = text; |
| 96 | + _ = count; |
| 97 | + true // FIXME |
| 98 | + } |
| 99 | + Self::Files { path, files } => { |
| 100 | + let _data = cache.load(path, dcx)?; |
| 101 | + _ = files; |
| 102 | + true // FIXME |
| 103 | + } |
| 104 | + Self::Snapshot { name, path, xpath } => { |
| 105 | + let _data = cache.load(path, dcx)?; |
| 106 | + _ = name; |
| 107 | + _ = path; |
| 108 | + _ = xpath; |
| 109 | + true // FIXME |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +mod channel_url { |
| 116 | + use std::{borrow::Cow, sync::OnceLock}; |
| 117 | + |
| 118 | + use crate::error::DiagCtxt; |
| 119 | + |
| 120 | + const PLACEHOLDER: &str = "{{channel}}"; |
| 121 | + |
| 122 | + pub(super) fn instantiate<'a>(input: &'a str, dcx: &mut DiagCtxt) -> Result<Cow<'a, str>, ()> { |
| 123 | + let Some(channel_url) = channel_url(dcx)? else { return Ok(input.into()) }; |
| 124 | + Ok(input.replace(PLACEHOLDER, channel_url).into()) |
| 125 | + } |
| 126 | + |
| 127 | + #[allow(dead_code)] // FIXME |
| 128 | + pub(super) fn anonymize<'a>(input: &'a str, dcx: &'_ mut DiagCtxt) -> Result<Cow<'a, str>, ()> { |
| 129 | + let Some(channel_url) = channel_url(dcx)? else { return Ok(input.into()) }; |
| 130 | + Ok(input.replace(channel_url, PLACEHOLDER).into()) |
| 131 | + } |
| 132 | + |
| 133 | + fn channel_url(dcx: &mut DiagCtxt) -> Result<Option<&'static str>, ()> { |
| 134 | + static CHANNEL_URL: OnceLock<Option<String>> = OnceLock::new(); |
| 135 | + |
| 136 | + // FIXME: Use `get_or_try_init` here (instead of `get`→`set`→`get`) if/once stabilized (on beta). |
| 137 | + |
| 138 | + if let Some(channel_url) = CHANNEL_URL.get() { |
| 139 | + return Ok(channel_url.as_deref()); |
| 140 | + } |
| 141 | + |
| 142 | + const KEY: &str = "DOC_RUST_LANG_ORG_CHANNEL"; |
| 143 | + |
| 144 | + let channel_url = match std::env::var(KEY) { |
| 145 | + Ok(url) => Some(url), |
| 146 | + // FIXME: should we make the channel mandatory instead? |
| 147 | + Err(std::env::VarError::NotPresent) => None, |
| 148 | + Err(std::env::VarError::NotUnicode(var)) => { |
| 149 | + // FIXME: better diag |
| 150 | + // FIXME: Use `OsStr::display` (instead of `to_string_lossy`) if/once stabilized (on beta). |
| 151 | + dcx.emit( |
| 152 | + &format!("env var `{KEY}` is not valid UTF-8: `{}`", var.to_string_lossy()), |
| 153 | + None, |
| 154 | + None, |
| 155 | + ); |
| 156 | + return Err(()); |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + // unwrap: The static item is locally scoped and no other thread tries to initialize it. |
| 161 | + CHANNEL_URL.set(channel_url).unwrap(); |
| 162 | + // unwrap: Initialized above. |
| 163 | + Ok(CHANNEL_URL.get().unwrap().as_deref()) |
| 164 | + } |
| 165 | +} |
0 commit comments