Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

TryFrom<&Path> should not be used #1756

@tdelabro

Description

@tdelabro

impl TryFrom<&Path> for VersionedConstants {
type Error = VersionedConstantsError;
fn try_from(path: &Path) -> Result<Self, Self::Error> {
Ok(serde_json::from_reader(std::fs::File::open(path)?)?)
}
}

https://doc.rust-lang.org/std/convert/trait.TryFrom.html

TryFrom is for type conversion. The implementation here reads from disk and does json parsing, we are way beyond type conversion. Nothing from the resulting type can be found in the original one. By this logic, any function that takes one single argument and returns another single different type could be implemented as From/TryFrom.

Now, what if I also want to offer a way to create the type from a toml file? I can only have a single implem of TryFrom<&Path> for VersionedConstants, so I have to add it to the existing one? How do I know if I should expect a toml or a json. This is obviously a bad design.
The correct way here is to add a from_json_file method to the impl VersionedConstant block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions