Skip to content

Instantly share code, notes, and snippets.

@dckc
Created October 26, 2012 03:10
Show Gist options
  • Save dckc/3956652 to your computer and use it in GitHub Desktop.
Save dckc/3956652 to your computer and use it in GitHub Desktop.
struct matching problem (compile with rustc structmatch.rs --test)
extern mod std; // else --test goes wonky
extern mod rparse; // https://github.com/jesse99/rparse or: cargo install rparse
use rparse::{Parser, Combinators, ParseFailed, match0 };
pub fn space() -> Parser<@~str> { match0(|ch| ws_char.contains_char(ch)) }
const ws_char: &str = &" \t\r\n\x0A";
#[cfg(test)]
mod test_preliminaries {
// This shows ParseFailed is correctly imported.
fn mk_failed() -> ParseFailed {
ParseFailed{file: @~"", line: 1, col: 2, mesg: @~"hello"}
}
#[test]
fn match_problemr() {
let actual = space()
.parse(@~"f", @"$( x \u0000 is not allowed $)");
match actual {
Ok(_) => fail,
Err(ParseFailed{file: f, line: y, col: x, mesg: txt}) => assert x == 6,
// The with the line below in place of the one above, this compiles.
//Err(_) => ()
}
}
}
@dckc
Copy link
Author

dckc commented Oct 26, 2012

$ rustc  structmatch.rs --test
structmatch.rs:24:7: 24:18 error: `ParseFailed` does not name a structure
structmatch.rs:24     Err(ParseFailed{file: f, line: y, col: x, mesg: txt}) => assert x == 6,
                         ^~~~~~~~~~~
error: aborting due to previous error

@dckc
Copy link
Author

dckc commented Oct 26, 2012

Meanwhile, this parses:

extern mod std; // else --test goes wonky

struct ParseFailed {
  file: @~str,
  line: uint,
  col: uint,
  mesg: @~str
}

type ParseResult = Result<int, ParseFailed>;

fn match_problem() {
    let actual: ParseResult = Ok(1);

    match actual {
      Ok(_) => fail,
      Err(ParseFailed{file: f, line: y, col: x, mesg: txt}) => assert x == 6,
    }
}

@dckc
Copy link
Author

dckc commented Oct 26, 2012

er... compiles, rather

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment