Skip to content

Commit

Permalink
Start running captured doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jun 24, 2023
1 parent 1450ba1 commit 1dcaf23
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 201 deletions.
2 changes: 1 addition & 1 deletion crates/rune-modules/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl RequestBuilder {
/// let client = http::Client::new();
///
/// let response = client.get("http://example.com")
/// .body_bytes(body)
/// .body_bytes(b"Hello World")
/// .send()
/// .await?;
///
Expand Down
6 changes: 5 additions & 1 deletion crates/rune/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod loader;
mod run;
mod tests;
mod visitor;
mod naming;

use std::fmt;
use std::io::{self, Write};
Expand Down Expand Up @@ -211,8 +212,11 @@ impl<'a> Entry<'a> {
}
}

enum EntryPoint<'a> {
/// A single entrypoint that can be built or processed.
pub(crate) enum EntryPoint<'a> {
/// A plain path entrypoint.
Path(PathBuf),
/// A package entrypoint.
Package(workspace::FoundPackage<'a>),
}

Expand Down
38 changes: 5 additions & 33 deletions crates/rune/src/cli/doc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;
use std::ffi::OsStr;
use std::io::Write;
use std::path::PathBuf;

Expand All @@ -10,8 +8,9 @@ use anyhow::{Context, Result};
use clap::Parser;

use crate::cli::{Config, Entry, EntryPoint, ExitCode, Io, SharedFlags, CommandBase, AssetKind};
use crate::cli::naming::Naming;
use crate::compile::{FileSourceLoader, ItemBuf};
use crate::{Diagnostics, Options, Source, Sources, workspace};
use crate::{Diagnostics, Options, Source, Sources};

#[derive(Parser, Debug)]
pub(super) struct Flags {
Expand Down Expand Up @@ -76,37 +75,10 @@ where

let mut visitors = Vec::new();

let mut names = HashSet::new();
let mut naming = Naming::default();

for (index, e) in entries.into_iter().enumerate() {
let name = match &e {
EntryPoint::Path(path) => {
match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::from(name),
None => String::from("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",
};

format!("{}-{name}-{ext}", p.package.name)
},
};

// TODO: make it so that we can communicate different entrypoints in the
// visitors context instead of this hackery.
let name = if !names.insert(name.clone()) {
format!("{name}{index}")
} else {
name
};
for e in entries {
let name = naming.name(&e);

let item = ItemBuf::with_crate(&name);
let mut visitor = crate::doc::Visitor::new(item);
Expand Down
52 changes: 52 additions & 0 deletions crates/rune/src/cli/naming.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use core::mem::replace;

use std::collections::HashSet;
use std::ffi::OsStr;

use crate::no_std::prelude::*;

use crate::cli::EntryPoint;
use crate::workspace;

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

impl Naming {
/// Construct a unique crate name for the given entrypoint.
pub(crate) fn name(&mut self, e: &EntryPoint<'_>) -> String {
let name = match &e {
EntryPoint::Path(path) => {
match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::from(name),
None => String::from("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",
};

format!("{}-{name}-{ext}", p.package.name)
},
};

// TODO: make it so that we can communicate different entrypoints in the
// visitors context instead of this hackery.
if !self.names.insert(name.clone()) {
let next = self.count.wrapping_add(1);
let index = replace(&mut self.count, next);
format!("{name}{index}")
} else {
name
}
}
}
Loading

0 comments on commit 1dcaf23

Please sign in to comment.