error_handling/
error_handling.rs1use dice_parser::{DiceExpr, DiceError};
4
5fn main() {
6 println!("=== Error Handling Examples ===\n");
7
8 println!("1. Parse error (missing sides):");
10 match DiceExpr::parse("2d") {
11 Ok(_) => println!(" Unexpectedly succeeded!"),
12 Err(e) => println!(" Error: {}", e),
13 }
14 println!();
15
16 println!("2. Syntax error (negative dice count):");
18 match DiceExpr::parse("-2d6") {
19 Ok(_) => println!(" Unexpectedly succeeded!"),
20 Err(e) => println!(" Error: {}", e),
21 }
22 println!();
23
24 println!("3. Trailing input error:");
26 match DiceExpr::parse("2d6 extra") {
27 Ok(_) => println!(" Unexpectedly succeeded!"),
28 Err(e) => println!(" Error: {}", e),
29 }
30 println!();
31
32 println!("4. Invalid roll specification:");
34 use dice_parser::{RollSpec, Keep};
35 let spec = RollSpec::new(2, 6, Some(Keep::Highest(5))); let expr = DiceExpr::Roll(spec);
37 match expr.roll() {
38 Ok(_) => println!(" Unexpectedly succeeded!"),
39 Err(e) => println!(" Error: {}", e),
40 }
41 println!();
42
43 println!("5. Pattern matching on DiceError:");
45 let result = DiceExpr::parse("abc");
46 match result {
47 Ok(_) => println!(" Unexpectedly succeeded!"),
48 Err(DiceError::ParseError { kind, input, start, stop: _ }) => {
49 println!(" Parse error at position {}: {:?}", start, kind);
50 println!(" Input: {}", input);
51 }
52 Err(e) => println!(" Other error: {}", e),
53 }
54}