utkarshkukreti / select.rs

A Rust library to extract useful data from HTML documents, suitable for web scraping.
MIT License
959 stars 69 forks source link

Add find_one method. #42

Open XAMPPRocky opened 7 years ago

XAMPPRocky commented 7 years ago

Currently just to find a single element requires a bit of boilerplate. Consider adding a a find_one method that returns Option<Node>.

utkarshkukreti commented 7 years ago

You could do .find(predicate).next(). Or is that what you mean by "a bit of boilerplate"?

XAMPPRocky commented 7 years ago

The boilerplate is not so much in the number of characters as document.find_one(pred).unwrap() is close to document.find(pred).next().unwrap() it's more the boilerplate of only returning collection types, especially when a lot of them could be single element collections.

HighCommander4 commented 5 years ago

Actually, it's not quite the same. I think find_one() should return None if there are two or more nodes in the selection, too.

XAMPPRocky commented 5 years ago

@HighCommander4 I don't think that is intuitive. I've never found myself commonly needing to require that a query result is completely unique. This would be a lot easier to convey through document.find(pred).size_hint()/document.find(pred).collect::<Vec<_>>().len() == 1

tversteeg commented 5 years ago

If it shouldn't be unique, shouldn't it be called find_first instead?