Skip to content

Commit a94f2ae

Browse files
committed
Code review feedback.
1 parent 6d44da5 commit a94f2ae

5 files changed

Lines changed: 56 additions & 17 deletions

File tree

crates/wac-graph/src/graph.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,17 @@ impl RegisteredPackage {
294294
#[derive(Debug, Clone)]
295295
pub struct Node {
296296
/// The node kind.
297-
pub kind: NodeKind,
297+
kind: NodeKind,
298298
/// The package associated with the node, if any.
299-
pub package: Option<PackageId>,
299+
package: Option<PackageId>,
300300
/// The item kind of the node.
301-
pub item_kind: ItemKind,
301+
item_kind: ItemKind,
302302
/// The optional name to associate with the node.
303303
///
304304
/// When the graph is encoded, node names are recorded in a `names` custom section.
305-
pub name: Option<String>,
305+
name: Option<String>,
306306
/// The name to use for exporting the node.
307-
pub export: Option<String>,
307+
export: Option<String>,
308308
}
309309

310310
impl Node {
@@ -318,13 +318,49 @@ impl Node {
318318
}
319319
}
320320

321-
fn import_name(&self) -> Option<&str> {
321+
/// Gets the kind of the node.
322+
pub fn kind(&self) -> &NodeKind {
323+
&self.kind
324+
}
325+
326+
/// Gets the package id associated with the node.
327+
///
328+
/// Returns `None` if the node is not directly associated with a package.
329+
pub fn package(&self) -> Option<PackageId> {
330+
self.package
331+
}
332+
333+
/// Gets the item kind of the node.
334+
pub fn item_kind(&self) -> ItemKind {
335+
self.item_kind
336+
}
337+
338+
/// Gets the name of the node.
339+
///
340+
/// Node names are encoded in a `names` custom section.
341+
///
342+
/// Returns `None` if the node is unnamed.
343+
pub fn name(&self) -> Option<&str> {
344+
self.name.as_deref()
345+
}
346+
347+
/// Gets the import name of the node.
348+
///
349+
/// Returns `Some` if the node is an import or `None` if the node is not an import.
350+
pub fn import_name(&self) -> Option<&str> {
322351
match &self.kind {
323352
NodeKind::Import(name) => Some(name),
324353
_ => None,
325354
}
326355
}
327356

357+
/// Gets the export name of the node.
358+
///
359+
/// Returns `None` if the node is not exported.
360+
pub fn export_name(&self) -> Option<&str> {
361+
self.export.as_deref()
362+
}
363+
328364
fn add_satisfied_arg(&mut self, index: usize) {
329365
match &mut self.kind {
330366
NodeKind::Instantiation(satisfied) => {
@@ -532,7 +568,10 @@ impl CompositionGraph {
532568
let key = entry.package.as_ref().unwrap().key();
533569
log::debug!("unregistering package `{key}` with the graph");
534570
let prev = self.package_map.remove(&key as &dyn BorrowedKey);
535-
assert!(prev.is_some());
571+
assert!(
572+
prev.is_some(),
573+
"package should exist in the package map (this is a bug)"
574+
);
536575

537576
// Finally free the package
538577
*entry = RegisteredPackage::new(entry.generation.wrapping_add(1));

crates/wac-parser/src/resolution.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ enum Item {
737737
impl Item {
738738
fn kind(&self, graph: &CompositionGraph) -> ItemKind {
739739
match self {
740-
Self::Node(id) => graph[*id].item_kind,
740+
Self::Node(id) => graph[*id].item_kind(),
741741
Self::Use(ty) => ItemKind::Type(*ty),
742742
Self::Type(ty) => ItemKind::Type(*ty),
743743
}
@@ -788,7 +788,7 @@ impl State {
788788
if let Item::Node(node) = item {
789789
// Use only the first name encountered for the node, ignoring
790790
// aliasing in the form of `let x = y;`
791-
if self.graph[node].name.is_none() {
791+
if self.graph[node].name().is_none() {
792792
self.graph.set_node_name(node, id.string.to_owned());
793793
}
794794
}
@@ -1133,10 +1133,10 @@ impl<'a> AstResolver<'a> {
11331133
) -> Result<(), Error> {
11341134
if let Some((item, prev_span)) = state.root_scope().get(&name) {
11351135
let node = &state.graph[item.node()];
1136-
if let NodeKind::Definition = node.kind {
1136+
if let NodeKind::Definition = node.kind() {
11371137
return Err(Error::ExportConflict {
11381138
name,
1139-
kind: node.item_kind.desc(state.graph.types()).to_string(),
1139+
kind: node.item_kind().desc(state.graph.types()).to_string(),
11401140
span,
11411141
definition: prev_span,
11421142
help: if !show_hint {
@@ -2720,7 +2720,7 @@ impl<'a> AstResolver<'a> {
27202720
for (name, import) in state
27212721
.graph
27222722
.node_ids()
2723-
.filter_map(|n| match &state.graph[n].kind {
2723+
.filter_map(|n| match &state.graph[n].kind() {
27242724
NodeKind::Import(name) => Some((name, n)),
27252725
_ => None,
27262726
})
@@ -2738,7 +2738,7 @@ impl<'a> AstResolver<'a> {
27382738
.is_subtype(
27392739
expected.promote(),
27402740
state.graph.types(),
2741-
state.graph[import].item_kind,
2741+
state.graph[import].item_kind(),
27422742
state.graph.types(),
27432743
)
27442744
.map_err(|e| Error::TargetMismatch {
@@ -2767,7 +2767,7 @@ impl<'a> AstResolver<'a> {
27672767

27682768
checker
27692769
.is_subtype(
2770-
state.graph[export].item_kind,
2770+
state.graph[export].item_kind(),
27712771
state.graph.types(),
27722772
expected.promote(),
27732773
state.graph.types(),

src/commands/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct EncodeCommand {
6464
#[clap(long, value_name = "URL")]
6565
pub registry: Option<String>,
6666

67-
/// The path to the source file.
67+
/// The path to the source WAC file.
6868
#[clap(value_name = "PATH")]
6969
pub path: PathBuf,
7070
}

src/commands/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use wac_parser::Document;
88
#[derive(Args)]
99
#[clap(disable_version_flag = true)]
1010
pub struct ParseCommand {
11-
/// The path to the source file.
11+
/// The path to the source WAC file.
1212
#[clap(value_name = "PATH")]
1313
pub path: PathBuf,
1414
}

src/commands/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct ResolveCommand {
3636
#[clap(long, value_name = "URL")]
3737
pub registry: Option<String>,
3838

39-
/// The path to the source file.
39+
/// The path to the source WAC file.
4040
#[clap(value_name = "PATH")]
4141
pub path: PathBuf,
4242
}

0 commit comments

Comments
 (0)