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

facet-rs / facet / 14468617911

15 Apr 2025 11:47AM UTC coverage: 29.024% (-1.8%) from 30.835%
14468617911

Pull #225

github

web-flow
Merge d5543fd61 into 00641afc4
Pull Request #225: Provide best-of-class precommit hook (facet-dev)

4 of 940 new or added lines in 9 files covered. (0.43%)

1 existing line in 1 file now uncovered.

2032 of 7001 relevant lines covered (29.02%)

24.21 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.
NEW
3
pub fn cargo_expand_and_format() -> String {
×
4
    use std::io::Write;
5

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

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

12
    // Command 1: cargo rustc for expansion
NEW
13
    let cargo_expand_output = std::process::Command::new("cargo")
×
14
        .env("RUSTC_BOOTSTRAP", "1") // Necessary for -Z flags
NEW
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
NEW
26
    if !cargo_expand_output.status.success() {
×
NEW
27
        panic!(
×
NEW
28
            "cargo rustc expansion failed:\n--- stderr ---\n{}\n--- stdout ---\n{}",
×
NEW
29
            String::from_utf8_lossy(&cargo_expand_output.stderr).trim(),
×
NEW
30
            String::from_utf8_lossy(&cargo_expand_output.stdout).trim()
×
31
        );
32
    }
33

34
    // Prepare the code for rustfmt: prepend the necessary lines
NEW
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::
NEW
39
    let expanded_code = expanded_code.replace("::facet::", "crate::");
×
NEW
40
    let expanded_code = expanded_code.replace("use facet::", "use crate::");
×
41

NEW
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
NEW
48
    let mut rustfmt_cmd = std::process::Command::new("rustfmt")
×
49
        .arg("--edition")
50
        .arg("2024")
51
        .arg("--emit")
52
        .arg("stdout")
NEW
53
        .stdin(std::process::Stdio::piped()) // Prepare to pipe stdin
×
NEW
54
        .stdout(std::process::Stdio::piped()) // Capture stdout
×
NEW
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
    {
NEW
62
        let mut stdin = rustfmt_cmd
×
NEW
63
            .stdin
×
64
            .take()
65
            .expect("Failed to open rustfmt stdin");
NEW
66
        stdin
×
NEW
67
            .write_all(expanded_code.as_bytes())
×
68
            .expect("Failed to write to rustfmt stdin");
NEW
69
    } // stdin is closed here
×
70

71
    // Wait for rustfmt to finish and collect its output
NEW
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.
NEW
79
    if !output.status.success() {
×
NEW
80
        panic!(
×
NEW
81
            "rustfmt failed:\n--- stderr ---\n{}\n--- stdout ---\n{}",
×
NEW
82
            String::from_utf8_lossy(&output.stderr).trim(),
×
NEW
83
            String::from_utf8_lossy(&output.stdout).trim()
×
84
        );
85
    }
NEW
86
    let _execution_time = start_time.elapsed();
×
87

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

NEW
92
    let expanded_code =
×
NEW
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
NEW
96
    let doc_comments = expanded_code
×
97
        .lines()
NEW
98
        .filter(|line| line.trim_start().starts_with("//!"))
×
99
        .collect::<Vec<_>>()
100
        .join("\n");
101

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

115
    // Ensure a trailing newline for consistency
NEW
116
    if expanded_code.is_empty() {
×
NEW
117
        String::new()
×
118
    } else {
NEW
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