Coverage Report

Created: 2026-07-19 07:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm-tools/crates/wasmprinter/src/print.rs
Line
Count
Source
1
use std::fmt;
2
use std::io;
3
use termcolor::{Color, ColorSpec};
4
5
/// Trait used to print WebAssembly modules in this crate.
6
///
7
/// Instances of this trait are passed to
8
/// [`Config::print`](super::Config::print). Instances of this trait are where
9
/// the output of a WebAssembly binary goes.
10
///
11
/// Note that this trait has built-in adapters in the `wasmprinter` crate:
12
///
13
/// * For users of [`std::io::Write`] use [`PrintIoWrite`].
14
/// * For users of [`std::fmt::Write`] use [`PrintFmtWrite`].
15
pub trait Print {
16
    /// Writes the given string `s` in its entirety.
17
    ///
18
    /// Returns an error for any I/O error.
19
    fn write_str(&mut self, s: &str) -> io::Result<()>;
20
21
    /// Indicates that a newline is being printed.
22
    ///
23
    /// This can be overridden to hook into the offset at which lines are
24
    /// printed.
25
10.1M
    fn newline(&mut self) -> io::Result<()> {
26
10.1M
        self.write_str("\n")
27
10.1M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::newline
Line
Count
Source
25
10.1M
    fn newline(&mut self) -> io::Result<()> {
26
10.1M
        self.write_str("\n")
27
10.1M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::newline
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::newline
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::newline
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::newline
28
29
    /// Indicates that a new line in the output is starting at the
30
    /// `binary_offset` provided.
31
    ///
32
    /// Not all new lines have a binary offset associated with them but this
33
    /// method should be called for new lines in the output. This enables
34
    /// correlating binary offsets to lines in the output.
35
10.1M
    fn start_line(&mut self, binary_offset: Option<usize>) {
36
10.1M
        let _ = binary_offset;
37
10.1M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_line
Line
Count
Source
35
10.1M
    fn start_line(&mut self, binary_offset: Option<usize>) {
36
10.1M
        let _ = binary_offset;
37
10.1M
    }
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_line
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_line
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_line
38
39
    /// Enables usage of `write!` with this trait.
40
24.2M
    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
41
        struct Adapter<'a, T: ?Sized + 'a> {
42
            inner: &'a mut T,
43
            error: io::Result<()>,
44
        }
45
46
        impl<T: Print + ?Sized> fmt::Write for Adapter<'_, T> {
47
39.8M
            fn write_str(&mut self, s: &str) -> fmt::Result {
48
39.8M
                match self.inner.write_str(s) {
49
39.8M
                    Ok(()) => Ok(()),
50
0
                    Err(e) => {
51
0
                        self.error = Err(e);
52
0
                        Err(fmt::Error)
53
                    }
54
                }
55
39.8M
            }
<wasmprinter::print::Print::write_fmt::Adapter<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String>> as core::fmt::Write>::write_str
Line
Count
Source
47
29.9M
            fn write_str(&mut self, s: &str) -> fmt::Result {
48
29.9M
                match self.inner.write_str(s) {
49
29.9M
                    Ok(()) => Ok(()),
50
0
                    Err(e) => {
51
0
                        self.error = Err(e);
52
0
                        Err(fmt::Error)
53
                    }
54
                }
55
29.9M
            }
Unexecuted instantiation: <wasmprinter::print::Print::write_fmt::Adapter<wasmprinter::print::PrintFmtWrite<alloc::string::String>> as core::fmt::Write>::write_str
Unexecuted instantiation: <wasmprinter::print::Print::write_fmt::Adapter<wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>>> as core::fmt::Write>::write_str
<wasmprinter::print::Print::write_fmt::Adapter<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>>> as core::fmt::Write>::write_str
Line
Count
Source
47
9.89M
            fn write_str(&mut self, s: &str) -> fmt::Result {
48
9.89M
                match self.inner.write_str(s) {
49
9.89M
                    Ok(()) => Ok(()),
50
0
                    Err(e) => {
51
0
                        self.error = Err(e);
52
0
                        Err(fmt::Error)
53
                    }
54
                }
55
9.89M
            }
Unexecuted instantiation: <wasmprinter::print::Print::write_fmt::Adapter<<wasmprinter::Config>::offsets_and_lines::TrackingPrint> as core::fmt::Write>::write_str
56
        }
57
58
24.2M
        let mut output = Adapter {
59
24.2M
            inner: self,
60
24.2M
            error: Ok(()),
61
24.2M
        };
62
24.2M
        match fmt::write(&mut output, args) {
63
24.2M
            Ok(()) => Ok(()),
64
0
            Err(..) => output.error,
65
        }
66
24.2M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::write_fmt
Line
Count
Source
40
18.7M
    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
41
        struct Adapter<'a, T: ?Sized + 'a> {
42
            inner: &'a mut T,
43
            error: io::Result<()>,
44
        }
45
46
        impl<T: Print + ?Sized> fmt::Write for Adapter<'_, T> {
47
            fn write_str(&mut self, s: &str) -> fmt::Result {
48
                match self.inner.write_str(s) {
49
                    Ok(()) => Ok(()),
50
                    Err(e) => {
51
                        self.error = Err(e);
52
                        Err(fmt::Error)
53
                    }
54
                }
55
            }
56
        }
57
58
18.7M
        let mut output = Adapter {
59
18.7M
            inner: self,
60
18.7M
            error: Ok(()),
61
18.7M
        };
62
18.7M
        match fmt::write(&mut output, args) {
63
18.7M
            Ok(()) => Ok(()),
64
0
            Err(..) => output.error,
65
        }
66
18.7M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::write_fmt
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::write_fmt
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::write_fmt
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::write_fmt
Line
Count
Source
40
5.48M
    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
41
        struct Adapter<'a, T: ?Sized + 'a> {
42
            inner: &'a mut T,
43
            error: io::Result<()>,
44
        }
45
46
        impl<T: Print + ?Sized> fmt::Write for Adapter<'_, T> {
47
            fn write_str(&mut self, s: &str) -> fmt::Result {
48
                match self.inner.write_str(s) {
49
                    Ok(()) => Ok(()),
50
                    Err(e) => {
51
                        self.error = Err(e);
52
                        Err(fmt::Error)
53
                    }
54
                }
55
            }
56
        }
57
58
5.48M
        let mut output = Adapter {
59
5.48M
            inner: self,
60
5.48M
            error: Ok(()),
61
5.48M
        };
62
5.48M
        match fmt::write(&mut output, args) {
63
5.48M
            Ok(()) => Ok(()),
64
0
            Err(..) => output.error,
65
        }
66
5.48M
    }
