Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement properties around participation in public APIs on importable paths and items. #260

Merged
merged 5 commits into from Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/adapter/mod.rs
Expand Up @@ -97,7 +97,14 @@ impl<'a> Adapter<'a> for RustdocAdapter<'a> {
| "AssociatedConstant" | "Module"
if matches!(
property_name.as_ref(),
"id" | "crate_id" | "name" | "docs" | "attrs" | "visibility_limit"
"id" | "crate_id"
| "name"
| "docs"
| "attrs"
| "doc_hidden"
| "deprecated"
| "public_api_eligible"
| "visibility_limit"
) =>
{
// properties inherited from Item, accesssed on Item subtypes
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/optimizations/item_lookup.rs
Expand Up @@ -89,7 +89,7 @@ fn resolve_items_by_importable_path_field_value<'a>(
.expect("crate's imports_index was never constructed")
.get(path_components.as_slice())
{
resolve_item_vertices(origin, items.iter().copied())
resolve_item_vertices(origin, items.iter().map(|(item, _)| item).copied())
} else {
// No such items found.
Box::new(std::iter::empty())
Expand Down
9 changes: 6 additions & 3 deletions src/adapter/origin.rs
Expand Up @@ -2,7 +2,10 @@ use std::rc::Rc;

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

use crate::attributes::{Attribute, AttributeMetaItem};
use crate::{
attributes::{Attribute, AttributeMetaItem},
indexed_crate::ImportablePath,
};

use super::vertex::{Vertex, VertexKind};

Expand Down Expand Up @@ -37,11 +40,11 @@ impl Origin {

pub(super) fn make_importable_path_vertex<'a>(
&self,
importable_path: Vec<&'a str>,
importable_path: ImportablePath<'a>,
) -> Vertex<'a> {
Vertex {
origin: *self,
kind: VertexKind::ImportablePath(importable_path),
kind: VertexKind::ImportablePath(Rc::from(importable_path)),
}
}

Expand Down
48 changes: 47 additions & 1 deletion src/adapter/properties.rs
@@ -1,4 +1,4 @@
use rustdoc_types::ItemEnum;
use rustdoc_types::{ItemEnum, Visibility};
use trustfall::{
provider::{
accessor_property, field_property, resolve_property_with, ContextIterator,
Expand Down Expand Up @@ -46,6 +46,35 @@ pub(super) fn resolve_item_property<'a>(
"name" => resolve_property_with(contexts, field_property!(as_item, name)),
"docs" => resolve_property_with(contexts, field_property!(as_item, docs)),
"attrs" => resolve_property_with(contexts, field_property!(as_item, attrs)),
"deprecated" => resolve_property_with(
contexts,
field_property!(as_item, deprecation, { deprecation.is_some().into() }),
),
"doc_hidden" => resolve_property_with(
contexts,
field_property!(as_item, attrs, {
attrs
.iter()
.any(|attr| Attribute::is_doc_hidden(attr))
.into()
}),
),
"public_api_eligible" => resolve_property_with(contexts, move |vertex| {
// Items are eligible for public API if both:
// - The item is public, either explicitly (`pub`) or implicitly (like enum variants).
// - The item is deprecated, or not `#[doc(hidden)]`.
//
// This does not mean that the item is necessarily part of the public API!
// An item that is not eligible by itself cannot be part of the public API,
// but eligible items might not be public API -- for example, pub-in-priv items
// (public items in a private module) are eligible but not public API.
let item = vertex.as_item().expect("vertex was not an Item");
let is_public = matches!(item.visibility, Visibility::Public | Visibility::Default);
(is_public
&& (item.deprecation.is_some()
|| !item.attrs.iter().any(|attr| Attribute::is_doc_hidden(attr))))
.into()
}),
"visibility_limit" => resolve_property_with(contexts, |vertex| {
let item = vertex.as_item().expect("not an item");
match &item.visibility {
Expand Down Expand Up @@ -162,12 +191,29 @@ pub(super) fn resolve_importable_path_property<'a>(
vertex
.as_importable_path()
.expect("not an importable path")
.path
.components
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.into()
}),
"visibility_limit" => resolve_property_with(contexts, |_| "public".into()),
"doc_hidden" => resolve_property_with(
contexts,
field_property!(as_importable_path, modifiers, {
modifiers.doc_hidden.into()
}),
),
"deprecated" => resolve_property_with(
contexts,
field_property!(as_importable_path, modifiers, {
modifiers.deprecated.into()
}),
),
"public_api" => {
resolve_property_with(contexts, accessor_property!(as_importable_path, public_api))
}
_ => unreachable!("ImportablePath property {property_name}"),
}
}
Expand Down