rust-syndication / rss

Library for serializing the RSS web content syndication format
https://crates.io/crates/rss
Apache License 2.0
419 stars 52 forks source link

Return error type other than `String` from ChannelBuilder::build #86

Closed casey closed 3 years ago

casey commented 4 years ago

I'm using the library to generate a feed for my personal blog, and I have an error type with a lot of From<lib::Error> for MyError implementations for converting errors from crates I use to my general error type.

ChannelBuilder::build returns String as the error type, so in order to get nice error conversions with the ? operator, I would have to implement From<String> for MyError, which seems error (haha) prone.

It would be nice if ChannelBuilder::build returned a type specific to the rss crate, like rss::BuildError, which could even be a simple wrapper for the error message, i.e. struct BuildError(String).

Also, thanks so much for this library!

andy128k commented 4 years ago

@casey This is a limitation of derive_builder.

All structs in rss crate with builders have Default trait implemented. This means that builder's .build() always returns Ok(). So, it is safe to call .build().unwrap().

This is ugly, so you can prefer to stop using builders and use regular setters.

e. g. instead of

let channel = ChannelBuilder::default()
    .title("title")
    .build()
    .unwrap();

just write

let mut channel = Channel::default();
channel.set_title("title");

or (if you need it in an expression)

let channel = {
    let mut channel = Channel::default();
    channel.set_title("title");
    channel
};

Next version will have all fields public, so it will be possible to write like this:

let channel = Channel {
    title: "title",
    ..Default::default(),
};