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

rustdoc typegen #217

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
564 changes: 519 additions & 45 deletions Cargo.lock

Large diffs are not rendered by default.

File renamed without changes.
21 changes: 21 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 RedBadger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 7 additions & 1 deletion crux_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true
rust-version.workspace = true
rust-version = "1.70"

[[bin]]
name = "crux"
Expand All @@ -17,8 +17,14 @@ path = "src/main.rs"
anyhow.workspace = true
clap = { version = "4.3.24", features = ["derive"] }
console = "0.15.8"
guppy = "0.17.5"
ignore = "0.4.22"
libc = "0.2.155"
ramhorns = "1.0.0"
rustdoc-types = "0.24.0"
serde = { workspace = true, features = ["derive"] }
serde_json = "1.0.114"
similar = { version = "2.5.0", features = ["inline"] }
tokio = { version = "1.37.0", features = ["full"] }
tokio-fd = "0.3.0"
toml = "0.8.13"
26 changes: 18 additions & 8 deletions crux_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ pub(crate) struct Cli {

#[arg(long, short, action = ArgAction::Count)]
pub verbose: u8,
}

#[derive(Subcommand)]
pub(crate) enum Commands {
#[command(visible_alias = "doc")]
Doctor(DoctorArgs),

#[command(visible_alias = "gen")]
Codegen(CodegenArgs),
}

#[derive(Args)]
pub(crate) struct DoctorArgs {
#[arg(long, short)]
pub(crate) fix: Option<PathBuf>,

#[arg(long, short, default_value = "false")]
pub include_source_code: bool,
Expand All @@ -31,16 +46,11 @@ pub(crate) struct Cli {
pub path: Option<PathBuf>,
}

#[derive(Subcommand)]
pub(crate) enum Commands {
#[command(visible_alias = "doc")]
Doctor(DoctorArgs),
}

#[derive(Args)]
pub(crate) struct DoctorArgs {
pub(crate) struct CodegenArgs {
/// name of the library containing your Crux App
#[arg(long, short)]
pub(crate) fix: Option<PathBuf>,
pub lib: String,
}

#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions crux_cli/src/codegen/crate_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use rustdoc_types::{Crate, Id, Item};

/// The [`Crate`] type represents the deserialized form of the rustdoc JSON
/// input. This wrapper adds some helpers and state on top.
pub struct CrateWrapper<'c> {
crate_: &'c Crate,

/// Normally, an item referenced by [`Id`] is present in the rustdoc JSON.
/// If [`Self::crate_.index`] is missing an [`Id`], then we add it here, to
/// aid with debugging. It will typically be missing because of bugs (or
/// borderline bug such as re-exports of foreign items like discussed in
/// <https://github.com/rust-lang/rust/pull/99287#issuecomment-1186586518>)
/// We do not report it to users by default, because they can't do anything
/// about it. Missing IDs will be printed with `--verbose` however.
missing_ids: Vec<&'c Id>,
}

impl<'c> CrateWrapper<'c> {
pub fn new(crate_: &'c Crate) -> Self {
Self {
crate_,
missing_ids: vec![],
}
}

pub fn get_item(&mut self, id: &'c Id) -> Option<&'c Item> {
self.crate_.index.get(id).or_else(|| {
self.missing_ids.push(id);
None
})
}

pub fn missing_item_ids(&self) -> Vec<String> {
self.missing_ids.iter().map(|m| m.0.clone()).collect()
}
}
8 changes: 8 additions & 0 deletions crux_cli/src/codegen/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use anyhow::Result;
use guppy::{graph::PackageGraph, MetadataCommand};

pub(crate) fn compute_package_graph() -> Result<PackageGraph> {
let mut cmd = MetadataCommand::new();
let package_graph = PackageGraph::from_command(&mut cmd)?;
Ok(package_graph)
}
54 changes: 54 additions & 0 deletions crux_cli/src/codegen/intermediate_public_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use rustdoc_types::Item;

use super::nameable_item::NameableItem;
use super::path_component::PathComponent;
use super::public_item::PublicItemPath;
use super::render::RenderingContext;
use super::tokens::Token;

/// This struct represents one public item of a crate, but in intermediate form.
/// Conceptually it wraps a single [`Item`] even though the path to the item
/// consists of many [`Item`]s. Later, one [`Self`] will be converted to exactly
/// one [`crate::PublicItem`].
#[derive(Clone, Debug)]
pub struct IntermediatePublicItem<'c> {
path: Vec<PathComponent<'c>>,
}

impl<'c> IntermediatePublicItem<'c> {
pub fn new(path: Vec<PathComponent<'c>>) -> Self {
Self { path }
}

#[must_use]
pub fn item(&self) -> &'c Item {
self.path()
.last()
.expect("path must not be empty")
.item
.item
}

#[must_use]
pub fn path(&self) -> &[PathComponent<'c>] {
&self.path
}

/// See [`crate::item_processor::sorting_prefix()`] docs for an explanation why we have this.
#[must_use]
pub fn sortable_path(&self, context: &RenderingContext) -> PublicItemPath {
self.path()
.iter()
.map(|p| NameableItem::sortable_name(&p.item, context))
.collect()
}

#[must_use]
pub fn path_contains_renamed_item(&self) -> bool {
self.path().iter().any(|m| m.item.overridden_name.is_some())
}

pub fn render_token_stream(&self, context: &RenderingContext) -> Vec<Token> {
context.token_stream(self)
}
}
Loading
Loading