Clean up config.rs
#8
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: ryze/ff2mpv-rust#8
Loading…
Reference in a new issue
No description provided.
Delete branch "typesafe-get-config-location"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Some refactoring to
config.rs
with the aim to:std::result::Result
&std::path::PathBuf
APIsTo achieve these goals, the following was done:
get_config_file
propagates anFF2MpvError
if the config file doesn't exist/is invalidunix
&windows
code by usingcfg!
expressions over#[cfg()]
attributes. I generally find this quite useful in order to not forget changes that span multiple platforms. This change also removed some duplicated lines of code#[serde(default)]
toConfig
struct instead of every struct member: https://serde.rs/container-attrs.html#defaultIt's not everyday that you stumble into a codebase which is this small & nice, so these changes were made just for fun. I hope that you find some value in these contributions, otherwise just ignore them. Thanks for you hard work 💜
Merging two implementations of
get_config_location
is definitely a bad idea both for readability and flexibility. I would suggest keeping it separate.As for the other changes:
Using
serde[default]
on the struct as a whole is awesome, nothing to doubt here.Using
unwrap_or_default()
instead ofif let
is more readable.I don't see a reason why we should use
try_exists
and why we should handle missingAPPDATA
@ -1,81 +1,67 @@
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::PathBuf;
I am not sure if the moving of
Deserialize
import was intentional.Use declarations should be ordered as following:
std
;Using
Path.join
instead ofPathBuf.push
here creates a second instance ofPathBuf
. While not significant to notice, this still has some runtime cost, which I don't think Rust's compiler can optimize away.I am not sure if handling
APPDATA
not existing here is a good idea. Are there any legitimate cases whenAPPDATA
is not defined, I think that would indicate the brokenWindows
system typically?get_config_location
was meant to be implemented separately for each platform as it is inherently platform dependent, i.e operating systems have different locations and ways where and how they might store configuration files.Using
cfg!
here not only makes code harder to read (as the reader has to keep in mind theif
blocks are platform dependent, while the rest of the function isn't).cfg!
just evaluates totrue
orfalse
, it doesn't remove any code and we have to rely on compiler for that. In addition to that this makes it impossible to use platform specific extensions, e.gstd::os::windows
, because the compiler doesn't understand that this code block is platform-specific.unimplemented!
would panic at runtime and unnecessarily increase the code size. If we don't support certain platform, it is best to let that know at compile-time.Duplication of
PathBuf
the second time.Why would we use
try_exists
instead ofexists
? This was designed to fallback to default config in case of an error.See
Path.exists
instd
docs.Closing this as there was no follow up in a month. Will incorporate the acceptable changes as coauthored commit directly in repo.
Pull request closed