Skip to content

Commit

Permalink
chore(term): add validation method to ConfigResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Aug 31, 2024
1 parent f75ddf9 commit 9152458
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 130 deletions.
20 changes: 18 additions & 2 deletions crates/synd_term/src/config/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,26 @@ impl ConfigResolver {
}
}

impl ConfigResolver {
/// performs validation based on the relationshsips between the various settings.
fn validate(self) -> Result<Self, ConfigResolverBuildError> {
if self.github_enable.resolve() && self.github_pat.resolve_ref().is_empty() {
return Err(ConfigResolverBuildError::ValidateConfigFile(
"github pat is required for github feature".into(),
));
}
Ok(self)
}
}

#[derive(Error, Debug)]
pub enum ConfigResolverBuildError {
#[error("failed to open {path} {err}")]
ConfigFileOpen { path: String, err: io::Error },
#[error(transparent)]
ConfigFileLoad(#[from] ConfigFileError),
#[error("invalid configration: {0}")]
ValidateConfigFile(String),
}

#[derive(Default)]
Expand Down Expand Up @@ -243,7 +257,7 @@ impl ConfigResolverBuilder {
panic!()
};

Ok(ConfigResolver {
let resolver = ConfigResolver {
config_file: config_path,
log_file: Entry {
flag: log_file_flag,
Expand Down Expand Up @@ -329,7 +343,9 @@ impl ConfigResolverBuilder {
default: config::theme::DEFAULT_PALETTE.into(),
},
categories,
})
};

resolver.validate()
}
}

Expand Down

This file was deleted.

68 changes: 42 additions & 26 deletions crates/synd_term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,52 @@ fn build_app(config: ConfigResolver, dry_run: bool) -> anyhow::Result<Applicatio

#[tokio::main]
async fn main() -> ExitCode {
let Args {
config,
log,
cache_dir,
api,
feed,
github,
command,
palette,
dry_run,
} = cli::parse();
// parse args and resolve configuration
let (config, command, dry_run) = {
let Args {
config,
log,
cache_dir,
api,
feed,
github,
command,
palette,
dry_run,
} = cli::parse();

let config = ConfigResolver::builder()
.config_file(config)
.log_file(log)
.cache_dir(cache_dir)
.api_options(api)
.feed_options(feed)
.github_options(github)
.palette(palette)
.build();
let config = match ConfigResolver::builder()
.config_file(config)
.log_file(log)
.cache_dir(cache_dir)
.api_options(api)
.feed_options(feed)
.github_options(github)
.palette(palette)
.try_build()
{
Ok(config) => config,
Err(err) => {
// tracing subscriber is not yet configured
eprintln!("{err}");
return ExitCode::FAILURE;
}
};
(config, command, dry_run)
};

// Subcommand logs to the terminal, while tui writes logs to a file.
let log = if command.is_some() {
None
} else {
Some(config.log_file())
// init tracing
let _guard = {
// Subcommand logs to the terminal, while tui writes logs to a file.
let log = if command.is_some() {
None
} else {
Some(config.log_file())
};
init_tracing(log).unwrap()
};
let _guard = init_tracing(log).unwrap();

// if subcommand is specified, execute it
if let Some(command) = command {
return match command {
cli::Command::Clean(clean) => clean.run(&FileSystem::new()),
Expand Down

0 comments on commit 9152458

Please sign in to comment.