Skip to content

Commit

Permalink
Include ordinal positions of generic parameters in the schema. (#688) (
Browse files Browse the repository at this point in the history
…#689)

Contains a breaking change to the `VertexKind::GenericParameter` enum
variant.
  • Loading branch information
obi1kenobi authored Dec 30, 2024
1 parent ba992f5 commit 34bb09a
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 13 deletions.
49 changes: 43 additions & 6 deletions src/adapter/edges.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rustdoc_types::{GenericBound::TraitBound, Id, ItemEnum, VariantKind, WherePredicate};
use std::rc::Rc;
use rustdoc_types::{
GenericBound::TraitBound, GenericParamDefKind, Id, ItemEnum, VariantKind, WherePredicate,
};
use std::{num::NonZeroUsize, rc::Rc};
use trustfall::provider::{
resolve_neighbors_with, AsVertex, ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo,
VertexIterator,
Expand Down Expand Up @@ -262,17 +264,52 @@ pub(super) fn resolve_generic_parameter_edge<'a, V: AsVertex<Vertex<'a>> + 'a>(
contexts: ContextIterator<'a, V>,
edge_name: &str,
) -> ContextOutcomeIterator<'a, V, VertexIterator<'a, Vertex<'a>>> {
struct GenericParamCounter {
lifetimes: NonZeroUsize,
types: NonZeroUsize,
consts: NonZeroUsize,
}

match edge_name {
"generic_parameter" => resolve_neighbors_with(contexts, move |vertex| {
let origin = vertex.origin;
let mut counter = GenericParamCounter {
lifetimes: NonZeroUsize::new(1).unwrap(),
types: NonZeroUsize::new(1).unwrap(),
consts: NonZeroUsize::new(1).unwrap(),
};
Box::new(
vertex
.as_generics()
.map(move |generics| {
generics
.params
.iter()
.map(move |param| origin.make_generic_parameter_vertex(generics, param))
generics.params.iter().map(move |param| {
let position = match param.kind {
GenericParamDefKind::Lifetime { .. } => {
let position = counter.lifetimes;
counter.lifetimes =
position.checked_add(1).expect("param position overflow");
Some(position)
}
GenericParamDefKind::Type { is_synthetic, .. } => {
if is_synthetic {
None
} else {
let position = counter.types;
counter.types = position
.checked_add(1)
.expect("param position overflow");
Some(position)
}
}
GenericParamDefKind::Const { .. } => {
let position = counter.consts;
counter.consts =
position.checked_add(1).expect("param position overflow");
Some(position)
}
};
origin.make_generic_parameter_vertex(generics, param, position)
})
})
.into_iter()
.flatten(),
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<'a> Adapter<'a> for &'a RustdocAdapter<'a> {
| "GenericTypeParameter"
| "GenericLifetimeParameter"
| "GenericConstParameter"
if matches!(property_name.as_ref(), "name") =>
if matches!(property_name.as_ref(), "name" | "position") =>
{
properties::resolve_generic_parameter_property(contexts, property_name)
}
Expand Down
5 changes: 3 additions & 2 deletions src/adapter/origin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, rc::Rc};
use std::{borrow::Cow, num::NonZeroUsize, rc::Rc};

use rustdoc_types::{Abi, Item, Span};

Expand Down Expand Up @@ -145,10 +145,11 @@ impl Origin {
&self,
generics: &'a rustdoc_types::Generics,
param: &'a rustdoc_types::GenericParamDef,
position: Option<NonZeroUsize>,
) -> Vertex<'a> {
Vertex {
origin: *self,
kind: VertexKind::GenericParameter(generics, param),
kind: VertexKind::GenericParameter(generics, param, position),
}
}
}
10 changes: 10 additions & 0 deletions src/adapter/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,16 @@ pub(crate) fn resolve_generic_parameter_property<'a, V: AsVertex<Vertex<'a>> + '

generic.name.clone().into()
}),
"position" => resolve_property_with(contexts, |vertex| {
let position = vertex
.as_generic_parameter_position()
.expect("vertex was not a GenericParameter");

match position {
None => FieldValue::NULL,
Some(x) => FieldValue::Uint64(x.get() as u64),
}
}),
_ => unreachable!("GenericParameter property {property_name}"),
}
}
Expand Down
Loading

0 comments on commit 34bb09a

Please sign in to comment.