Skip to content

Commit 920d45b

Browse files
refactor: unify error
1 parent d9014f9 commit 920d45b

File tree

6 files changed

+13
-15
lines changed

6 files changed

+13
-15
lines changed

crates/cli/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ impl ProjectConfig {
9090
read_directory_yaml(self, global_rules, rule_overwrite)
9191
}
9292
// do not report error if no sgconfig.yml is found
93-
pub fn setup(config_path: Option<PathBuf>) -> Result<Option<Self>> {
93+
pub fn setup(config_path: Option<PathBuf>) -> Result<Result<Self>> {
9494
let Some((project_dir, mut sg_config)) = Self::discover_project(config_path)? else {
95-
return Ok(None);
95+
return Ok(Err(anyhow::anyhow!(EC::ProjectNotExist)));
9696
};
9797
let config = ProjectConfig {
9898
project_dir,
@@ -102,7 +102,7 @@ impl ProjectConfig {
102102
};
103103
// sg_config will not use rule dirs and test configs anymore
104104
register_custom_language(&config.project_dir, sg_config)?;
105-
Ok(Some(config))
105+
Ok(Ok(config))
106106
}
107107
}
108108

crates/cli/src/lsp.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ pub struct LspArg {
1515
async fn run_language_server_impl(arg: LspArg) -> Result<()> {
1616
// env_logger::init();
1717
// TODO: move this error to client
18-
let Some(project_config) = ProjectConfig::setup(arg.config.clone())? else {
19-
return Err(anyhow::anyhow!(EC::ProjectNotExist));
20-
};
18+
let project_config = ProjectConfig::setup(arg.config.clone())??;
2119
let stdin = tokio::io::stdin();
2220
let stdout = tokio::io::stdout();
2321
let config_result = project_config.find_rules(Default::default());

crates/cli/src/new.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ pub fn run_create_new(mut arg: NewArg) -> Result<()> {
157157
}
158158
}
159159

160-
fn run_create_entity(entity: Entity, arg: NewArg, project: Option<ProjectConfig>) -> Result<()> {
160+
fn run_create_entity(entity: Entity, arg: NewArg, project: Result<ProjectConfig>) -> Result<()> {
161161
// check if we are under a project dir
162-
if let Some(found) = project {
162+
if let Ok(found) = project {
163163
return do_create_entity(entity, found, arg);
164164
}
165165
// check if we creating a project
@@ -181,9 +181,9 @@ fn do_create_entity(entity: Entity, found: ProjectConfig, arg: NewArg) -> Result
181181
}
182182
}
183183

184-
fn ask_entity_type(arg: NewArg, project: Option<ProjectConfig>) -> Result<()> {
184+
fn ask_entity_type(arg: NewArg, project: Result<ProjectConfig>) -> Result<()> {
185185
// 1. check if we are under a sgconfig.yml
186-
if let Some(found) = project {
186+
if let Ok(found) = project {
187187
// 2. ask users what to create if yes
188188
let entity = arg.ask_entity_type()?;
189189
do_create_entity(entity, found, arg)

crates/cli/src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn register_custom_language_if_is_run(args: &[String]) -> Result<()> {
2222
return Ok(());
2323
};
2424
if arg.starts_with('-') || arg == "run" {
25-
ProjectConfig::setup(None)?;
25+
_ = ProjectConfig::setup(None)?;
2626
}
2727
Ok(())
2828
}

crates/cli/src/scan.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct ScanWithConfig<Printer> {
109109
trace: ScanTrace,
110110
}
111111
impl<P: Printer> ScanWithConfig<P> {
112-
fn try_new(arg: ScanArg, printer: P, project: Option<ProjectConfig>) -> Result<Self> {
112+
fn try_new(arg: ScanArg, printer: P, project: Result<ProjectConfig>) -> Result<Self> {
113113
let mut rule_trace = RuleTrace::default();
114114
let configs = if let Some(path) = &arg.rule {
115115
let rules = read_rule_file(path, None)?;
@@ -119,7 +119,8 @@ impl<P: Printer> ScanWithConfig<P> {
119119
.with_context(|| EC::ParseRule("INLINE_RULES".into()))?;
120120
RuleCollection::try_new(rules).context(EC::GlobPattern)?
121121
} else {
122-
let project_config = project.ok_or_else(|| anyhow::anyhow!(EC::ProjectNotExist))?;
122+
// NOTE: only query project here since -r does not need project
123+
let project_config = project?;
123124
let overwrite = RuleOverwrite::new(&arg.overwrite)?;
124125
let (configs, r_stats) = project_config.find_rules(overwrite)?;
125126
rule_trace = r_stats;

crates/cli/src/verify.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ pub struct TestArg {
187187
}
188188

189189
pub fn run_test_rule(arg: TestArg) -> Result<()> {
190-
let project = ProjectConfig::setup(arg.config.clone())?
191-
.ok_or_else(|| anyhow!(ErrorContext::ProjectNotExist))?;
190+
let project = ProjectConfig::setup(arg.config.clone())??;
192191
if arg.interactive {
193192
let reporter = InteractiveReporter {
194193
output: std::io::stdout(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy