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

TryFrom<&Path> should not be used #1756

Open
tdelabro opened this issue Apr 3, 2024 · 0 comments
Open

TryFrom<&Path> should not be used #1756

tdelabro opened this issue Apr 3, 2024 · 0 comments

Comments

@tdelabro
Copy link
Contributor

tdelabro commented Apr 3, 2024

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant