Skip to content

Commit

Permalink
doc: Clean up how documentation works
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jul 26, 2024
1 parent 816e003 commit 06db632
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 334 deletions.
45 changes: 45 additions & 0 deletions crates/rune-core/src/item/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ impl Item {
it.next_back()?;
Some(it.into_item())
}

/// Display an unqalified variant of the item which does not include `::` if
/// a crate is present.
pub fn unqalified(&self) -> Unqalified {
Unqalified::new(self)
}
}

impl AsRef<Item> for &Item {
Expand Down Expand Up @@ -484,3 +490,42 @@ impl PartialEq<Iter<'_>> for &Item {
*self == other.as_item()
}
}

/// Display an unqalified path.
pub struct Unqalified<'a> {
item: &'a Item,
}

impl<'a> Unqalified<'a> {
fn new(item: &'a Item) -> Self {
Self { item }
}
}

impl fmt::Display for Unqalified<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut it = self.item.iter();

if let Some(last) = it.next_back() {
for c in it {
match c {
ComponentRef::Crate(name) => {
write!(f, "{name}::")?;
}
ComponentRef::Str(name) => {
write!(f, "{name}::")?;
}
c => {
write!(f, "{c}::")?;
}
}
}

write!(f, "{}", last)?;
} else {
f.write_str("{root}")?;
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ anyhow = { version = "1.0.71", default-features = false, optional = true }
bincode = { version = "1.3.3", optional = true }
clap = { version = "4.2.7", features = ["derive"], optional = true }
codespan-reporting = { version = "0.11.1", optional = true }
handlebars = { version = "5.1.0", optional = true }
handlebars = { version = "6.0.0", optional = true }
pulldown-cmark = { version = "0.9.2", optional = true }
relative-path = { version = "1.8.0", optional = true, features = ["serde"] }
rust-embed = { version = "6.6.1", optional = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/rune/src/cli/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::alloc::prelude::*;
use crate::cli::naming::Naming;
use crate::cli::{AssetKind, CommandBase, Config, Entry, EntryPoint, ExitCode, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::{Diagnostics, ItemBuf, Options, Source, Sources};
use crate::{Diagnostics, Options, Source, Sources};

mod cli {
use std::path::PathBuf;
Expand Down Expand Up @@ -94,9 +94,8 @@ where
let mut naming = Naming::default();

for e in entries {
let name = naming.name(&e)?;
let item = naming.item(&e)?;

let item = ItemBuf::with_crate(&name)?;
let mut visitor = crate::doc::Visitor::new(&item)?;
let mut sources = Sources::new();

Expand Down
46 changes: 18 additions & 28 deletions crates/rune/src/cli/naming.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
use core::mem::replace;

use std::ffi::OsStr;

use crate::alloc::prelude::*;
use crate::alloc::{self, try_format, HashSet, String};
use crate::alloc::{self, HashMap};
use crate::cli::EntryPoint;
use crate::workspace;
use crate::item::ComponentRef;
use crate::ItemBuf;

/// Helper to perform non-conflicting crate naming.
#[derive(Default)]
pub(crate) struct Naming {
names: HashSet<String>,
count: usize,
names: HashMap<ItemBuf, usize>,
}

impl Naming {
/// Construct a unique crate name for the given entrypoint.
pub(crate) fn name(&mut self, e: &EntryPoint<'_>) -> alloc::Result<String> {
let name = match &e {
pub(crate) fn item(&mut self, e: &EntryPoint<'_>) -> alloc::Result<ItemBuf> {
let mut item = match &e {
EntryPoint::Path(path) => match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::try_from(name)?,
None => String::try_from("entry")?,
Some(name) => ItemBuf::with_crate(name)?,
None => ItemBuf::with_crate("entry")?,
},
EntryPoint::Package(p) => {
let name = p.found.name.as_str();

let ext = match &p.found.kind {
workspace::FoundKind::Binary => "bin",
workspace::FoundKind::Test => "test",
workspace::FoundKind::Example => "example",
workspace::FoundKind::Bench => "bench",
};

try_format!("{}-{name}-{ext}", p.package.name)
ItemBuf::with_crate_item(&p.package.name, [name])?
}
};

// TODO: make it so that we can communicate different entrypoints in the
// visitors context instead of this hackery.
Ok(if !self.names.try_insert(name.try_clone()?)? {
let next = self.count.wrapping_add(1);
let index = replace(&mut self.count, next);
try_format!("{name}{index}")
} else {
name
})
let values = self.names.entry(item.try_clone()?).or_try_default()?;

if *values > 0 {
let name = try_format!("{}", *values - 1);
item.push(ComponentRef::Str(&name))?;
}

*values += 1;
Ok(item)
}
}
3 changes: 1 addition & 2 deletions crates/rune/src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ where
};

for entry in entries {
let name = naming.name(&entry)?;
let item = ItemBuf::with_crate(&name)?;
let item = naming.item(&entry)?;

let mut sources = Sources::new();

Expand Down
1 change: 1 addition & 0 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ impl Context {
.entry(i.hash)
.or_try_default()?
.try_push(i.trait_hash)?;

Ok(())
}

Expand Down
Loading

0 comments on commit 06db632

Please sign in to comment.