o2sh / onefetch

Command-line Git information tool
https://onefetch.dev
MIT License
9.86k stars 270 forks source link

Consider using cargo-util-schemas instead of cargo_toml #1361

Open musicinmybrain opened 3 months ago

musicinmybrain commented 3 months ago

Summary 💡

As suggested in a Fedora package review, you might consider replacing the cargo_toml crate with cargo-util-schemas (plus toml). While cargo-util-schemas doesn’t have as much high-level functionality as cargo_toml, it seems like it should be adequate for what onefetch-manifest needs.

Motivation 🔦

The advantage of cargo-util-schemas is that it is the official implementation of serialization and deserialization schemas used by cargo itself, so if you use it, you can be confident that your Cargo.toml support remains consistent with cargo‘s.

musicinmybrain commented 3 months ago

The beginning of this could look something like

    let data = std::fs::read_to_string(path)?;
    let m: cargo_util_schemas::manifest::TomlManifest = toml::from_str(&data)?;

and then perhaps

    let package = *m.package.context("Not a package (only a workspace)")?;
    let description = package.description.map(|v| v.as_value().cloned()).flatten();

but after that there are decisions and/or slightly more invasive changes to be made. In particular, cargo_toml claims a package always has a version, but in cargo-util-schemas the version field is an Option<…>, which is inconsistent with the current definition of onefetch_manifest::Manifest:

https://github.com/o2sh/onefetch/blob/ac7e2b8e3313c70f7e7960aefa1296e302ac19e6/manifest/src/lib.rs#L6-L14

spenserblack commented 3 months ago

If you'd like, feel free to submit a PR 🙂