Crates.io | anyseq |
lib.rs | anyseq |
version | 0.2.0 |
source | src |
created_at | 2024-06-04 15:44:58.80131 |
updated_at | 2024-06-15 07:08:40.62087 |
description | Automatically generate serde Deserializer from separated values. |
homepage | https://github.com/yamaura/anyseq |
repository | https://github.com/yamaura/anyseq |
max_upload_size | |
id | 1261577 |
size | 19,970 |
anyseq is a Rust crate that provides a trait named Lexer
for splitting structured text into string tokens.
By implementing this trait, Deserializer
for serde
are automatically generated.
Lexer
for creating token steam.serde
Deserializer when the trait is implemented.Lexer
is compatibility with the winnow
crate, making implementation easier when using winnow
.use anyseq::{Token, Lexer};
use anyseq::de::Deserializer;
use serde::Deserialize;
struct IterLexer;
impl<I, O> Lexer<I, Token<O>> for IterLexer
where
I: Iterator<Item = O>
{
type Error = Box<dyn std::error::Error>;
fn item(input: &mut I) -> Result<Token<O>, Self::Error> {
Ok(match input.next() {
Some(t) => Token::Item(t),
None => Token::SeqEnd,
})
}
}
#[derive(Debug, PartialEq, Deserialize)]
struct Test {
a: i32,
b: String,
}
let mut d = Deserializer::<IterLexer, _, _>::new(["42", "foo"].into_iter());
assert_eq!(
Test {
a: 42,
b: "foo".to_string(),
},
Test::deserialize(&mut d).unwrap()
);