Skip to content

Commit

Permalink
Remove unecessary re-export (#657)
Browse files Browse the repository at this point in the history
* Remove unecessary re-export

* Format everything
  • Loading branch information
udoprog authored Nov 28, 2023
1 parent 1fdc17f commit 6c936b3
Show file tree
Hide file tree
Showing 30 changed files with 737 additions and 429 deletions.
77 changes: 38 additions & 39 deletions crates/rune/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ mod doc;
mod format;
mod languageserver;
mod loader;
mod naming;
mod run;
mod tests;
mod visitor;
mod naming;

use rust_alloc::string::String;
use rust_alloc::vec::Vec;
use std::fmt;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use rust_alloc::string::String;
use rust_alloc::vec::Vec;

use crate::alloc;
use crate::alloc::prelude::*;
Expand All @@ -34,7 +34,7 @@ use tracing_subscriber::filter::EnvFilter;
use crate::compile::{ItemBuf, ParseOptionError};
use crate::modules::capture_io::CaptureIo;
use crate::termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use crate::{Context, ContextError, Options, Hash};
use crate::{Context, ContextError, Hash, Options};

/// Default about splash.
const DEFAULT_ABOUT: &str = "The Rune Language Interpreter";
Expand Down Expand Up @@ -83,7 +83,11 @@ impl<'a> Entry<'a> {
/// .run();
///```
pub fn about(mut self, about: impl fmt::Display) -> Self {
self.about = Some(about.try_to_string().expect("Failed to format about string"));
self.about = Some(
about
.try_to_string()
.expect("Failed to format about string"),
);
self
}

Expand Down Expand Up @@ -266,7 +270,10 @@ where
command: T,
}

impl<T> CommandShared<T> where T: CommandBase + clap::Args {
impl<T> CommandShared<T>
where
T: CommandBase + clap::Args,
{
/// Construct compiler options from arguments.
fn options(&self) -> Result<Options, ParseOptionError> {
let mut options = Options::default();
Expand Down Expand Up @@ -379,8 +386,7 @@ trait CommandBase {

/// Propagate related flags from command and config.
#[inline]
fn propagate(&mut self, _: &mut Config, _: &mut SharedFlags) {
}
fn propagate(&mut self, _: &mut Config, _: &mut SharedFlags) {}
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -442,10 +448,7 @@ impl Command {
Command::Hash(..) => return None,
};

Some(CommandSharedRef {
shared,
command,
})
Some(CommandSharedRef { shared, command })
}
}

Expand Down Expand Up @@ -488,25 +491,25 @@ impl Config {
build_paths.try_push(BuildPath::Package(p))?;
}
}

if let Some(test) = cmd.find_tests() {
for p in self.manifest.find_tests(test)? {
build_paths.try_push(BuildPath::Package(p))?;
}
}

if let Some(example) = cmd.find_examples() {
for p in self.manifest.find_examples(example)? {
build_paths.try_push(BuildPath::Package(p))?;
}
}

if let Some(bench) = cmd.find_benches() {
for p in self.manifest.find_benches(bench)? {
build_paths.try_push(BuildPath::Package(p))?;
}
}

Ok(build_paths)
}
}
Expand All @@ -525,7 +528,11 @@ impl SharedFlags {
test: c.test,
};

let mut context = entry.context.as_mut().context("Context builder not configured with Entry::context")?(opts)?;
let mut context =
entry
.context
.as_mut()
.context("Context builder not configured with Entry::context")?(opts)?;

