Skip to content

Commit

Permalink
Honor nostdlib in customize mode as well (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkleen authored Sep 27, 2023
1 parent c4b7e0d commit 98824c5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
10 changes: 5 additions & 5 deletions cli/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use nickel_lang_core::{

use crate::{
cli::GlobalOptions,
customize::NoCustomizeMode,
error::{CliResult, ResultErrorExt},
eval::EvalCommand,
input::Prepare,
input::{InputOptions, Prepare},
};

#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, clap::ValueEnum)]
Expand Down Expand Up @@ -56,12 +56,12 @@ pub struct DocCommand {
pub format: crate::doc::DocFormat,

#[command(flatten)]
pub evaluation: EvalCommand,
pub input: InputOptions<NoCustomizeMode>,
}

impl DocCommand {
pub fn run(self, global: GlobalOptions) -> CliResult<()> {
let mut program = self.evaluation.prepare(&global)?;
let mut program = self.input.prepare(&global)?;
self.export_doc(&mut program).report_with_program(program)
}

Expand Down Expand Up @@ -95,7 +95,7 @@ impl DocCommand {

let mut has_file_name = false;

if let Some(path) = self.evaluation.input.files.get(0) {
if let Some(path) = self.input.files.get(0) {
if let Some(file_stem) = path.file_stem() {
output_file.push(file_stem);
has_file_name = true;
Expand Down
25 changes: 1 addition & 24 deletions cli/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use nickel_lang_core::{eval::cache::lazy::CBNCache, program::Program};

use crate::{
cli::GlobalOptions,
customize::CustomizeMode,
Expand All @@ -9,37 +7,16 @@ use crate::{

#[derive(clap::Parser, Debug)]
pub struct EvalCommand {
#[cfg(debug_assertions)]
/// Skips the standard library import. For debugging only
#[arg(long, global = true)]
pub nostdlib: bool,

#[command(flatten)]
pub input: InputOptions<CustomizeMode>,
}

impl EvalCommand {
pub fn run(self, global: GlobalOptions) -> CliResult<()> {
let mut program = self.prepare(&global)?;
let mut program = self.input.prepare(&global)?;
program
.eval_full()
.map(|t| println!("{t}"))
.report_with_program(program)
}
}

impl Prepare for EvalCommand {
fn prepare(&self, global: &GlobalOptions) -> CliResult<Program<CBNCache>> {
// Rust warns about `program` not needing to be mutable if
// `debug_assertions` are off
#![allow(unused_mut)]
let mut program = self.input.prepare(global)?;

#[cfg(debug_assertions)]
if self.nostdlib {
program.set_skip_stdlib();
}

Ok(program)
}
}
8 changes: 4 additions & 4 deletions cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use nickel_lang_core::{

use crate::{
cli::GlobalOptions,
customize::CustomizeMode,
error::{CliResult, ResultErrorExt},
eval::EvalCommand,
input::Prepare,
input::{InputOptions, Prepare},
};

#[derive(clap::Parser, Debug)]
Expand All @@ -24,12 +24,12 @@ pub struct ExportCommand {
pub output: Option<PathBuf>,

#[command(flatten)]
pub evaluation: EvalCommand,
pub input: InputOptions<CustomizeMode>,
}

impl ExportCommand {
pub fn run(self, global: GlobalOptions) -> CliResult<()> {
let mut program = self.evaluation.prepare(&global)?;
let mut program = self.input.prepare(&global)?;

self.export(&mut program).report_with_program(program)
}
Expand Down
10 changes: 10 additions & 0 deletions cli/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ pub struct InputOptions<Customize: clap::Args> {
/// Input files, omit to read from stdin
pub files: Vec<PathBuf>,

#[cfg(debug_assertions)]
/// Skips the standard library import. For debugging only
#[arg(long, global = true)]
pub nostdlib: bool,

#[command(flatten)]
pub customize_mode: Customize,
}
Expand All @@ -27,6 +32,11 @@ impl<C: clap::Args + Customize> Prepare for InputOptions<C> {

program.color_opt = global.color.into();

#[cfg(debug_assertions)]
if self.nostdlib {
program.set_skip_stdlib();
}

self.customize_mode.customize(program)
}
}

0 comments on commit 98824c5

Please sign in to comment.