67
68
    /// Helper to print a custom section, if needed.
69
    ///
70
    /// If this output has a custom means of printing a custom section then this
71
    /// can be used to override the default. If `Ok(true)` is returned then the
72
    /// section will be considered to be printed. Otherwise an `Ok(false)`
73
    /// return value indicates that the default printing should happen.
74
0
    fn print_custom_section(
75
0
        &mut self,
76
0
        name: &str,
77
0
        binary_offset: usize,
78
0
        data: &[u8],
79
0
    ) -> io::Result<bool> {
80
0
        let _ = (name, binary_offset, data);
81
0
        Ok(false)
82
0
    }
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::print_custom_section
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::print_custom_section
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::print_custom_section
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::print_custom_section
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::print_custom_section
83
84
    /// Sets the colors settings for literals (`"foo"`) to be printed.
85
1.42M
    fn start_literal(&mut self) -> io::Result<()> {
86
1.42M
        Ok(())
87
1.42M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_literal
Line
Count
Source
85
1.42M
    fn start_literal(&mut self) -> io::Result<()> {
86
1.42M
        Ok(())
87
1.42M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::start_literal
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_literal
88
89
    /// Sets the colors settings for a name (`$foo`) to be printed.
90
3.76M
    fn start_name(&mut self) -> io::Result<()> {
91
3.76M
        Ok(())
92
3.76M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_name
Line
Count
Source
90
3.76M
    fn start_name(&mut self) -> io::Result<()> {
91
3.76M
        Ok(())
92
3.76M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::start_name
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_name
93
94
    /// Sets the colors settings for a keyword (`(module ...)`) to be printed.
95
4.02M
    fn start_keyword(&mut self) -> io::Result<()> {
96
4.02M
        Ok(())
97
4.02M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_keyword
Line
Count
Source
95
4.02M
    fn start_keyword(&mut self) -> io::Result<()> {
96
4.02M
        Ok(())
97
4.02M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::start_keyword
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_keyword
98
99
    /// Sets the colors settings for a type (`(param i32)`) to be printed.
100
6.64M
    fn start_type(&mut self) -> io::Result<()> {
101
6.64M
        Ok(())
102
6.64M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_type
Line
Count
Source
100
6.64M
    fn start_type(&mut self) -> io::Result<()> {
101
6.64M
        Ok(())
102
6.64M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::start_type
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_type
103
104
    /// Sets the colors settings for a comment (`;; ...`) to be printed.
105
1.02M
    fn start_comment(&mut self) -> io::Result<()> {
106
1.02M
        Ok(())
107
1.02M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::start_comment
Line
Count
Source
105
1.02M
    fn start_comment(&mut self) -> io::Result<()> {
106
1.02M
        Ok(())
107
1.02M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::start_comment
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::start_comment
108
109
    /// Resets colors settings to the default.
110
16.5M
    fn reset_color(&mut self) -> io::Result<()> {
111
16.5M
        Ok(())
112
16.5M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::reset_color
Line
Count
Source
110
16.5M
    fn reset_color(&mut self) -> io::Result<()> {
111
16.5M
        Ok(())
112
16.5M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::reset_color
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::reset_color
113
114
    /// Returns `true` if the device uses colors without interacting synchronously with a terminal (e.g. ANSI)
115
9.53M
    fn supports_async_color(&self) -> bool {
116
9.53M
        false
117
9.53M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::supports_async_color
Line
Count
Source
115
9.53M
    fn supports_async_color(&self) -> bool {
116
9.53M
        false
117
9.53M
    }
Unexecuted instantiation: <<wasmprinter::Config>::offsets_and_lines::TrackingPrint as wasmprinter::print::Print>::supports_async_color
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::supports_async_color
118
}
119
120
/// An adapter between the [`std::io::Write`] trait and [`Print`].
121
pub struct PrintIoWrite<T>(pub T);
122
123
impl<T> Print for PrintIoWrite<T>
124
where
125
    T: io::Write,
126
{
127
0
    fn write_str(&mut self, s: &str) -> io::Result<()> {
128
0
        self.0.write_all(s.as_bytes())
129
0
    }
130
}
131
132
/// An adapter between the [`std::fmt::Write`] trait and [`Print`].
133
pub struct PrintFmtWrite<T>(pub T);
134
135
impl<T> Print for PrintFmtWrite<T>
136
where
137
    T: fmt::Write,
138
{
139
241M
    fn write_str(&mut self, s: &str) -> io::Result<()> {
140
241M
        match self.0.write_str(s) {
141
241M
            Ok(()) => Ok(()),
142
0
            Err(fmt::Error) => Err(io::Error::new(
143
0
                io::ErrorKind::Other,
144
0
                "failed to write string",
145
0
            )),
146
        }
147
241M
    }
<wasmprinter::print::PrintFmtWrite<&mut alloc::string::String> as wasmprinter::print::Print>::write_str
Line
Count
Source
139
241M
    fn write_str(&mut self, s: &str) -> io::Result<()> {
140
241M
        match self.0.write_str(s) {
141
241M
            Ok(()) => Ok(()),
142
0
            Err(fmt::Error) => Err(io::Error::new(
143
0
                io::ErrorKind::Other,
144
0
                "failed to write string",
145
0
            )),
146
        }
147
241M
    }
Unexecuted instantiation: <wasmprinter::print::PrintFmtWrite<alloc::string::String> as wasmprinter::print::Print>::write_str
148
}
149
150
/// An adapter between the [`termcolor::WriteColor`] trait and [`Print`].
151
pub struct PrintTermcolor<T>(pub T);
152
153
impl<T> Print for PrintTermcolor<T>
154
where
155
    T: termcolor::WriteColor,
156
{
157
19.1M
    fn write_str(&mut self, s: &str) -> io::Result<()> {
158
19.1M
        self.0.write_all(s.as_bytes())
159
19.1M
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::write_str
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::write_str
Line
Count
Source
157
19.1M
    fn write_str(&mut self, s: &str) -> io::Result<()> {
158
19.1M
        self.0.write_all(s.as_bytes())
159
19.1M
    }
160
161
1.80M
    fn start_name(&mut self) -> io::Result<()> {
162
1.80M
        self.0
163
1.80M
            .set_color(ColorSpec::new().set_fg(Some(Color::Magenta)))
164
1.80M
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_name
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_name
Line
Count
Source
161
1.80M
    fn start_name(&mut self) -> io::Result<()> {
162
1.80M
        self.0
163
1.80M
            .set_color(ColorSpec::new().set_fg(Some(Color::Magenta)))
164
1.80M
    }
165
166
1.13M
    fn start_literal(&mut self) -> io::Result<()> {
167
1.13M
        self.0.set_color(ColorSpec::new().set_fg(Some(Color::Red)))
168
1.13M
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_literal
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_literal
Line
Count
Source
166
1.13M
    fn start_literal(&mut self) -> io::Result<()> {
167
1.13M
        self.0.set_color(ColorSpec::new().set_fg(Some(Color::Red)))
168
1.13M
    }
169
170
413k
    fn start_keyword(&mut self) -> io::Result<()> {
171
413k
        self.0.set_color(
172
413k
            ColorSpec::new()
173
413k
                .set_fg(Some(Color::Yellow))
174
413k
                .set_bold(true)
175
413k
                .set_intense(true),
176
        )
177
413k
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_keyword
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_keyword
Line
Count
Source
170
413k
    fn start_keyword(&mut self) -> io::Result<()> {
171
413k
        self.0.set_color(
172
413k
            ColorSpec::new()
173
413k
                .set_fg(Some(Color::Yellow))
174
413k
                .set_bold(true)
175
413k
                .set_intense(true),
176
        )
177
413k
    }
178
179
314k
    fn start_type(&mut self) -> io::Result<()> {
180
314k
        self.0.set_color(
181
314k
            ColorSpec::new()
182
314k
                .set_fg(Some(Color::Green))
183
314k
                .set_bold(true)
184
314k
                .set_intense(true),
185
        )
186
314k
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_type
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_type
Line
Count
Source
179
314k
    fn start_type(&mut self) -> io::Result<()> {
180
314k
        self.0.set_color(
181
314k
            ColorSpec::new()
182
314k
                .set_fg(Some(Color::Green))
183
314k
                .set_bold(true)
184
314k
                .set_intense(true),
185
        )
186
314k
    }
187
188
1.01M
    fn start_comment(&mut self) -> io::Result<()> {
189
1.01M
        self.0.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)))
190
1.01M
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_comment
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::start_comment
Line
Count
Source
188
1.01M
    fn start_comment(&mut self) -> io::Result<()> {
189
1.01M
        self.0.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)))
190
1.01M
    }
191
192
4.32M
    fn reset_color(&mut self) -> io::Result<()> {
193
4.32M
        self.0.reset()
194
4.32M
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::reset_color
<wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::reset_color
Line
Count
Source
192
4.32M
    fn reset_color(&mut self) -> io::Result<()> {
193
4.32M
        self.0.reset()
194
4.32M
    }
195
196
0
    fn supports_async_color(&self) -> bool {
197
0
        self.0.supports_color() && !self.0.is_synchronous()
198
0
    }
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::Ansi<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::supports_async_color
Unexecuted instantiation: <wasmprinter::print::PrintTermcolor<termcolor::NoColor<alloc::vec::Vec<u8>>> as wasmprinter::print::Print>::supports_async_color
199
}