diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/sysinfo/tests/code_checkers/docs.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/sysinfo/tests/code_checkers/docs.rs')
-rw-r--r-- | vendor/sysinfo/tests/code_checkers/docs.rs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/vendor/sysinfo/tests/code_checkers/docs.rs b/vendor/sysinfo/tests/code_checkers/docs.rs new file mode 100644 index 000000000..918e64bb0 --- /dev/null +++ b/vendor/sysinfo/tests/code_checkers/docs.rs @@ -0,0 +1,135 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use super::utils::{show_error, TestResult}; +use std::ffi::OsStr; +use std::path::Path; + +fn to_correct_name(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + + for c in s.chars() { + if c.is_uppercase() { + if !out.is_empty() { + out.push('_'); + } + out.push_str(c.to_lowercase().to_string().as_str()); + } else { + out.push(c); + } + } + out +} + +fn check_md_doc_path(p: &Path, md_line: &str, ty_line: &str) -> bool { + let parts = md_line.split("/").collect::<Vec<_>>(); + if let Some(md_name) = parts.last().and_then(|n| n.split(".md").next()) { + if let Some(name) = ty_line + .split_whitespace() + .filter(|s| !s.is_empty()) + .skip(2) + .next() + { + if let Some(name) = name + .split('<') + .next() + .and_then(|n| n.split('{').next()) + .and_then(|n| n.split('(').next()) + .and_then(|n| n.split(';').next()) + { + let correct = to_correct_name(name); + if correct.as_str() == md_name { + return true; + } + show_error( + p, + &format!( + "Invalid markdown file name `{}`, should have been `{}`", + md_name, correct + ), + ); + return false; + } + } + show_error(p, &format!("Cannot extract type name from `{}`", ty_line)); + } else { + show_error(p, &format!("Cannot extract md name from `{}`", md_line)); + } + return false; +} + +fn check_doc_comments_before(p: &Path, lines: &[&str], start: usize) -> bool { + let mut found_docs = false; + + for pos in (0..start).rev() { + let trimmed = lines[pos].trim(); + if trimmed.starts_with("///") { + if !lines[start].trim().starts_with("pub enum ThreadStatus {") { + show_error( + p, + &format!( + "Types should use common documentation by using `#[doc = include_str!(` \ + and by putting the markdown file in the `md_doc` folder instead of `{}`", + &lines[pos], + ), + ); + return false; + } + return true; + } else if trimmed.starts_with("#[doc = include_str!(") { + found_docs = true; + if !check_md_doc_path(p, trimmed, lines[start]) { + return false; + } + } else if !trimmed.starts_with("#[") && !trimmed.starts_with("//") { + break; + } + } + if !found_docs { + show_error( + p, + &format!( + "Missing documentation for public item: `{}` (if it's not supposed to be a public \ + item, use `pub(crate)` instead)", + lines[start], + ), + ); + return false; + } + true +} + +pub fn check_docs(content: &str, p: &Path) -> TestResult { + let mut res = TestResult { + nb_tests: 0, + nb_errors: 0, + }; + + // No need to check if we are in the `src` folder or if we are in a `ffi.rs` file. + if p.parent().unwrap().file_name().unwrap() == OsStr::new("src") + || p.file_name().unwrap() == OsStr::new("ffi.rs") + { + return res; + } + let lines = content.lines().collect::<Vec<_>>(); + + for pos in 1..lines.len() { + let line = lines[pos]; + let trimmed = line.trim(); + if trimmed.starts_with("//!") { + show_error(p, "There shouln't be inner doc comments (`//!`)"); + res.nb_tests += 1; + res.nb_errors += 1; + continue; + } else if !line.starts_with("pub fn ") + && !trimmed.starts_with("pub struct ") + && !trimmed.starts_with("pub enum ") + { + continue; + } + res.nb_tests += 1; + if !check_doc_comments_before(p, &lines, pos) { + res.nb_errors += 1; + } + } + res +} |