Skip to content

Commit

Permalink
fix some tests without std feature enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfriends committed Apr 7, 2024
1 parent bfb39b9 commit 99fcca0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
30 changes: 25 additions & 5 deletions trilogy/src/bin/trilogy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down
53 changes: 36 additions & 17 deletions trilogy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions trilogy/tests/samples.rs
Original file line number Diff line number Diff line change
@@ -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()
}};
}

Expand Down

0 comments on commit 99fcca0

Please sign in to comment.