• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 14477221649

15 Apr 2025 06:54PM UTC coverage: 39.362% (+9.8%) from 29.588%
14477221649

Pull #240

github

fasterthanlime
AH yes, the wrong path
Pull Request #240: Blacksmith cache

3987 of 10129 relevant lines covered (39.36%)

41.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/facet-dev/src/sample.rs
1
/// Expands and formats the sample code in `sample/` so we can
2
/// include it in the documentation.
3
pub fn cargo_expand_and_format() -> String {
×
4
    use std::io::Write;
5

6
    let workspace_dir = std::env::current_dir().unwrap();
×
7
    let sample_dir = workspace_dir.join("sample");
×
8

×
9
    // Run cargo expand command and measure execution time
×
10
    let start_time = std::time::Instant::now();
×
11

×
12
    // Command 1: cargo rustc for expansion
×
13
    let cargo_expand_output = std::process::Command::new("cargo")
×
14
        .env("RUSTC_BOOTSTRAP", "1") // Necessary for -Z flags
×
15
        .current_dir(&sample_dir) // Set working directory instead of changing it
×
16
        .arg("rustc")
×
17
        .arg("--target-dir")
×
18
        .arg("/tmp/facet-codegen-expand") // Use a temporary, less intrusive target dir
×
19
        .arg("--lib") // Expand the library crate in the current directory
×
20
        .arg("--") // Separator for rustc flags
×
21
        .arg("-Zunpretty=expanded") // The flag to expand macros
×
22
        .output() // Execute and capture output
×
23
        .expect("Failed to execute cargo rustc for expansion");
×
24

×
25
    // Check if cargo rustc succeeded
×
26
    if !cargo_expand_output.status.success() {
×
27
        panic!(
×
28
            "cargo rustc expansion failed:\n--- stderr ---\n{}\n--- stdout ---\n{}",
×
29
            String::from_utf8_lossy(&cargo_expand_output.stderr).trim(),
×
30
            String::from_utf8_lossy(&cargo_expand_output.stdout).trim()
×
31
        );
32
    }
×
33

×
34
    // Prepare the code for rustfmt: prepend the necessary lines
×
35
    let expanded_code = String::from_utf8(cargo_expand_output.stdout)
×
36
        .expect("Failed to convert cargo expand output to UTF-8 string");
×
37

×
38
    // Replace any ::facet:: references with crate::
×
39
    let expanded_code = expanded_code.replace("::facet::", "crate::");
×
40
    let expanded_code = expanded_code.replace("use facet::", "use crate::");
×
41

×
42
    let expanded_code = expanded_code.replace(
×
43
        "::impls::_core::marker::PhantomData",
×
44
        "::core::marker::PhantomData",
×
45
    );
×
46

×
47
    // Command 2: rustfmt to format the expanded code
×
48
    let mut rustfmt_cmd = std::process::Command::new("rustfmt")
×
49
        .arg("--edition")
×
50
        .arg("2024")
×
51
        .arg("--emit")
×
52
        .arg("stdout")
×
53
        .stdin(std::process::Stdio::piped()) // Prepare to pipe stdin
×
54
        .stdout(std::process::Stdio::piped()) // Capture stdout
×
55
        .stderr(std::process::Stdio::piped()) // Capture stderr
×
56
        .spawn()
×
57
        .expect("Failed to spawn rustfmt");
×
58

×
59
    // Write the combined code to rustfmt's stdin in a separate scope
×
60
    // to ensure stdin is closed, signaling EOF to rustfmt.
×
61
    {
×
62
        let mut stdin = rustfmt_cmd
×
63
            .stdin
×
64
            .take()
×
65
            .expect("Failed to open rustfmt stdin");
×
66
        stdin
×
67
            .write_all(expanded_code.as_bytes())
×
68
            .expect("Failed to write to rustfmt stdin");
×
69
    } // stdin is closed here
×
70

×
71
    // Wait for rustfmt to finish and collect its output
×
72
    let output = rustfmt_cmd
×
73
        .wait_with_output()
×
74
        .expect("Failed to wait for rustfmt");
×
75

×
76
    // Check if rustfmt succeeded (using the final 'output' variable)
×
77
    // Note: The original code only checked the final status, which might hide
×
78
    // the cargo expand error if rustfmt succeeds. We now check both stages.
×
79
    if !output.status.success() {
×
80
        panic!(
×
81
            "rustfmt failed:\n--- stderr ---\n{}\n--- stdout ---\n{}",
×
82
            String::from_utf8_lossy(&output.stderr).trim(),
×
83
            String::from_utf8_lossy(&output.stdout).trim()
×
84
        );
85
    }
×
86
    let _execution_time = start_time.elapsed();
×
87

×
88
    if !output.status.success() {
×
89
        panic!("Cargo expand command failed");
×
90
    }
×
91

×
92
    let expanded_code =
×
93
        String::from_utf8(output.stdout).expect("Failed to convert output to string");
×
94

×
95
    // First collect doc comments, then filter out lines we don't want
×
96
    let doc_comments = expanded_code
×
97
        .lines()
×
98
        .filter(|line| line.trim_start().starts_with("//!"))
×
99
        .collect::<Vec<_>>()
×
100
        .join("\n");
×
101

×
102
    let expanded_code = expanded_code
×
103
        .lines()
×
104
        .filter(|line| {
×
105
            let trimmed = line.trim_start();
×
106
            !trimmed.starts_with("#![")
×
107
                && !trimmed.starts_with("#[facet(")
×
108
                && !trimmed.starts_with("#[macro_use]")
×
109
                && !trimmed.starts_with("//!")
×
110
        })
×
111
        .collect::<Vec<_>>()
×
112
        .join("\n");
×
113
    let expanded_code = format!("{}\n#![allow(warnings)]\n{}", doc_comments, expanded_code);
×
114

×
115
    // Ensure a trailing newline for consistency
×
116
    if expanded_code.is_empty() {
×
117
        String::new()
×
118
    } else {
119
        format!("{}\n", expanded_code)
×
120
    }
121
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc