Skip to content

Commit

Permalink
improve config.toml error msg (#1155)
Browse files Browse the repository at this point in the history
improve config.toml error msg

when I write a unvalid config.toml
```toml
[[sources]]
# name = "company-internal"
url = "https://company.internal/simple/"
```
the before msg is very strange: `error: expected name`
after: `error: bad config.toml syntax,expected source.name`
  • Loading branch information
zys864 authored Jun 24, 2024
1 parent 7412ee4 commit 5df957d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion rye/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ impl Config {
if let Some(sources) = self.doc.get("sources").map(|x| toml::iter_tables(x)) {
for source in sources {
let source = source.context("invalid value for source in config.toml")?;
let source_ref = SourceRef::from_toml_table(source)?;
let source_ref = SourceRef::from_toml_table(source)
.context("invalid source definition in config.toml")?;
if source_ref.name == "default" {
need_default = false;
}
Expand Down
10 changes: 6 additions & 4 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ impl SourceRef {
.get("name")
.and_then(|x| x.as_str())
.map(|x| x.to_string())
.ok_or_else(|| anyhow!("expected name"))?;
.ok_or_else(|| anyhow!("expected source.name"))?;
let url = source
.get("url")
.and_then(|x| x.as_str())
.map(|x| x.to_string())
.ok_or_else(|| anyhow!("expected url"))?;
.ok_or_else(|| anyhow!("expected source.url"))?;
let verify_ssl = source
.get("verify_ssl")
.and_then(|x| x.as_bool())
Expand All @@ -174,7 +174,7 @@ impl SourceRef {
.get("type")
.and_then(|x| x.as_str())
.map_or(Ok(SourceRefType::Index), |x| x.parse::<SourceRefType>())
.context("invalid value for type")?;
.context("invalid value for source.type")?;
Ok(SourceRef {
name,
url,
Expand Down Expand Up @@ -1265,7 +1265,8 @@ fn get_sources(doc: &DocumentMut) -> Result<Vec<SourceRef>, Error> {
{
for source in sources {
let source = source.context("invalid value for pyproject.toml's tool.rye.sources")?;
let source_ref = SourceRef::from_toml_table(source)?;
let source_ref = SourceRef::from_toml_table(source)
.context("invalid source definition in pyproject.toml")?;
rv.push(source_ref);
}
}
Expand Down Expand Up @@ -1320,6 +1321,7 @@ fn get_project_metadata(path: &Path) -> Result<Metadata, Error> {
}
serde_json::from_slice(&metadata.stdout).map_err(Into::into)
}

/// Represents expanded sources.
#[derive(Debug, Clone, Serialize)]
pub struct ExpandedSources {
Expand Down

0 comments on commit 5df957d

Please sign in to comment.