#[cfg(test)]
mod tests {
fn rust_files_under(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut files = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return files;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
files.extend(rust_files_under(&path));
} else if path.extension().is_some_and(|ext| ext == "rs") {
files.push(path);
}
}
files
}
fn scan_dirs() -> Vec<std::path::PathBuf> {
let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
vec![
manifest.join("src/view"),
manifest.join("../knotra-ui/src/widget"),
]
}
fn is_scan_target(path: &std::path::Path) -> bool {
!path.ends_with("button_label_guard.rs")
}
fn is_comment_line(trimmed: &str) -> bool {
trimmed.starts_with("//")
}
fn blank_comment_lines(source: &str) -> String {
source
.lines()
.map(|line| {
if is_comment_line(line.trim_start()) {
" ".repeat(line.len())
} else {
line.to_owned()
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn matching_open_paren(source: &str, close_paren: usize) -> Option<usize> {
let bytes = source.as_bytes();
let mut depth = 1i32;
let mut i = close_paren;
while i > 0 {
i -= 1;
match bytes[i] {
b')' => depth += 1,
b'(' => {
depth -= 1;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
}
None
}
fn find_fixed_height_calls(source: &str) -> Vec<usize> {
let mut starts = Vec::new();
let mut search_from = 0;
while let Some(rel) = source[search_from..].find(".height(") {
let dot = search_from + rel;
let after_paren = dot + ".height(".len();
let rest = source[after_paren..].trim_start();
let rest = rest.strip_prefix("layout::").unwrap_or(rest);
if rest.starts_with("BUTTON_HEIGHT") || rest.starts_with("SMALL_BUTTON_HEIGHT") {
let const_name = if rest.starts_with("SMALL_BUTTON_HEIGHT") {
"SMALL_BUTTON_HEIGHT"
} else {
"BUTTON_HEIGHT"
};
let after_const = rest[const_name.len()..].trim_start();
if after_const.starts_with(')') {
starts.push(dot);
}
}
search_from = after_paren;
}
starts
}
#[derive(Debug)]
struct Violation {
location: String,
reason: &'static str,
}
fn is_button_callee(callee: &str) -> bool {
let Some(rest) = callee.strip_suffix("button") else {
return false;
};
rest.is_empty() || !rest.ends_with(|c: char| c.is_alphanumeric() || c == '_')
}
fn check_source(path: &std::path::Path, clean_source: &str, out: &mut Vec<Violation>) {
for dot in find_fixed_height_calls(clean_source) {
let line = clean_source[..dot].matches('\n').count() + 1;
let location = format!("{}:{line}", path.display());
let before = clean_source[..dot].trim_end();
let Some(close_paren) = before.strip_suffix(')').map(|_| before.len() - 1) else {
out.push(Violation {
location,
reason: "`.height(...)` on a fixed height is not chained directly \
off a preceding call's closing paren",
});
continue;
};
let Some(open_paren) = matching_open_paren(clean_source, close_paren) else {
out.push(Violation {
location,
reason: "could not find the matching opening paren for the call \
`.height(...)` is chained onto",
});
continue;
};
let callee = clean_source[..open_paren].trim_end();
if !is_button_callee(callee) {
out.push(Violation {
location,
reason: "`.height(...)` on a fixed height is chained onto a call \
that is not `button(...)` — this guard only knows how to \
check button labels",
});
continue;
}
let args = &clean_source[open_paren + 1..close_paren];
if !args.contains("centered_button_label(") {
out.push(Violation {
location,
reason: "a fixed-height `button(...)`'s content does not go through \
`centered_button_label` — its label will sit at the top \
of the button, not centred",
});
}
}
}
#[test]
fn every_fixed_height_button_label_is_centered() {
let mut actual: Vec<String> = Vec::new();
let mut total_files = 0;
for dir in scan_dirs() {
let files = rust_files_under(&dir);
total_files += files.len();
for file in files.iter().filter(|f| is_scan_target(f)) {
let Ok(source) = std::fs::read_to_string(file) else {
continue;
};
let clean = blank_comment_lines(&source);
let mut violations = Vec::new();
check_source(file, &clean, &mut violations);
for v in violations {
actual.push(format!("{}: {}", v.location, v.reason));
}
}
}
assert!(
total_files > 20,
"found only {total_files} .rs files under the two scanned \
directories -- path resolution is broken (expected 20+ across \
view/ and widget/), not that nothing needed checking"
);
assert!(
actual.is_empty(),
"fixed-height button(s) found whose label is not centred via \
`centered_button_label` -- RFC-062 D1/R3 requires every \
`button(...).height(BUTTON_HEIGHT)` / `.height(SMALL_BUTTON_HEIGHT)` \
site to build its label through that helper:\n{actual:#?}"
);
}
}