if let Some(capture) = capture {
context.install(crate::modules::capture_io::module(capture)?)?;
Expand Down Expand Up @@ -681,12 +688,8 @@ fn find_manifest() -> Option<(PathBuf, PathBuf)> {
}

fn populate_config(io: &mut Io<'_>, c: &mut Config, cmd: CommandSharedRef<'_>) -> Result<()> {
c.found_paths.try_extend(
cmd.shared
.paths
.iter()
.map(|p| p.as_path().into()),
)?;
c.found_paths
.try_extend(cmd.shared.paths.iter().map(|p| p.as_path().into()))?;

if !c.found_paths.is_empty() && !cmd.shared.workspace {
return Ok(());
Expand Down Expand Up @@ -744,18 +747,18 @@ async fn main_with_out(io: &mut Io<'_>, entry: &mut Entry<'_>, mut args: Args) -
return Ok(ExitCode::Failure);
}
};

let mut entrys = alloc::Vec::new();

if let Some(cmd) = cmd.as_command_shared_ref() {
populate_config(io, &mut c, cmd)?;

let build_paths = c.build_paths(cmd)?;

let what = cmd.command.describe();
let verbose = c.verbose;
let recursive = cmd.shared.recursive;

for build_path in build_paths {
match build_path {
BuildPath::Path(path) => {
Expand All @@ -771,9 +774,15 @@ async fn main_with_out(io: &mut Io<'_>, entry: &mut Entry<'_>, mut args: Args) -
o.set_color(&ColorSpec::new())?;
o.flush()?;
result?;
writeln!(o, " {} `{}` (from {})", p.found.kind, p.found.path.display(), p.package.name)?;
writeln!(
o,
" {} `{}` (from {})",
p.found.kind,
p.found.path.display(),
p.package.name
)?;
}

entrys.try_push(EntryPoint::Package(p))?;
}
}
Expand Down Expand Up @@ -823,17 +832,7 @@ where
Command::Test(f) => {
let options = f.options()?;

match tests::run(
io,
c,
&f.command,
&f.shared,
&options,
entry,
entries,
)
.await?
{
match tests::run(io, c, &f.command, &f.shared, &options, entry, entries).await? {
ExitCode::Success => (),
other => return Ok(other),
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/cli/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::time::Instant;
use clap::Parser;

use crate::alloc::Vec;
use crate::cli::{ExitCode, Io, CommandBase, AssetKind, Config, SharedFlags};
use crate::cli::{AssetKind, CommandBase, Config, ExitCode, Io, SharedFlags};
use crate::compile::{Item, ItemBuf};
use crate::modules::capture_io::CaptureIo;
use crate::runtime::{Function, Unit, Value};
use crate::{Context, Hash, Sources, Vm};
use crate::modules::test::Bencher;
use crate::runtime::{Function, Unit, Value};
use crate::support::Result;
use crate::{Context, Hash, Sources, Vm};

#[derive(Parser, Debug)]
pub(super) struct Flags {
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::Path;
use anyhow::{Context, Result};
use clap::Parser;

use crate::cli::{visitor, Config, Entry, ExitCode, Io, SharedFlags, CommandBase, AssetKind};
use crate::cli::{visitor, AssetKind, CommandBase, Config, Entry, ExitCode, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::{Diagnostics, Options, Source, Sources};

Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/cli/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::doc::Artifacts;
use anyhow::{Context, Result};
use clap::Parser;

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

Expand Down
37 changes: 24 additions & 13 deletions crates/rune/src/cli/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::io::Write;
use clap::Parser;
use similar::{ChangeTag, TextDiff};

use crate::support::{Context, Result};
use crate::alloc::BTreeSet;
use crate::alloc::prelude::*;
use crate::cli::{Entry, ExitCode, Io, EntryPoint, SharedFlags, Config, CommandBase, AssetKind};
use crate::termcolor::{WriteColor, ColorSpec, Color};
use crate::{Source, Sources, Options, Diagnostics};
use crate::alloc::BTreeSet;
use crate::cli::{AssetKind, CommandBase, Config, Entry, EntryPoint, ExitCode, Io, SharedFlags};
use crate::support::{Context, Result};
use crate::termcolor::{Color, ColorSpec, WriteColor};
use crate::{Diagnostics, Options, Source, Sources};

#[derive(Parser, Debug)]
pub(super) struct Flags {
Expand All @@ -35,7 +35,18 @@ impl CommandBase for Flags {
}
}

pub(super) fn run<'m, I>(io: &mut Io<'_>, entry: &mut Entry<'_>, c: &Config, entrys: I, flags: &Flags, shared: &SharedFlags, options: &Options) -> Result<ExitCode> where I: IntoIterator<Item = EntryPoint<'m>> {
pub(super) fn run<'m, I>(
io: &mut Io<'_>,
entry: &mut Entry<'_>,
c: &Config,
entrys: I,
flags: &Flags,
shared: &SharedFlags,
options: &Options,
) -> Result<ExitCode>
where
I: IntoIterator<Item = EntryPoint<'m>>,
{
let col = Colors::new();

let mut changed = 0;
Expand Down Expand Up @@ -192,18 +203,18 @@ fn diff(io: &mut Io, source: &Source, val: &[u8], col: &Colors) -> Result<(), an
ChangeTag::Insert => ("+", &col.green),
ChangeTag::Equal => (" ", &col.dim),
};

io.stdout.set_color(color)?;
write!(io.stdout,"{}", Line(change.old_index()))?;
write!(io.stdout,"{sign}")?;

write!(io.stdout, "{}", Line(change.old_index()))?;
write!(io.stdout, "{sign}")?;

for (_, value) in change.iter_strings_lossy() {
write!(io.stdout, "{value}")?;
}

io.stdout.reset()?;

if change.missing_newline() {
writeln!(io.stdout)?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/cli/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{path::Path, sync::Arc};

use anyhow::{anyhow, Context as _, Result};

use crate::alloc::{VecDeque, Vec};
use crate::alloc::{Vec, VecDeque};
use crate::cli::{visitor, Io, SharedFlags};
use crate::compile::{FileSourceLoader, ItemBuf};
use crate::Diagnostics;
Expand Down
14 changes: 6 additions & 8 deletions crates/rune/src/cli/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use core::mem::replace;

use std::ffi::OsStr;

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

Expand All @@ -18,12 +18,10 @@ 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 {
EntryPoint::Path(path) => {
match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::try_from(name)?,
None => String::try_from("entry")?,
}
}
EntryPoint::Path(path) => match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::try_from(name)?,
None => String::try_from("entry")?,
},
EntryPoint::Package(p) => {
let name = p.found.name.as_str();

Expand All @@ -35,7 +33,7 @@ impl Naming {
};

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

// TODO: make it so that we can communicate different entrypoints in the
Expand Down
22 changes: 16 additions & 6 deletions crates/rune/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::time::Instant;
use anyhow::{anyhow, Result};
use clap::Parser;

use crate::cli::{Config, ExitCode, Io, CommandBase, AssetKind, SharedFlags};
use crate::runtime::{VmError, VmExecution, VmResult, UnitStorage};
use crate::cli::{AssetKind, CommandBase, Config, ExitCode, Io, SharedFlags};
use crate::runtime::{UnitStorage, VmError, VmExecution, VmResult};
use crate::{Context, Sources, Unit, Value, Vm};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -145,7 +145,11 @@ pub(super) async fn run(
}

if args.dump_unit() {
writeln!(io.stdout, "Unit size: {} bytes", unit.instructions().bytes())?;
writeln!(
io.stdout,
"Unit size: {} bytes",
unit.instructions().bytes()
)?;

if args.emit_instructions() {
let mut o = io.stdout.lock();
Expand Down Expand Up @@ -328,8 +332,10 @@ where
let vm = execution.vm();
let mut o = io.stdout.lock();

if let Some((hash, signature)) =
vm.unit().debug_info().and_then(|d| d.function_at(vm.last_ip()))
if let Some((hash, signature)) = vm
.unit()
.debug_info()
.and_then(|d| d.function_at(vm.last_ip()))
{
writeln!(o, "fn {} ({}):", signature, hash)?;
}
Expand All @@ -350,7 +356,11 @@ where
writeln!(o, "{}:", label)?;
}

if let Some((inst, _)) = vm.unit().instruction_at(vm.last_ip()).map_err(VmError::from)? {
if let Some((inst, _)) = vm
.unit()
.instruction_at(vm.last_ip())
.map_err(VmError::from)?
{
write!(o, " {:04} = {}", vm.last_ip(), inst)?;
} else {
write!(o, " {:04} = *out of bounds*", vm.last_ip())?;
Expand Down
Loading

0 comments on commit 6c936b3

Please sign in to comment.