Skip to content

Use new spanned diagnostics #320

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/mdbook-goals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ regex = "1.11.1"
rust-project-goals = { version = "0.1.0", path = "../rust-project-goals" }
semver = "1.0.23"
serde_json = "1.0.133"
spanned = "0.4.0"
47 changes: 27 additions & 20 deletions crates/mdbook-goals/src/mdbook_preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use rust_project_goals::config::Configuration;
use rust_project_goals::format_team_ask::format_team_asks;
use rust_project_goals::util::{self, GithubUserInfo};

use rust_project_goals::spanned::Spanned;
use rust_project_goals::{
goal::{self, GoalDocument, Status, TeamAsk},
re, team,
};
use spanned::Spanned;

const LINKS: &str = "links";
const LINKIFIERS: &str = "linkifiers";
Expand Down Expand Up @@ -233,13 +233,16 @@ impl<'c> GoalPreprocessorWithContext<'c> {

// Extract out the list of goals with the given status.
let goals = self.goal_documents(chapter_path)?;
let mut goals_with_status: Vec<&GoalDocument> =
goals.iter().filter(|g| filter(g.metadata.status)).collect();
let mut goals_with_status: Vec<&GoalDocument> = goals
.iter()
.filter(|g| filter(g.metadata.status.content))
.collect();

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

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

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

Ok(())
Expand Down Expand Up @@ -323,7 +327,8 @@ impl<'c> GoalPreprocessorWithContext<'c> {
return Ok(goals.clone());
}

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

let display_name = match team::get_person_data(username)? {
Some(person) => person.data.name.clone(),
None => match GithubUserInfo::load(username)
.with_context(|| format!("loading user info for {}", username))
{
Ok(GithubUserInfo { name: Some(n), .. }) => n,
Ok(GithubUserInfo { name: None, .. }) => username.to_string(),
Err(e) => {
eprintln!("{:?}", e);
username.to_string()
}
},
};
let display_name =
match team::get_person_data(username).map_err(|e| anyhow::anyhow!("{e}"))? {
Some(person) => person.data.name.clone(),
None => match GithubUserInfo::load(username)
.map_err(|e| anyhow::anyhow!("{e}"))
.with_context(|| format!("loading user info for {}", username))
{
Ok(GithubUserInfo { name: Some(n), .. }) => n,
Ok(GithubUserInfo { name: None, .. }) => username.to_string(),
Err(e) => {
eprintln!("{:?}", e);
username.to_string()
}
},
};
let display_name = Rc::new(display_name);
self.display_names
.insert(username.to_string(), display_name.clone());
Expand Down Expand Up @@ -421,7 +428,7 @@ impl<'c> GoalPreprocessorWithContext<'c> {

fn link_teams(&self, chapter: &mut Chapter) -> anyhow::Result<()> {
chapter.content.push_str("\n\n");
for team in team::get_team_names()? {
for team in team::get_team_names().map_err(|e| anyhow::anyhow!("{e}"))? {
chapter
.content
.push_str(&format!("{team}: {}\n", team.url()));
Expand Down
1 change: 0 additions & 1 deletion crates/rust-project-goals-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ clap = { version = "4.5.23", features = ["derive"] }
rust-project-goals-json = { version = "0.1.0", path = "../rust-project-goals-json" }
handlebars = { version = "6.2.0", features = ["dir_source"] }
comrak = "0.31.0"
spanned = "0.4.0"
29 changes: 13 additions & 16 deletions crates/rust-project-goals-cli/src/cfp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use regex::Regex;
use rust_project_goals::spanned::{Error, Spanned};
use rust_project_goals::{spanned::Context as _, spanned::Result};
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -288,8 +289,9 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
println!("Would create/overwrite directory: {}", dir_path.display());
}
} else if !dry_run {
fs::create_dir_all(&dir_path)
.with_context(|| format!("Failed to create directory {}", dir_path.display()))?;
fs::create_dir_all(&dir_path).with_context(|| {
Spanned::here(format!("Failed to create directory {}", dir_path.display()))
})?;
println!("Created directory: {}", dir_path.display());
} else {
println!("Would create directory: {}", dir_path.display());
Expand Down Expand Up @@ -342,7 +344,7 @@ pub fn create_cfp(timeframe: &str, force: bool, dry_run: bool) -> Result<()> {
fn validate_timeframe(timeframe: &str) -> Result<()> {
let re = Regex::new(r"^\d{4}[hH][12]$").unwrap();
if !re.is_match(timeframe) {
anyhow::bail!("Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2)");
return Err(Error::str("Invalid timeframe format. Expected format: YYYYhN or YYYYHN (e.g., 2025h1, 2025H1, 2025h2, or 2025H2"));
}
Ok(())
}
Expand All @@ -356,8 +358,7 @@ fn copy_and_process_template(
dry_run: bool,
) -> Result<()> {
// Read the template file
let template_content = fs::read_to_string(template_path)
.with_context(|| format!("Failed to read template file: {}", template_path))?;
let template_content = Spanned::read_str_from_file(template_path).transpose()?;

// Use the pure function to process the content
let processed_content = text_processing::process_template_content(
Expand All @@ -369,9 +370,9 @@ fn copy_and_process_template(
// Write to destination file
if !dry_run {
File::create(dest_path)
.with_context(|| format!("Failed to create file: {}", dest_path.display()))?
.with_path_context(dest_path, "Failed to create file")?
.write_all(processed_content.as_bytes())
.with_context(|| format!("Failed to write to file: {}", dest_path.display()))?;
.with_path_context(dest_path, "Failed to write to file")?;

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

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

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

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

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

// Write the updated content back to README.md
if !dry_run {
fs::write(readme_path, new_content)
.with_context(|| format!("Failed to write to src/README.md"))?;
fs::write(readme_path, new_content).with_str_context("Failed to write to src/README.md")?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion crates/rust-project-goals-cli/src/generate_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use rust_project_goals::gh::{
issue_id::Repository,
issues::{checkboxes, list_issues_in_milestone, ExistingGithubComment},
};
use rust_project_goals::spanned::Result;
use rust_project_goals_json::{TrackingIssue, TrackingIssueUpdate, TrackingIssues};

pub(super) fn generate_json(
repository: &Repository,
milestone: &str,
json_path: &Option<PathBuf>,
) -> anyhow::Result<()> {
) -> Result<()> {
let issues = list_issues_in_milestone(repository, milestone)?;

let issues = TrackingIssues {
Expand Down
17 changes: 11 additions & 6 deletions crates/rust-project-goals-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Context;
use clap::Parser;
use regex::Regex;
use rust_project_goals::gh::issue_id::Repository;
use rust_project_goals::{
gh::issue_id::Repository,
spanned::{Result, Spanned},
};
use std::path::PathBuf;
use walkdir::WalkDir;

Expand Down Expand Up @@ -109,7 +111,7 @@ enum Command {
},
}

fn main() -> anyhow::Result<()> {
fn main() -> Result<()> {
let opt: Opt = Opt::parse();

match &opt.cmd {
Expand Down Expand Up @@ -138,8 +140,11 @@ fn main() -> anyhow::Result<()> {
commit,
sleep,
} => {
rfc::generate_issues(&opt.repository, path, *commit, *sleep)
.with_context(|| format!("failed to adjust issues; rerun command to resume"))?;
rfc::generate_issues(&opt.repository, path, *commit, *sleep).map_err(|e| {
e.wrap_str(Spanned::here(
"failed to adjust issues; rerun command to resume",
))
})?;
}

Command::TeamRepo {
Expand Down Expand Up @@ -174,7 +179,7 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

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

Expand Down
Loading