Skip to content

Member-level macro to use differently-named fallible constructor method? #131

Closed Answered by lkdm
lkdm asked this question in Q&A
Discussion options

You must be logged in to vote

Please ignore me, I found the article here which points out that I should be using a function builder.

Here's an example of my code, in case its helpful to anyone:

#[derive(Error, Debug, Clone, PartialEq)]
enum ProjectError {
    #[error("Error")]
    TitleError(#[from] TitleError)
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, From)]
pub struct Project {
    title: Title,
}

#[bon]
impl Project {
    #[builder]
    fn new(title: &str) -> Result<Self, ProjectError> {
        let title = Title::try_new(title.to_string())?;
        Ok(Self {title})
    }
}


mod tests {
    #[cfg(test)]
    use super::*;

    #[test]
    fn test_create_project() {
        let project = Project

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Veetaha
Comment options

@lkdm
Comment options

Answer selected by lkdm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants