-
Notifications
You must be signed in to change notification settings - Fork 1
feat(config): add FactConfigBuilder for better semantics #135
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
Draft
Molter73
wants to merge
1
commit into
main
Choose a base branch
from
mauro/config-builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use std::{fs::read_to_string, path::PathBuf, sync::LazyLock}; | ||
|
|
||
| use anyhow::Context; | ||
| use clap::Parser; | ||
|
|
||
| use crate::config::FactCli; | ||
|
|
||
| use super::FactConfig; | ||
|
|
||
| pub(super) struct FactConfigBuilder { | ||
| files: Vec<PathBuf>, | ||
| } | ||
|
|
||
| impl FactConfigBuilder { | ||
| pub(super) fn new() -> Self { | ||
| let files = Vec::new(); | ||
| FactConfigBuilder { files } | ||
| } | ||
|
|
||
| pub(super) fn add_files( | ||
| mut self, | ||
| files: &[impl Into<PathBuf> + AsRef<std::ffi::OsStr>], | ||
| ) -> Self { | ||
| for file in files { | ||
| self.files.push(file.into()); | ||
| } | ||
| self | ||
| } | ||
|
|
||
| pub(super) fn files(&self) -> &[PathBuf] { | ||
| self.files.as_slice() | ||
| } | ||
|
|
||
| pub(super) fn build(&self) -> anyhow::Result<FactConfig> { | ||
| let mut config = self | ||
| .files | ||
| .iter() | ||
| .filter(|p| p.exists()) | ||
| .map(|p| { | ||
| let content = | ||
| read_to_string(p).with_context(|| format!("Failed to read {}", p.display()))?; | ||
| FactConfig::try_from(content.as_str()) | ||
| .with_context(|| format!("parsing error while processing {}", p.display())) | ||
| }) | ||
| .try_fold( | ||
| FactConfig::default(), | ||
| |mut config: FactConfig, other: anyhow::Result<FactConfig>| { | ||
| config.update(&other?); | ||
| Ok::<FactConfig, anyhow::Error>(config) | ||
| }, | ||
| )?; | ||
|
|
||
| // Once file configuration is handled, apply CLI arguments | ||
| static CLI_ARGS: LazyLock<FactConfig> = LazyLock::new(|| FactCli::parse().to_config()); | ||
| config.update(&CLI_ARGS); | ||
|
|
||
| Ok(config) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| use fact::config::FactConfig; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> anyhow::Result<()> { | ||
| fact::init_log()?; | ||
| let config = FactConfig::new()?; | ||
|
|
||
| fact::run(config).await | ||
| fact::run().await | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a nit, so feel free to tell me where to go with this one, but this feels like it's breaking the builder pattern a fair bit. The builder should be a bit more ephemeral (and it might even make sense for
buildto consume the builder)But these files (as
PathBuf) are used extensively later on, long after the builder has been used to build the config.Perhaps we could expose this from the config itself since it is the one that owns the content of these files and should know which files it got that information from? WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense when you want a builder that is a one-off that gets destroyed when you are done using it, but in this case we would want to call
.build()every time we need to reload our configuration, in that case we don't want to have to construct the entire builder again. Sure, the builder now is super simple, it only takes the static list of files, but it may grow later on to.This is similar to what the current implementation does, the list of files is static right now, so it is just a list held privately in the config module. Both the reloader and the the config objects can just use the list as needed this way.
For the time being, I think the existing implementation on
mainis good enough, maybe we can leave this PR alone for a bit and see where the configuration of fact goes in the future, maybe we'll take this builder approach back up again, maybe we'll just close the PR in a few months.