-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(token-reader): infrastructure setup
* added `TokenReader` type with basic API * improve existing filter tests to verify TokenReader operation Next: implementation
- Loading branch information
Showing
4 changed files
with
69 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod lexer; | ||
mod key_value_filter; | ||
mod reader; | ||
|
||
pub use lexer::{Lexer, Token, TokenType, Span, BufferType, Buffer}; | ||
pub use key_value_filter::{FilterTypedKeyValuePairs}; | ||
pub use reader::{TokenReader}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::io::{Read, Result, self}; | ||
|
||
use super::{Token, TokenType}; | ||
|
||
pub struct TokenReader<'a, I: Iterator<Item=Token>> { | ||
iter: I, | ||
src: Option<&'a str>, | ||
} | ||
|
||
impl<'a, I: Iterator<Item=Token>> TokenReader<'a, I> { | ||
pub fn new(iter: I, source: Option<&'a str>) -> TokenReader<'a, I> { | ||
TokenReader { | ||
iter: iter, | ||
src: source, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, I: Iterator<Item=Token>> Read for TokenReader<'a, I> { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
Err(io::Error::last_os_error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
extern crate json_tools; | ||
|
||
use json_tools::{Lexer, FilterTypedKeyValuePairs, BufferType, TokenType}; | ||
use std::io::Read; | ||
|
||
use json_tools::{Lexer, FilterTypedKeyValuePairs, BufferType, TokenType, TokenReader}; | ||
|
||
#[test] | ||
fn filter_null_values() { | ||
for &(src, count, fcount) in &[(r#"{"s":null, "s":true, "s":null }"#, 13, 5), | ||
(r#"{"s":null, "s":null, "s":null }"#, 13, 2), | ||
(r#"{"s":true, "s":null, "s":null }"#, 13, 5), | ||
(r#"{"s":true, "s":null "s":null }"#, 12, 5), // invalid is fine | ||
(r#"{"s":true,,,, "s":null, "s":null }"#, 16, 8), | ||
(r#"{"s":null, "s":null, "s":true }"#, 13, 5), | ||
(r#"{"s":true, "s":null, "s":true }"#, 13, 9), | ||
(r#"{"s":true, "s":null "s":true }"#, 12, 8),] { | ||
for &(src, want, count, fcount) in | ||
&[(r#"{ "s":null, "s":true, "s":null }"#, | ||
r#"{"s":true}"#, 13, 5), | ||
(r#"{"s " : null, "s":null, "s":null }"#, | ||
r#"{}"#, 13, 2), | ||
(r#"{"s":true, "s":null, "s":null }"#, | ||
r#"{"s":true}"#, 13, 5), | ||
(r#"{"s":true, "s":null "s":null }"#, | ||
r#"{"s":true}"#, 12, 5), // invalid is fine | ||
(r#"{"s":true,,,, "s":null, "s":null }"#, | ||
r#"{"s":true,,,}"#, 16, 8), | ||
(r#"{"s":null, "s":null, "s":true }"#, | ||
r#"{"s":true}"#, 13, 5), | ||
(r#"{"s":true, "s":null, "s":true }"#, | ||
r#"{"s":true,"s":true}"#, 13, 9), | ||
(r#"{"s":true, "s":null "s":true }"#, | ||
r#"{"s":true"s":true}"#, 12, 8),] { | ||
assert_eq!(Lexer::new(src.bytes(), BufferType::Span).count(), count); | ||
assert_eq!(FilterTypedKeyValuePairs::new(Lexer::new(src.bytes(), BufferType::Span), | ||
TokenType::Null).count(), fcount); | ||
let new_filter = |bt| { | ||
FilterTypedKeyValuePairs::new(Lexer::new(src.bytes(), bt), TokenType::Null) | ||
}; | ||
assert_eq!(new_filter(BufferType::Span).count(), fcount); | ||
|
||
for bt in &[BufferType::Bytes(128), BufferType::Span] { | ||
let mut buf: Vec<u8> = Vec::new(); | ||
let mut byte = [0u8]; | ||
let mut r = TokenReader::new(new_filter(bt.clone()), Some(src)); | ||
|
||
while let Ok(l) = r.read(&mut byte) { | ||
if l == 0 { | ||
break | ||
} | ||
buf.push(byte[0]); | ||
} | ||
assert_eq!(&String::from_utf8(buf).unwrap(), want); | ||
} | ||
} | ||
} |