Skip to content

Commit

Permalink
feat: Add position property to StructField, Variant, and subtypes (#764)
Browse files Browse the repository at this point in the history
close: #758

it seems we have to add another vertex kind to get the struct field
position? let me know if there is a better way to do it!

I would add tests for struct fields, union fields, enum struct variants
fields/tuple variants fields/plain variants/unit variants if we think
the code is good
  • Loading branch information
Frank-III authored and obi1kenobi committed Feb 9, 2025
1 parent 99fceeb commit 29e8d53
Show file tree
Hide file tree
Showing 14 changed files with 700 additions and 17 deletions.
46 changes: 32 additions & 14 deletions src/adapter/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,11 @@ pub(super) fn resolve_struct_edge<'a, V: AsVertex<Vertex<'a>> + 'a>(
rustdoc_types::StructKind::Plain { fields, .. } => Box::new(fields.iter()),
};

Box::new(field_ids_iter.map(move |field_id| {
origin.make_item_vertex(item_index.get(field_id).expect("missing item"))
Box::new(field_ids_iter.enumerate().map(move |(index, field_id)| {
origin.make_positioned_item_vertex(
index + 1,
item_index.get(field_id).expect("missing item"),
)
}))
}),
_ => unreachable!("resolve_struct_edge {edge_name}"),
Expand Down Expand Up @@ -418,19 +421,25 @@ pub(super) fn resolve_variant_edge<'a, V: AsVertex<Vertex<'a>> + 'a>(
match &item.kind {
VariantKind::Plain => Box::new(std::iter::empty()),
VariantKind::Tuple(fields) => {
Box::new(fields.iter().filter(|x| x.is_some()).map(move |field_id| {
origin.make_item_vertex(
item_index
.get(field_id.as_ref().unwrap())
.expect("missing item"),
)
}))
Box::new(fields.iter().filter(|x| x.is_some()).enumerate().map(
move |(index, field_id)| {
origin.make_positioned_item_vertex(
index + 1,
item_index
.get(field_id.as_ref().unwrap())
.expect("missing item"),
)
},
))
}
VariantKind::Struct {
fields,
has_stripped_fields: _,
} => Box::new(fields.iter().map(move |field_id| {
origin.make_item_vertex(item_index.get(field_id).expect("missing item"))
} => Box::new(fields.iter().enumerate().map(move |(index, field_id)| {
origin.make_positioned_item_vertex(
index + 1,
item_index.get(field_id).expect("missing item"),
)
})),
}
}),
Expand Down Expand Up @@ -568,9 +577,18 @@ pub(super) fn resolve_union_edge<'a, V: AsVertex<Vertex<'a>> + 'a>(
}
};

Box::new(union_item.fields.iter().map(move |field_id| {
origin.make_item_vertex(item_index.get(field_id).expect("missing item"))
}))
Box::new(
union_item
.fields
.iter()
.enumerate()
.map(move |(index, field_id)| {
origin.make_positioned_item_vertex(
index + 1,
item_index.get(field_id).expect("missing item"),
)
}),
)
}),
_ => unreachable!("resolve_union_edge {edge_name}"),
}
Expand Down
4 changes: 4 additions & 0 deletions src/adapter/enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl<'a> EnumVariant<'a> {
pub(super) fn item(&self) -> &'a Item {
self.item
}

pub(super) fn position(&self) -> i64 {
self.index as i64 + 1
}
}

enum DiscriminantValue {
Expand Down
4 changes: 4 additions & 0 deletions src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ impl<'a> Adapter<'a> for &'a RustdocAdapter<'a> {
}
"Module" => properties::resolve_module_property(contexts, property_name),
"Struct" => properties::resolve_struct_property(contexts, property_name),
"StructField" => properties::resolve_struct_field_property(contexts, property_name),
"Enum" => properties::resolve_enum_property(contexts, property_name),
"Variant" | "PlainVariant" | "TupleVariant" | "StructVariant" => {
properties::resolve_enum_variant_property(contexts, property_name)
}
"Union" => properties::resolve_union_property(contexts, property_name),
"Span" => properties::resolve_span_property(contexts, property_name),
"Path" => properties::resolve_path_property(contexts, property_name),
Expand Down
11 changes: 11 additions & 0 deletions src/adapter/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ impl Origin {
}
}

pub(super) fn make_positioned_item_vertex<'a>(
&self,
index: usize,
item: &'a Item,
) -> Vertex<'a> {
Vertex {
origin: *self,
kind: VertexKind::PositionedItem(index, item),
}
}

pub(super) fn make_span_vertex<'a>(&self, span: &'a Span) -> Vertex<'a> {
Vertex {
origin: *self,
Expand Down
26 changes: 26 additions & 0 deletions src/adapter/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ pub(super) fn resolve_struct_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
}
}

pub(super) fn resolve_struct_field_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
contexts: ContextIterator<'a, V>,
property_name: &str,
) -> ContextOutcomeIterator<'a, V, FieldValue> {
match property_name {
"position" => resolve_property_with(contexts, |vertex| {
let (index, _) = vertex.as_positioned_item().expect("not a PositionedItem");
(index as i64).into()
}),
_ => unreachable!("StructField property {property_name}"),
}
}

pub(super) fn resolve_span_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
contexts: ContextIterator<'a, V>,
property_name: &str,
Expand Down Expand Up @@ -182,6 +195,19 @@ pub(super) fn resolve_union_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
}
}

pub(super) fn resolve_enum_variant_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
contexts: ContextIterator<'a, V>,
property_name: &str,
) -> ContextOutcomeIterator<'a, V, FieldValue> {
match property_name {
"position" => resolve_property_with(contexts, |vertex| {
let variant = vertex.as_variant().expect("not a Variant vertex");
variant.position().into()
}),
_ => unreachable!("EnumVariant property {property_name}"),
}
}

pub(super) fn resolve_path_property<'a, V: AsVertex<Vertex<'a>> + 'a>(
contexts: ContextIterator<'a, V>,
property_name: &str,
Expand Down
Loading

0 comments on commit 29e8d53

Please sign in to comment.