Skip to main content

facet_cargo_toml/
lib.rs

1//! Type-safe `Cargo.toml` and `Cargo.lock` parser using [facet](https://github.com/facet-rs/facet).
2//!
3//! # Quick Start
4//!
5//! ```rust,no_run
6//! use facet_cargo_toml::{CargoToml, CargoLock};
7//!
8//! // Parse a Cargo.toml
9//! let manifest = CargoToml::from_path("Cargo.toml")?;
10//! if let Some(pkg) = &manifest.package {
11//!     println!("Package: {:?}", pkg.name);
12//! }
13//!
14//! // Parse a Cargo.lock
15//! let lockfile = CargoLock::from_path("Cargo.lock")?;
16//! println!("Contains {} packages", lockfile.packages.len());
17//! # Ok::<_, facet_cargo_toml::Error>(())
18//! ```
19
20mod lockfile;
21mod manifest;
22
23pub use lockfile::{CRATES_IO_SOURCE, CargoLock, LockPackage};
24pub use manifest::*;
25
26use camino::Utf8PathBuf;
27use facet::Facet;
28
29/// Errors that can occur when parsing `Cargo.toml` or `Cargo.lock` files.
30#[derive(Debug, Facet)]
31#[facet(derive(Error))]
32#[repr(u8)]
33#[non_exhaustive]
34pub enum Error {
35    /// failed to read {path}: {source}
36    Io { path: Utf8PathBuf, source: IoError },
37
38    /// parse error: {message}
39    Parse { message: String },
40}
41
42/// Wrapper for `std::io::Error` that implements `Facet`.
43#[derive(Debug, Facet)]
44#[repr(transparent)]
45pub struct IoError(#[facet(opaque)] std::io::Error);
46
47impl From<std::io::Error> for IoError {
48    fn from(e: std::io::Error) -> Self {
49        IoError(e)
50    }
51}
52
53impl std::fmt::Display for IoError {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        self.0.fmt(f)
56    }
57}