Skip to content

Commit f9bf422

Browse files
committed
Use new spanned diagnostics everywhere
1 parent 09da95e commit f9bf422

File tree

18 files changed

+271
-271
lines changed

18 files changed

+271
-271
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/mdbook-goals/src/mdbook_preprocessor.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,16 @@ impl<'c> GoalPreprocessorWithContext<'c> {
233233

234234
// Extract out the list of goals with the given status.
235235
let goals = self.goal_documents(chapter_path)?;
236-
let mut goals_with_status: Vec<&GoalDocument> =
237-
goals.iter().filter(|g| filter(g.metadata.status)).collect();
236+
let mut goals_with_status: Vec<&GoalDocument> = goals
237+
.iter()
238+
.filter(|g| filter(g.metadata.status.content))
239+
.collect();
238240

239241
goals_with_status.sort_by_key(|g| &g.metadata.title);
240242

241243
// Format the list of goals and replace the `<!-- -->` comment with that.
242-
let output = goal::format_goal_table(&goals_with_status)?;
244+
let output =
245+
goal::format_goal_table(&goals_with_status).map_err(|e| anyhow::anyhow!("{e}"))?;
243246
chapter.content.replace_range(range, &output);
244247

245248
// Populate with children if this is not README
@@ -281,7 +284,8 @@ impl<'c> GoalPreprocessorWithContext<'c> {
281284
.filter(|g| g.metadata.status.is_not_not_accepted())
282285
.flat_map(|g| &g.team_asks)
283286
.collect();
284-
let format_team_asks = format_team_asks(&asks_of_any_team)?;
287+
let format_team_asks =
288+
format_team_asks(&asks_of_any_team).map_err(|e| anyhow::anyhow!("{e}"))?;
285289
chapter.content.replace_range(range, &format_team_asks);
286290

287291
Ok(())
@@ -323,7 +327,8 @@ impl<'c> GoalPreprocessorWithContext<'c> {
323327
return Ok(goals.clone());
324328
}
325329

326-
let goal_documents = goal::goals_in_dir(&self.ctx.config.book.src.join(milestone_path))?;
330+
let goal_documents = goal::goals_in_dir(&self.ctx.config.book.src.join(milestone_path))
331+
.map_err(|e| anyhow::anyhow!("{e}"))?;
327332
let goals = Arc::new(goal_documents);
328333
self.goal_document_map
329334
.insert(milestone_path.to_path_buf(), goals.clone());
@@ -380,19 +385,21 @@ impl<'c> GoalPreprocessorWithContext<'c> {
380385
return Ok(n.clone());
381386
}
382387

383-
let display_name = match team::get_person_data(username)? {
384-
Some(person) => person.data.name.clone(),
385-
None => match GithubUserInfo::load(username)
386-
.with_context(|| format!("loading user info for {}", username))
387-
{
388-
Ok(GithubUserInfo { name: Some(n), .. }) => n,
389-
Ok(GithubUserInfo { name: None, .. }) => username.to_string(),
390-
Err(e) => {
391-
eprintln!("{:?}", e);
392-
username.to_string()
393-
}
394-
},
395-
};
388+
let display_name =
389+
match team::get_person_data(username).map_err(|e| anyhow::anyhow!("{e}"))? {
390+
Some(person) => person.data.name.clone(),
391+
None => match GithubUserInfo::load(username)
392+
.map_err(|e| anyhow::anyhow!("{e}"))
393+
.with_context(|| format!("loading user info for {}", username))
394+
{
395+
Ok(GithubUserInfo { name: Some(n), .. }) => n,
396+
Ok(GithubUserInfo { name: None, .. }) => username.to_string(),
397+
Err(e) => {
398+
eprintln!("{:?}", e);
399+
username.to_string()
400+
}
401+
},
402+
};
396403
let display_name = Rc::new(display_name);
397404
self.display_names
398405
.insert(username.to_string(), display_name.clone());
@@ -421,7 +428,7 @@ impl<'c> GoalPreprocessorWithContext<'c> {
421428

422429
fn link_teams(&self, chapter: &mut Chapter) -> anyhow::Result<()> {
423430
chapter.content.push_str("\n\n");
424-
for team in team::get_team_names()? {
431+
for team in team::get_team_names().map_err(|e| anyhow::anyhow!("{e}"))? {
425432
chapter
426433
.content
427434
.push_str(&format!("{team}: {}\n", team.url()));

crates/rust-project-goals-cli/src/cfp.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use anyhow::{Context, Result};
21
use regex::Regex;
2+
use rust_project_goals::spanned::{Error, Spanned};
3+
use rust_project_goals::{spanned::Context as _, spanned::Result};
34
use std::fs::{self, File};
45
use std::io::Write;
56
use std::path::{Path, PathBuf};
@@ -288,8 +289,9 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
288289
println!("Would create/overwrite directory: {}", dir_path.display());
289290
}
290291
} else if !dry_run {
291-
fs::create_dir_all(&dir_path)
292-
.with_context(|| format!("Failed to create directory {}", dir_path.display()))?;
292+
fs::create_dir_all(&dir_path).with_context(|| {
293+
Spanned::here(format!("Failed to create directory {}", dir_path.display()))
294+
})?;
293295
println!("Created directory: {}", dir_path.display());
294296
} else {
295297
println!("Would create directory: {}", dir_path.display());
@@ -342,7 +344,7 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
342344
fn validate_timeframe(timeframe: &str) -> Result<()> {
343345
let re = Regex::new(r"^\d{4}[hH][12]$").unwrap();
344346
if !re.is_match(timeframe) {
345-
anyhow::bail!("Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2)");
347+
return Err(Error::str("Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2"));
346348
}
347349
Ok(())
348350
}
@@ -356,8 +358,7 @@ fn copy_and_process_template(
356358
dry_run: bool,
357359
) -> Result<()> {
358360
// Read the template file
359-
let template_content = fs::read_to_string(template_path)
360-
.with_context(|| format!("Failed to read template file: {}", template_path))?;
361+
let template_content = Spanned::read_str_from_file(template_path).transpose()?;
361362

362363
// Use the pure function to process the content
363364
let processed_content = text_processing::process_template_content(
@@ -369,9 +370,9 @@ fn copy_and_process_template(
369370
// Write to destination file
370371
if !dry_run {
371372
File::create(dest_path)
372-
.with_context(|| format!("Failed to create file: {}", dest_path.display()))?
373+
.with_path_context(dest_path, "Failed to create file")?
373374
.write_all(processed_content.as_bytes())
374-
.with_context(|| format!("Failed to write to file: {}", dest_path.display()))?;
375+
.with_path_context(dest_path, "Failed to write to file")?;
375376

376377
println!("Created file: {}", dest_path.display());
377378
} else {
@@ -383,8 +384,7 @@ fn copy_and_process_template(
383384
/// Updates the SUMMARY.md file to include the new timeframe section
384385
fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool) -> Result<()> {
385386
let summary_path = "src/SUMMARY.md";
386-
let content =
387-
fs::read_to_string(summary_path).with_context(|| format!("Failed to read SUMMARY.md"))?;
387+
let content = fs::read_to_string(summary_path).with_str_context("Failed to read SUMMARY.md")?;
388388

389389
// Use the pure function to process the content
390390
let new_content =
@@ -408,8 +408,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
408408

409409
// Write the updated content back to SUMMARY.md
410410
if !dry_run {
411-
fs::write(summary_path, new_content)
412-
.with_context(|| format!("Failed to write to SUMMARY.md"))?;
411+
fs::write(summary_path, new_content).with_str_context("Failed to write to SUMMARY.md")?;
413412

414413
println!("Updated SUMMARY.md with {} section", timeframe);
415414
} else {
@@ -422,8 +421,7 @@ fn update_summary_md(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
422421
/// Updates the src/README.md with information about the new timeframe
423422
fn update_main_readme(timeframe: &str, lowercase_timeframe: &str, dry_run: bool) -> Result<()> {
424423
let readme_path = "src/README.md";
425-
let content =
426-
fs::read_to_string(readme_path).with_context(|| format!("Failed to read README.md"))?;
424+
let content = fs::read_to_string(readme_path).with_str_context("Failed to read README.md")?;
427425

428426
// Use the pure function to process the content
429427
let new_content =
@@ -495,8 +493,7 @@ fn update_main_readme(timeframe: &str, lowercase_timeframe: &str, dry_run: bool)
495493

496494
// Write the updated content back to README.md
497495
if !dry_run {
498-
fs::write(readme_path, new_content)
499-
.with_context(|| format!("Failed to write to src/README.md"))?;
496+
fs::write(readme_path, new_content).with_str_context("Failed to write to src/README.md")?;
500497
}
501498

502499
Ok(())

crates/rust-project-goals-cli/src/generate_json.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use rust_project_goals::gh::{
44
issue_id::Repository,
55
issues::{checkboxes, list_issues_in_milestone, ExistingGithubComment},
66
};
7+
use rust_project_goals::spanned::Result;
78
use rust_project_goals_json::{TrackingIssue, TrackingIssueUpdate, TrackingIssues};
89

910
pub(super) fn generate_json(
1011
repository: &Repository,
1112
milestone: &str,
1213
json_path: &Option<PathBuf>,
13-
) -> anyhow::Result<()> {
14+
) -> Result<()> {
1415
let issues = list_issues_in_milestone(repository, milestone)?;
1516

1617
let issues = TrackingIssues {

crates/rust-project-goals-cli/src/main.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use anyhow::Context;
21
use clap::Parser;
32
use regex::Regex;
4-
use rust_project_goals::gh::issue_id::Repository;
3+
use rust_project_goals::{
4+
gh::issue_id::Repository,
5+
spanned::{Result, Spanned},
6+
};
57
use std::path::PathBuf;
68
use walkdir::WalkDir;
79

@@ -109,7 +111,7 @@ enum Command {
109111
},
110112
}
111113

112-
fn main() -> anyhow::Result<()> {
114+
fn main() -> Result<()> {
113115
let opt: Opt = Opt::parse();
114116

115117
match &opt.cmd {
@@ -138,8 +140,11 @@ fn main() -> anyhow::Result<()> {
138140
commit,
139141
sleep,
140142
} => {
141-
rfc::generate_issues(&opt.repository, path, *commit, *sleep)
142-
.with_context(|| format!("failed to adjust issues; rerun command to resume"))?;
143+
rfc::generate_issues(&opt.repository, path, *commit, *sleep).map_err(|e| {
144+
e.wrap_str(Spanned::here(
145+
"failed to adjust issues; rerun command to resume",
146+
))
147+
})?;
143148
}
144149

145150
Command::TeamRepo {
@@ -174,7 +179,7 @@ fn main() -> anyhow::Result<()> {
174179
Ok(())
175180
}
176181

177-
fn check() -> anyhow::Result<()> {
182+
fn check() -> Result<()> {
178183
// Look for all directories like `2024h2` or `2025h1` and load goals from those directories.
179184
let regex = Regex::new(r"\d\d\d\dh[12]")?;
180185

0 commit comments

Comments
 (0)