Skip to content

Commit

Permalink
Replaced toml_edit with cargo_toml
Browse files Browse the repository at this point in the history
  • Loading branch information
tonowak committed Nov 9, 2022
1 parent 69ba63a commit 49ba171
Showing 1 changed file with 12 additions and 57 deletions.
69 changes: 12 additions & 57 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,35 @@
#[derive(Debug, Clone)]
pub(crate) struct Manifest<'a> {
pub(crate) path: &'a std::path::Path,
pub(crate) doc: toml_edit::Document,
path: &'a std::path::Path,
parsed: cargo_toml::Manifest,
}

impl<'a> Manifest<'a> {
pub(crate) fn parse(path: &'a std::path::Path) -> anyhow::Result<Self> {
let manifest_text = std::fs::read_to_string(path)
.map_err(|e| anyhow::format_err!("Failed when reading {}: {}", path.display(), e))?;
let doc = manifest_text
.parse()
let parsed = toml::from_str(manifest_text.as_str())
.map_err(|e| anyhow::format_err!("Failed to parse {}: {}", path.display(), e))?;

Ok(Self { path, doc })
Ok(Self { path, parsed })
}
}

pub(crate) fn get_package_name(manifest: &Manifest) -> anyhow::Result<String> {
let package = manifest.doc.get("package").ok_or_else(|| {
let package = manifest.parsed.package.as_ref().ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: no `package` table",
manifest.path.display()
)
})?;
let crate_name = package["name"].as_str().ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid package.name",
manifest.path.display()
)
})?;
Ok(crate_name.to_owned())
Ok(package.name.clone())
}

pub(crate) fn get_lib_target_name(manifest: &Manifest) -> anyhow::Result<String> {
// If there's a [lib] section, return the name it specifies, if any.
if let Some(lib_target) = manifest.doc.get("lib") {
let lib_name = lib_target
.as_table()
.ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid lib item",
manifest.path.display()
)
})?
.get("name");

if let Some(lib_name) = lib_name {
return lib_name.as_str().map(|x| x.to_owned()).ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid lib.name",
manifest.path.display()
)
});
if let Some(product) = &manifest.parsed.lib {
if let Some(lib_name) = &product.name {
return Ok(lib_name.clone());
}
}

Expand All @@ -62,32 +40,9 @@ pub(crate) fn get_lib_target_name(manifest: &Manifest) -> anyhow::Result<String>

pub(crate) fn get_first_bin_target_name(manifest: &Manifest) -> anyhow::Result<String> {
// If there's a [[bin]] section, return the first item's name.
if let Some(lib_target) = manifest.doc.get("bin") {
let bin_name = lib_target
.as_array_of_tables()
.ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid bin item",
manifest.path.display()
)
})?
.into_iter()
.next()
.ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid bin first item",
manifest.path.display()
)
})?
.get("name");

if let Some(bin_name) = bin_name {
return bin_name.as_str().map(|x| x.to_owned()).ok_or_else(|| {
anyhow::format_err!(
"Failed to parse {}: invalid name in first bin item",
manifest.path.display()
)
});
if let Some(product) = manifest.parsed.bin.first() {
if let Some(bin_name) = &product.name {
return Ok(bin_name.clone());
}
}

Expand Down

3 comments on commit 49ba171

@obi1kenobi
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, I like it! And I think the compile times will likely be shorter now, toml_edit was definitely taking a while to compile.

Since these functions now don't have any branches that return Result::Err, consider making them not return Result at all?

@tonowak
Copy link
Collaborator Author

@tonowak tonowak commented on 49ba171 Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right. I'll do that.

@tonowak
Copy link
Collaborator Author

@tonowak tonowak commented on 49ba171 Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, they do return Result:Err, because get_package_name returns it and the other functions execute get_package_name(manifest) at the end.

Please sign in to comment.