Skip to content

Commit

Permalink
fix calling scan! from a submodule
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
oli-obk committed May 11, 2016
1 parent 103240a commit 6b2090b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "text_io"
version = "0.1.4"
version = "0.1.5"
authors = ["Oliver Schneider <rust-si1949781094849131978611@oli-obk.de>"]
description = "really simple to use panicking input functions"
homepage = "https://github.com/oli-obk/rust-si"
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ macro_rules! read(
/// This macro allows to pass several variables so multiple values can be read
#[macro_export]
macro_rules! scan(
($text:expr, $($arg:expr),*) => { scan!(std::io::stdin().bytes().map(|c| c.unwrap()) => $text, $($arg),*) };
($text:expr, $($arg:expr),*) => { scan!(::std::io::stdin().bytes().map(|c| c.unwrap()) => $text, $($arg),*) };
($input:expr => $text:expr, $($arg:expr),*) => {{
use std::io::Read;
use std::str::FromStr;
use ::std::io::Read;
use ::std::str::FromStr;
// typesafe macros :)
let text: &'static str = $text;
let stdin: &mut Iterator<Item = u8> = &mut ($input);
Expand All @@ -63,15 +63,15 @@ macro_rules! scan(
Some(c) => stdin.take_while(|&ch| ch != c).collect(),
None => stdin.take_while(|ch| !b"\t\r\n ".contains(ch)).collect(),
};
let s = match std::str::from_utf8(&s) {
let s = match ::std::str::from_utf8(&s) {
Ok(s) => s,
Err(e) => {
let n = e.valid_up_to();
if n == 0 {
panic!("input was not valid utf8: {:?}", s);
} else {
panic!("input was only partially valid utf8: \"{}\" followed by {:?}",
std::str::from_utf8(&s[..n]).unwrap(), &s[n..]);
::std::str::from_utf8(&s[..n]).unwrap(), &s[n..]);
}
}
};
Expand Down
14 changes: 14 additions & 0 deletions tests/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[macro_use] extern crate text_io;

mod bla {
pub fn parse(text: String) -> (i32, i32) {
let (a, b): (i32, i32);
scan!(text.bytes() => "{}:{}", a, b);
return (a, b);
}
}

#[test]
fn calling_scan_in_submodule() {
assert_eq!(bla::parse("5:6".to_owned()), (5, 6));
}

0 comments on commit 6b2090b

Please sign in to comment.