thiserror icon indicating copy to clipboard operation
thiserror copied to clipboard

Generate doc string from error string or vice versa

Open przb opened this issue 1 year ago • 2 comments

Sometimes an error enum's display can also be used as its documentation.

#[derive(Debug, thiserror::Error)]
enum Foo {
    /// A Bar Error Occurred
    #[error("a bar error occurred")]
    Bar,
    /// Something happened with baz
    #[error("something happened with baz")]
    Baz, 
}

It would be useful to use the same string for documentation generation with something to tell thiserror to also use that as a doc string. Something like the following:

    #[error("Something happened with baz", doc = true)]
    Baz, 

could expand to:

    /// Something happened with baz
    #[error("Something happened with baz")]
    Baz, 

Or something like the following:

    /// Something happened with baz
    #[error(doc = true)]
    Baz, 

could expand to:

    /// Something happened with baz
    #[error("Something happened with baz")]
    Baz, 

Is this something that other people think would be useful? Would this make sense to implement? The ui would likely need to be fleshed out a little bit.

przb avatar Nov 03 '25 04:11 przb

I would prefer not to support this in this crate. But there are other crates for deriving Display based on a doc comment.

dtolnay avatar Nov 03 '25 17:11 dtolnay

(I would've personally closed this as "not planned" instead of "completed")

Why not support this tho? Adding doc comment in thiserror_impl::derive_error seems trivial and would make cargo doc documentation more explicit. I don't think this feature would require a lot of maintenance from you, as a maintainer.

nikableh avatar Jan 14 '26 12:01 nikableh