add iterator based read_files_iter -> Iterator<Result<Directive>> method#115
add iterator based read_files_iter -> Iterator<Result<Directive>> method#115jakobhellermann wants to merge 1 commit intojcornaz:mainfrom
read_files_iter -> Iterator<Result<Directive>> method#115Conversation
often, yes. But not necessarily always. In this case, I am a bit skeptical that it provides enough benefits to justify the implementation complexity. Could you show me an example of code for which using the iterator is tangibly better than using the callback version? |
|
Hm, to be honest for the actual code I ended up with it doesn't make much of a difference: let mut directives = Vec::new();
for entry in beancount_parser::read_files_iter::<Decimal>(files) {
if let Entry::Directive(directive) = entry? {
directives.push(directive);
}
}vs let mut directives = Vec::new();
beancount_parser::read_files::<Decimal>(files, |directive| {
if let Entry::Directive(directive) = entry? {
directives.push(directive);
}
});It would be more of an advantage if I wanted to collect all directives let directives = beancount_parser::read_files_iter(files).collect::<Result<Vec<_>,_>>()?;or ignore all errors let directives = beancount_parser::read_files_iter(files).filter_map(Result::ok);Actually, I just remembered a major advantage of the custom iterator: the ability to find out which files were loaded through the let files = path.iter().map(Clone::clone);
let mut iter = beancount_parser::read_files_iter::<Decimal>(files);
for entry in iter.by_ref() {
if let Entry::Directive(directive) = entry? {
directives.push(directive);
}
}
let loaded_files = iter.loaded();
let _watcher = notify::watch_files(loaded_files);I don't think that would be possible with Also, one option I'd like to mention is that you could implement |
That could be achieved in an even simpler way with the new let directives = beancount_parser::read_files_to_vec::<f64>(files)?;
Good point. The current API doesn't allow to ignore errors. I'll think about that.
Ah... ok. I get that. I have to think about what would be the best solution for this use-case specifically.
Yes, of course, and I would want to do it that way. But the Thanks for your answers, they are good points. I'll think about it. |
1f23ea5 to
2f0e0d7
Compare
|
In version 2.5.0, I made the In other words, it is now possible to get the list of path that was loaded. Either by handling the include directives, or by loading them into a Of course, it is still not possible to filter or handle errors individually or partially load files that do not contain any error. Now, I am wondering, is this a use-case actually have? |
|
No I don't, I think this should be all that I need. Thanks! |
Thanks to you! |
It's often more convenient to work with an iterator, rather than a calllback function.
This PR adds
beancount_parser::read_files_iter, which works likeread_filesexcept that it returns an iterator over allEntrys, recursively.Do you have a preference for whether
Importentries should be emitted as well?I think I remember having a use case but I can't find it right now.
I lean towards yes, because that's less magic, preserves more information, and if you're only interested in Directives you're gonna have to pattern match either way.