Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootspec(generation): offer TryInto conversion from Generation to any… #109

Conversation

RaitoBezarius
Copy link
Contributor

@RaitoBezarius RaitoBezarius commented Apr 14, 2023

Description

… schema version

Enables replacing:

        // TODO: replace me when https://github.com/DeterminateSystems/bootspec/pull/109 lands.
        let bootspec: BootSpec = match boot_json.generation {
            BootspecGeneration::V1(bootspec) => bootspec,
            _ => return Err(anyhow!("Unsupported bootspec schema"))
        };

by

        let bootspec: BootSpec = boot_json.generation.try_into().map_err(|err| ...);
Checklist
  • Built with cargo build
  • Formatted with cargo fmt
  • Linted with cargo clippy
  • Ran tests with cargo test
  • Added or updated relevant tests (leave unchecked if not applicable)
  • Added or updated relevant documentation (leave unchecked if not applicable)

@lheckemann
Copy link
Contributor

Looks good (just needs a cargo fmt), any other reason it's still a draft?

@RaitoBezarius
Copy link
Contributor Author

Looks good (just needs a cargo fmt), any other reason it's still a draft?

It was more like a discussion on whether this PR is a good idea and I made it in 2 minutes so I was not sure of the quality :-)

Copy link
Member

@cole-h cole-h left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally in favor of making this library easier to use for consumers :)

@@ -33,6 +33,17 @@ impl Generation {
}
}

impl TryInto<v1::GenerationV1> for Generation {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't manually implement {Try,}Into ourselves since we get that for free from {Try,}From: https://doc.rust-lang.org/std/convert/trait.TryInto.html

Library authors should usually not directly implement this trait, but should prefer implementing the TryFrom trait, which offers greater flexibility and provides an equivalent TryInto implementation for free, thanks to a blanket implementation in the standard library. For more information on this, see the documentation for Into.

fn try_into(self) -> Result<v1::GenerationV1, Self::Error> {
match self {
Self::V1(gen) => Ok(gen),
_ => return Err(format!("Invalid schema version, expected version 1, got: {}", self.version()).to_string())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but I don't like the fact we'd need a separate impl block for each generation. I think switching to TryFrom will make this point moot, though.

@cole-h
Copy link
Member

cole-h commented May 18, 2023

I was wrong about TryFrom making us able to get rid of the "manually add a new impl block for each variant", but there is a derive_more crate that can help with this. I've opened #120 which should do what you want.

@cole-h cole-h closed this in #120 May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants