[][src]Crate serde_scan

Easily deserialize whitespace seperated data into any rust data structure supported by serde. Useful for demos, programming contests, and the like.

current issues:

  • no support for enums with struct variants
  • structs or tuples cannot contain an unbounded container, like a Vec or HashMap.

Example

extern crate serde;
extern crate serde_scan;

#[macro_use]
extern crate serde_derive;

#[derive(Deserialize, Debug, PartialEq)]
struct Triple {
    a: u32,
    b: u32,
    c: u32,
}

#[derive(Deserialize, Debug, PartialEq)]
enum Command {
    Q,
    Help,
    Size(usize, usize),
    Color(u8),
}

fn main() {
    let s = "1 2 3";

    let a: [u32; 3] = serde_scan::from_str(s).unwrap();
    assert_eq!(a, [1, 2, 3]);

    let b: (u32, u32, u32) = serde_scan::from_str(s).unwrap();
    assert_eq!(b, (1, 2, 3));

    let c: Triple = serde_scan::from_str(s).unwrap();
    assert_eq!(c, Triple { a: 1, b: 2, c: 3 });

    let s = "Size 1 2";
    let size: Command = serde_scan::from_str(s).unwrap();
    assert_eq!(size, Command::Size(1, 2));
}

Macros

scan

The scan! macro.

Enums

ScanError

Functions

from_str

Parse a string contaning whitespace seperated data.

from_str_skipping

Parse a string contaning data seperated by whitespace or any character in the given skip string.

next_line

Get a line of input from stdin, and parse it.