Coverage Report

Created: 2026-02-14 07:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/wasm-encoder-0.245.0/src/component/names.rs
Line
Count
Source
1
use alloc::borrow::Cow;
2
use alloc::vec::Vec;
3
4
use super::*;
5
use crate::{ExportKind, NameMap, SectionId, encoding_size};
6
7
/// Encoding for the `component-name` custom section which assigns
8
/// human-readable names to items within a component.
9
#[derive(Clone, Debug, Default)]
10
pub struct ComponentNameSection {
11
    bytes: Vec<u8>,
12
}
13
14
enum Subsection {
15
    Component,
16
    Decls,
17
}
18
19
impl ComponentNameSection {
20
    /// Creates a new blank `name` custom section.
21
0
    pub fn new() -> Self {
22
0
        Self::default()
23
0
    }
24
25
    /// Appends a component name subsection to this section.
26
    ///
27
    /// This will indicate that the name of the entire component should be the
28
    /// `name` specified. Note that this should be encoded first before other
29
    /// subsections.
30
0
    pub fn component(&mut self, name: &str) {
31
0
        let len = encoding_size(u32::try_from(name.len()).unwrap());
32
0
        self.subsection_header(Subsection::Component, len + name.len());
33
0
        name.encode(&mut self.bytes);
34
0
    }
35
36
    /// Appends a decls name subsection to name core functions within the
37
    /// component.
38
0
    pub fn core_funcs(&mut self, names: &NameMap) {
39
0
        self.core_decls(ExportKind::Func as u8, names)
40
0
    }
41
42
    /// Appends a decls name subsection to name core tables within the
43
    /// component.
44
0
    pub fn core_tables(&mut self, names: &NameMap) {
45
0
        self.core_decls(ExportKind::Table as u8, names)
46
0
    }
47
48
    /// Appends a decls name subsection to name core memories within the
49
    /// component.
50
0
    pub fn core_memories(&mut self, names: &NameMap) {
51
0
        self.core_decls(ExportKind::Memory as u8, names)
52
0
    }
53
54
    /// Appends a decls name subsection to name core globals within the
55
    /// component.
56
0
    pub fn core_globals(&mut self, names: &NameMap) {
57
0
        self.core_decls(ExportKind::Global as u8, names)
58
0
    }
59
60
    /// Appends a decls name subsection to name core tags within the
61
    /// component.
62
0
    pub fn core_tags(&mut self, names: &NameMap) {
63
0
        self.core_decls(ExportKind::Tag as u8, names)
64
0
    }
65
66
    /// Appends a decls name subsection to name core types within the
67
    /// component.
68
0
    pub fn core_types(&mut self, names: &NameMap) {
69
0
        self.core_decls(CORE_TYPE_SORT, names)
70
0
    }
71
72
    /// Appends a decls name subsection to name core modules within the
73
    /// component.
74
0
    pub fn core_modules(&mut self, names: &NameMap) {
75
0
        self.core_decls(CORE_MODULE_SORT, names)
76
0
    }
77
78
    /// Appends a decls name subsection to name core instances within the
79
    /// component.
80
0
    pub fn core_instances(&mut self, names: &NameMap) {
81
0
        self.core_decls(CORE_INSTANCE_SORT, names)
82
0
    }
83
84
    /// Appends a decls name subsection to name component functions within the
85
    /// component.
86
0
    pub fn funcs(&mut self, names: &NameMap) {
87
0
        self.component_decls(FUNCTION_SORT, names)
88
0
    }
89
90
    /// Appends a decls name subsection to name component values within the
91
    /// component.
92
0
    pub fn values(&mut self, names: &NameMap) {
93
0
        self.component_decls(VALUE_SORT, names)
94
0
    }
95
96
    /// Appends a decls name subsection to name component type within the
97
    /// component.
98
0
    pub fn types(&mut self, names: &NameMap) {
99
0
        self.component_decls(TYPE_SORT, names)
100
0
    }
101
102
    /// Appends a decls name subsection to name components within the
103
    /// component.
104
0
    pub fn components(&mut self, names: &NameMap) {
105
0
        self.component_decls(COMPONENT_SORT, names)
106
0
    }
107
108
    /// Appends a decls name subsection to name component instances within the
109
    /// component.
110
0
    pub fn instances(&mut self, names: &NameMap) {
111
0
        self.component_decls(INSTANCE_SORT, names)
112
0
    }
113
114
    /// Appends a raw subsection with the given id and data.
115
0
    pub fn raw(&mut self, id: u8, data: &[u8]) {
116
0
        self.bytes.push(id);
117
0
        data.encode(&mut self.bytes);
118
0
    }
119
120
0
    fn component_decls(&mut self, kind: u8, names: &NameMap) {
121
0
        self.subsection_header(Subsection::Decls, 1 + names.size());
122
0
        self.bytes.push(kind);
123
0
        names.encode(&mut self.bytes);
124
0
    }
125
126
0
    fn core_decls(&mut self, kind: u8, names: &NameMap) {
127
0
        self.subsection_header(Subsection::Decls, 2 + names.size());
128
0
        self.bytes.push(CORE_SORT);
129
0
        self.bytes.push(kind);
130
0
        names.encode(&mut self.bytes);
131
0
    }
132
133
0
    fn subsection_header(&mut self, id: Subsection, len: usize) {
134
0
        self.bytes.push(id as u8);
135
0
        len.encode(&mut self.bytes);
136
0
    }
137
138
    /// Returns whether this section is empty, or nothing has been encoded.
139
0
    pub fn is_empty(&self) -> bool {
140
0
        self.bytes.is_empty()
141
0
    }
142
143
    /// View the encoded section as a CustomSection.
144
0
    pub fn as_custom<'a>(&'a self) -> CustomSection<'a> {
145
0
        CustomSection {
146
0
            name: "component-name".into(),
147
0
            data: Cow::Borrowed(&self.bytes),
148
0
        }
149
0
    }
150
}
151
152
impl Encode for ComponentNameSection {
153
0
    fn encode(&self, sink: &mut Vec<u8>) {
154
0
        self.as_custom().encode(sink);
155
0
    }
156
}
157
158
impl ComponentSection for ComponentNameSection {
159
0
    fn id(&self) -> u8 {
160
0
        SectionId::Custom.into()
161
0
    }
162
}