diff --git a/trilogy/src/bin/trilogy/main.rs b/trilogy/src/bin/trilogy/main.rs index 97630b0..3b5bfce 100644 --- a/trilogy/src/bin/trilogy/main.rs +++ b/trilogy/src/bin/trilogy/main.rs @@ -21,12 +21,12 @@ struct Cli { enum Command { /// Start up the interactive Trilogy REPL. Repl, - #[cfg(feature = "std")] /// Run a Trilogy program. Run { /// The path to the Trilogy source file containing the `main!()` procedure. file: PathBuf, /// Run without including the standard library. + #[cfg(feature = "std")] #[arg(short = 'S', long)] no_std: bool, /// Print the exit value instead of using it as the exit code. @@ -62,12 +62,12 @@ enum Command { #[arg(long = "lib")] library: bool, }, - #[cfg(feature = "std")] /// Check the syntax and warnings of a Trilogy program. Check { /// The path to the Trilogy source file containing the `main!()` procedure. file: PathBuf, /// Check without including the standard library. + #[cfg(feature = "std")] #[arg(short = 'S', long)] no_std: bool, }, @@ -158,12 +158,22 @@ fn main_sync() -> std::io::Result<()> { let args = Cli::parse(); match args.command { - #[cfg(feature = "std")] Command::Run { file, print, - no_std: _, debug, + #[cfg(feature = "std")] + no_std: true, + } => match Builder::new().build_from_source(file) { + Ok(trilogy) => run(trilogy, print, debug), + Err(report) => { + report.eprint(); + std::process::exit(1); + } + }, + #[cfg(feature = "std")] + Command::Run { + file, print, debug, .. } => match Trilogy::from_file(file) { Ok(trilogy) => run(trilogy, print, debug), Err(report) => { @@ -208,8 +218,18 @@ fn main_sync() -> std::io::Result<()> { } } } + Command::Check { + file, + #[cfg(feature = "std")] + no_std: true, + } => { + if let Err(report) = Builder::new().build_from_source(file) { + report.eprint(); + std::process::exit(1); + } + } #[cfg(feature = "std")] - Command::Check { file, no_std: _ } => { + Command::Check { file, .. } => { if let Err(report) = Trilogy::from_file(file) { report.eprint(); std::process::exit(1); diff --git a/trilogy/src/lib.rs b/trilogy/src/lib.rs index 69c4001..031103e 100644 --- a/trilogy/src/lib.rs +++ b/trilogy/src/lib.rs @@ -8,23 +8,42 @@ //! //! # Embedding //! -//! In the simplest case, a Trilogy program is loaded from a file external to the -//! running Rust program. -//! -//! ```no_run -//! use trilogy::Trilogy; -//! let trilogy = Trilogy::from_file("./path/to/main.tri").unwrap(); -//! let exit_value = trilogy.run().unwrap(); -//! ``` -//! -//! For more advanced usage, the [`Builder`][] allows for customizing the module -//! resolution system and injecting libraries directly into the instance. -//! -//! ```no_run -//! use trilogy::Builder; -//! let trilogy = Builder::default().build_from_source("./path/to/main.tri").unwrap(); -//! let exit_value = trilogy.run().unwrap(); -//! ``` + +#![cfg_attr( + feature = "std", + doc = r##" +In the simplest case, a Trilogy program is loaded from a file external to the +running Rust program. + +```no_run +use trilogy::Trilogy; +let trilogy = Trilogy::from_file("./path/to/main.tri").unwrap(); +let exit_value = trilogy.run().unwrap(); +``` + +For more advanced usage, the [`Builder`][] allows for customizing the module +resolution system and injecting libraries directly into the instance. + +```no_run +use trilogy::Builder; +let trilogy = Builder::new().build_from_source("./path/to/main.tri").unwrap(); +let exit_value = trilogy.run().unwrap(); +``` +"## +)] +#![cfg_attr( + not(feature = "std"), + doc = r##" +Without the `std` feature enabled, the [`Builder`][] must be used to manually set up the +module resolution system and injected libraries to compile the program. + +```no_run +use trilogy::Builder; +let trilogy = Builder::new().build_from_source("./path/to/main.tri").unwrap(); +let exit_value = trilogy.run().unwrap(); +``` +"## +)] #[cfg(feature = "std")] mod stdlib; diff --git a/trilogy/tests/samples.rs b/trilogy/tests/samples.rs index 683d15c..7cc8a07 100644 --- a/trilogy/tests/samples.rs +++ b/trilogy/tests/samples.rs @@ -1,13 +1,15 @@ use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use trilogy::{Builder, Trilogy}; +use trilogy::Builder; use trilogy_vm::{Struct, StructuralEq, Value}; const TEST_DIR: &str = "../samples"; macro_rules! include_tri { ($path:literal) => {{ - Trilogy::from_file(PathBuf::from(TEST_DIR).join($path)).unwrap() + Builder::new() + .build_from_source(PathBuf::from(TEST_DIR).join($path)) + .unwrap() }}; }