Skip to content

Commit 92cadb0

Browse files
author
ynn
committed
feat(args.rs): adds --fall-back-to-white and --fall-back-to-black options
1 parent 448079d commit 92cadb0

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Arguments:
5050

5151
Options:
5252
-c, --codel-size <CODEL_SIZE> Specifies the codel size (default: auto detect)
53+
--fall-back-to-white Treats unknown color as white instead of error
54+
--fall-back-to-black Treats unknown color as black instead of error
55+
--max-iter <MAX_ITER> Terminates the program after this number of iterations
5356
-v, --verbose Enables debug output (path trace etc.)
5457
-h, --help Print help
5558
-V, --version Print version
@@ -65,7 +68,9 @@ In this section, we explain how such behaviors are implemented.
6568

6669
> *Additional colours (such as orange, brown) may be used, though their effect is implementation-dependent. In the simplest case, non-standard colours are treated by the language interpreter as the same as white, so may be used freely wherever white is used. (Another possibility is that they are treated the same as black.)*
6770
68-
Our implementation marks any unknown color as an error, immediately terminating the interpreter before your program starts.
71+
By default, our implementation marks any unknown color as an error, immediately terminating the interpreter before your program starts.
72+
73+
You can change this behavior by specifying `--fall-back-to-white` or `--fall-back-to-black` option. The former treats unknown colors as white, and the latter treats them as black.
6974

7075
### 3.2 Codels
7176

src/args.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ pub struct Args {
1111
#[arg(short, long)]
1212
pub codel_size: Option<usize>,
1313

14+
/// Treats unknown colors as white instead of error
15+
#[arg(long)]
16+
pub fall_back_to_white: bool,
17+
18+
/// Treats unknown colors as black instead of error
19+
#[arg(long)]
20+
pub fall_back_to_black: bool,
21+
1422
/// Terminates the program after this number of iterations
1523
#[arg(long)]
1624
pub max_iter: Option<usize>,
@@ -19,3 +27,16 @@ pub struct Args {
1927
#[arg(short, long)]
2028
pub verbose: bool,
2129
}
30+
31+
impl Args {
32+
pub fn validate(&self) -> Result<(), String> {
33+
if self.fall_back_to_white && self.fall_back_to_black {
34+
return Err(
35+
"at most one of `fall_back_to_white` and `fall_back_to_black` can be set"
36+
.to_string(),
37+
);
38+
}
39+
40+
Ok(())
41+
}
42+
}

src/image.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ impl Display for Image {
7272
}
7373

7474
impl Image {
75-
pub fn new(file: impl AsRef<Path>, codel_size: Option<usize>) -> Result<Self, Box<dyn Error>> {
75+
pub fn new(
76+
file: impl AsRef<Path>,
77+
codel_size: Option<usize>,
78+
default_color: Option<Codel>,
79+
) -> Result<Self, Box<dyn Error>> {
7680
let mut pixel_map = vec![];
7781
if !file.as_ref().exists() {
7882
return Err("file not found".into());
@@ -126,7 +130,9 @@ impl Image {
126130
for i in 0..height {
127131
for j in 0..width {
128132
let pixel = pixel_map[i * codel_size][j * codel_size];
129-
let codel = Codel::new(&pixel).ok_or(format!("invalid color at ({}, {})", i, j))?;
133+
let codel = Codel::new(&pixel)
134+
.or(default_color)
135+
.ok_or(format!("invalid color at ({}, {})", i, j))?;
130136
m[i].push(codel);
131137
}
132138
}

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod stdin;
1010

1111
use std::error::Error;
1212

13+
use codel::Codel;
1314
use rustc_hash::FxHashSet;
1415

1516
use crate::args::Args;
@@ -27,7 +28,14 @@ fn debug_print(is_verbose_mode: bool, s: &str) {
2728
/// Runs a Piet program.
2829
//This functions is tested in integration tests.
2930
pub fn run(args: &Args) -> Result<(), Box<dyn Error>> {
30-
let img = Image::new(&args.image_file, args.codel_size)?;
31+
let default_color = if args.fall_back_to_white {
32+
Some(Codel::White)
33+
} else if args.fall_back_to_black {
34+
Some(Codel::Black)
35+
} else {
36+
None
37+
};
38+
let img = Image::new(&args.image_file, args.codel_size, default_color)?;
3139
debug_print(args.verbose, &format!("{}", img));
3240

3341
if img.get_codel_at((0, 0)).is_black() {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ use piet_programming_language::args::Args;
66

77
fn main() -> Result<(), Box<dyn Error>> {
88
let args = Args::parse();
9+
args.validate()?;
910
piet_programming_language::run(&args)
1011
}

0 commit comments

Comments
 (0)