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

facet-rs / facet / 14532623925

18 Apr 2025 08:53AM UTC coverage: 43.982% (-0.2%) from 44.183%
14532623925

push

github

fasterthanlime
Upgrades

4688 of 10659 relevant lines covered (43.98%)

54.82 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
×
8
        .join("outside-workspace")
×
9
        .join("sample-for-expand");
×
10

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

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

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

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

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

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

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

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

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

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

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

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

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

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

×
117
    // Ensure a trailing newline for consistency
×
118
    if expanded_code.is_empty() {
×
119
        String::new()
×
120
    } else {
121
        format!("{}\n", expanded_code)
×
122
    }
123
}
×
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