-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derive a parser for delimited data #36
Comments
Nope, you're definitely not the only one who thinks this would be cool. I was just solving day 2 of advent of code, and had a struct: /// A semicolon-separated list of subsets of cube-pickings.
#[derive(From)]
#[cfg_attr(test, derive(PartialEq, Eq, Debug))]
struct CubePickingSubsets(Vec<CubePickingSubset>);
// TODO: this FromStr impl should be automated e.g. by parse_display
impl str::FromStr for CubePickingSubsets {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let cube_subsets = s.split("; ").map(str::parse).collect::<Result<_>>()?;
Ok(Self(cube_subsets))
}
} Would love to see this feature in |
haha yeah that's exactly where this question came from |
I too would find it useful to be able to do that. Also, I may want to customize the implementation of each field in other ways, such as
However, adding more functions to the macro to increase the number of customization methods would make the macro more complex, so I am considering adding just one function instead, as follows.
|
ahhhh that's a good idea. I wrote up a poc for delimited data (https://github.com/tsheinen/parse-display/tree/delimited_fields) and was kinda having the same "well if i add delimited data i would kinda like to be able to do.... as well oh my thats a lot of complexity" realization as i worked on it. add a prelude of common DisplayFormat/FromStrFormat use cases and it's essentially the same thing but implemented more cleanly and extensibly. |
I implemented pub trait DisplayFormat {
type Value;
fn write(&self, f: &mut Formatter, value: &Self::Value) -> Result;
}
pub trait FromStrFormat {
type Value;
type Err;
fn parse(&self, s: &str) -> core::result::Result<Self::Value, Self::Err>;
fn regex(&self) -> &str {
"(?s:.*?)"
}
} I also wrote |
I was just curious: Do you know if |
Since Instead, you need to implement use std::{fmt::Formatter, marker::PhantomData, str::FromStr};
use parse_display::{FromStr, FromStrFormat};
fn main() {
let s: ParsableVec<u32> = "1234".parse().unwrap();
println!("{:?}", s.0); // [1, 2, 3, 4]
}
#[derive(FromStr)]
struct ParsableVec<T: FromStr>(#[from_str(with = CharsFormat)] Vec<T>);
struct CharsFormat;
impl<T: FromStr> FromStrFormat<Vec<T>> for CharsFormat {
type Err = T::Err;
fn parse(&self, s: &str) -> core::result::Result<Vec<T>, Self::Err> {
let mut items = Vec::new();
if !s.is_empty() {
let mut start = 0;
for i in 1..=s.len() {
if s.is_char_boundary(i) {
items.push(s[start..i].parse()?);
start = i;
}
}
}
Ok(items)
}
} |
Any updates on this? Would really love this feature in |
The support for Since
By separating into two crates, I believe it is also appropriate to add formats that depend on other crates (e.g., formats that use chrono::NaiveDate::parse_from_str). These crates will be released as soon as I add |
I'd like to be able to able to parse delimited data -- for example
1, 2, 3, 4, 5
I envision this looking something like
If I have time over the next couple days i'll try and PR this; otherwise thought i'd mention it to see if this would be useful to other people (or if im dumb and this is already doable lol)
The text was updated successfully, but these errors were encountered: