tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

⎇ main scripts/format-fixtures/src/main.rs Rust · 63 líneas · 2.0 KB Historia Quién tocó qué crudo

Sort test fixtures and generated tests into folders (#514) 24b8faf43 · aemreaydin ·

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6use regex::{Captures, Regex};
7use walkdir::WalkDir;
8
9fn main() {
10    let fixtures_root = std::env::var("FIXTURE_DIR").map(PathBuf::from).unwrap_or_else(|_| {
11        let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
12        let repo_root = root_dir.parent().and_then(Path::parent).unwrap();
13        repo_root.join("test_fixtures")
14    });
15
16    println!("reading test fixtures from disk");
17    let fixtures: Vec<_> = WalkDir::new(fixtures_root)
18        .into_iter()
19        .filter_map(|a| a.ok())
20        .filter(|f| f.path().is_file() && f.path().extension().map(|p| p == "html").unwrap_or(false))
21        .map(|f| {
22            let fixture_path = f.path().to_path_buf();
23            let name = fixture_path.file_stem().unwrap().to_str().unwrap().to_string();
24            (name, fixture_path)
25        })
26        .collect();
27
28    let style_regex = Regex::new(r#"style="([^"]*)""#).unwrap();
29
30    for (_, fixture_path) in fixtures {
31        let content = fs::read_to_string(&fixture_path).unwrap();
32
33        let formatted_content = style_regex.replace_all(&content, |captures: &Captures| {
34            let style_attr = captures.get(0).unwrap().as_str();
35            format_style_attr(style_attr)
36        });
37
38        fs::write(&fixture_path, formatted_content.as_ref()).unwrap();
39    }
40}
41
42fn format_style_attr(input: &str) -> String {
43    let mut attr = String::with_capacity(1024);
44    attr.push_str("style=\"");
45
46    let input = &input[7..(input.len() - 1)];
47    let declarations = input.split(';').map(|decl| decl.trim()).filter(|decl| !decl.is_empty());
48    let mut has_declarations = false;
49    for decl in declarations {
50        has_declarations = true;
51        let (key, value) = decl.split_once(':').unwrap();
52        attr.push_str(key.trim());
53        attr.push_str(": ");
54        attr.push_str(value.trim());
55        attr.push_str("; ");
56    }
57    if has_declarations {
58        attr.pop();
59    }
60    attr.push('"');
61
62    attr
63}