Skip to content

Commit

Permalink
anyhow -> color_eyre
Browse files Browse the repository at this point in the history
  • Loading branch information
ameknite committed Oct 21, 2023
1 parent db1e35d commit 0846bea
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 38 deletions.
43 changes: 36 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ keywords = ["cli", "attribution", "credits", "license"]
categories = ["command-line-utilities"]

[dependencies]
anyhow = "1.0.75"
bytes = "1.5.0"
cargo_metadata = "0.18.1"
clap = { version = "4.4.6", features = ["derive"] }
color-eyre = { version = "0.6.2", default-features = false }
reqwest = "0.11.22"
serde = { version = "1.0.189", features = ["derive"] }
spdx = "0.10.2"
Expand Down
46 changes: 38 additions & 8 deletions attribution/dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ notices = ["Copyright (c) 2015 Josh Triplett, 2022 The rust-cli Developers"]
repository = "https://github.com/rust-cli/anstyle.git"
homepage = "https://github.com/rust-cli/anstyle"

[[dependencies]]
name = "anyhow"
version = "1.0.75"
description = "Flexible concrete Error type built on std::error::Error"
license = "MIT OR Apache-2.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
repository = "https://github.com/dtolnay/anyhow"

[[dependencies]]
name = "autocfg"
version = "1.1.0"
Expand Down Expand Up @@ -228,6 +220,14 @@ license = "MIT OR Apache-2.0"
notices = ["Copyright (c) 2015-2022 Kevin B. Knapp and Clap Contributors"]
repository = "https://github.com/clap-rs/clap/tree/master/clap_lex"

[[dependencies]]
name = "color-eyre"
version = "0.6.2"
description = "An error report handler for panics and eyre::Reports for colorful, consistent, and well formatted error reports for all kinds of errors."
license = "MIT OR Apache-2.0"
authors = ["Jane Lusby <jlusby@yaah.dev>"]
repository = "https://github.com/yaahc/color-eyre"

[[dependencies]]
name = "colorchoice"
version = "1.0.0"
Expand Down Expand Up @@ -281,6 +281,17 @@ notices = ["Copyright (c) 2014 Chris Wong"]
authors = ["Chris Wong <lambda.fairy@gmail.com>"]
repository = "https://github.com/lambda-fairy/rust-errno"

[[dependencies]]
name = "eyre"
version = "0.6.8"
description = "Flexible concrete Error Reporting type built on std::error::Error with customizable Reports"
license = "MIT OR Apache-2.0"
authors = [
"David Tolnay <dtolnay@gmail.com>",
"Jane Lusby <jlusby42@gmail.com>",
]
repository = "https://github.com/yaahc/eyre"

[[dependencies]]
name = "fastrand"
version = "2.0.1"
Expand Down Expand Up @@ -530,6 +541,16 @@ notices = ["Copyright (c) 2013-2022 The rust-url developers"]
authors = ["The rust-url developers"]
repository = "https://github.com/servo/rust-url/"

[[dependencies]]
name = "indenter"
version = "0.3.3"
description = """
A formatter wrapper that indents the text, designed for error display impls
"""
license = "MIT OR Apache-2.0"
authors = ["Jane Lusby <jlusby@yaah.dev>"]
repository = "https://github.com/yaahc/indenter"

[[dependencies]]
name = "indexmap"
version = "1.9.3"
Expand Down Expand Up @@ -749,6 +770,15 @@ authors = [
]
repository = "https://github.com/sfackler/rust-openssl"

[[dependencies]]
name = "owo-colors"
version = "3.5.0"
description = "Zero-allocation terminal colors that'll make people go owo"
license = "MIT"
notices = ["Copyright (c) 2020 - present The owo-colors Developers"]
authors = ["jam1garner <8260240+jam1garner@users.noreply.github.com>"]
repository = "https://github.com/jam1garner/owo-colors"

[[dependencies]]
name = "parking_lot"
version = "0.12.1"
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use color_eyre::Result;
use std::{fs, path::Path};

pub mod licenses;
pub mod metadata;
pub mod serialize;

pub fn recreate_folder(path: &Path) -> anyhow::Result<()> {
pub fn recreate_folder(path: &Path) -> Result<()> {
if path.try_exists()? {
fs::remove_dir_all(path)?;
}
Expand All @@ -19,7 +20,7 @@ pub fn recreate_folder(path: &Path) -> anyhow::Result<()> {
Ok(())
}

pub fn create_folder(path: &Path) -> anyhow::Result<()> {
pub fn create_folder(path: &Path) -> Result<()> {
if path.try_exists()? {
return Ok(());
}
Expand Down
15 changes: 6 additions & 9 deletions src/licenses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::path::PathBuf;

use bytes::Bytes;
use color_eyre::Result;
use reqwest::{self, Url};
use std::path::PathBuf;
use tokio::{fs::File, io::AsyncWriteExt, task};

use crate::metadata::DependencyData;
Expand All @@ -18,18 +18,18 @@ pub struct LicenseData {
}

impl LicenseData {
pub async fn get_license_content(&self) -> anyhow::Result<Bytes> {
pub async fn get_license_content(&self) -> Result<Bytes> {
let content = reqwest::get(self.url.clone()).await?.bytes().await?;
Ok(content)
}

pub async fn create_license_file(&self, content: Bytes) -> anyhow::Result<()> {
pub async fn create_license_file(&self, content: Bytes) -> Result<()> {
let mut file = File::create(&self.path).await?;
file.write_all(&content).await?;
Ok(())
}

pub async fn generate_license(&self) -> anyhow::Result<()> {
pub async fn generate_license(&self) -> Result<()> {
println!("Downloading {}", self.name);
let content = self.get_license_content().await?;
self.create_license_file(content).await?;
Expand All @@ -38,10 +38,7 @@ impl LicenseData {
}
}

pub async fn generate_licenses(
crates_data: &[DependencyData],
output_dir: PathBuf,
) -> anyhow::Result<()> {
pub async fn generate_licenses(crates_data: &[DependencyData], output_dir: PathBuf) -> Result<()> {
let mut licenses = crates_data
.iter()
.flat_map(|c| c.licenses.clone())
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ use cargo_attribution::{
};
use cargo_metadata::{CargoOpt, MetadataCommand};
use clap::Parser;
use color_eyre::eyre::Result;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> Result<()> {
color_eyre::install()?;

let CargoCli::Attribution(Args {
manifest_path,
current_dir,
Expand Down
12 changes: 6 additions & 6 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use std::fs;

use anyhow::Context;
use cargo_metadata::{DependencyKind, Metadata, Package};
use color_eyre::{eyre::ContextCompat, Result};
use serde::Serialize;
use spdx::Expression;

Expand Down Expand Up @@ -50,11 +50,11 @@ impl DependencyData {
}

impl DependencyData {
pub fn get_license_notices(&mut self, package: &Package) -> anyhow::Result<()> {
pub fn get_license_notices(&mut self, package: &Package) -> Result<()> {
let parent = package
.manifest_path
.parent()
.context("Not manifest parent")?;
.wrap_err("Not manifest parent")?;
for entry in fs::read_dir(parent)? {
let entry = entry?;
let file_path = entry.path();
Expand Down Expand Up @@ -99,8 +99,8 @@ impl DependencyData {
Ok(())
}

pub fn get_parse_licenses(&mut self) -> anyhow::Result<()> {
let license_metadata = self.license.clone().context("License not found")?;
pub fn get_parse_licenses(&mut self) -> Result<()> {
let license_metadata = self.license.clone().wrap_err("License not found")?;

let expression = match Expression::parse(&license_metadata) {
Ok(spdx) => spdx,
Expand Down Expand Up @@ -134,7 +134,7 @@ impl DependencyData {
pub fn get_data(
metadata: Metadata,
only_normal_dependencies: bool,
) -> anyhow::Result<(Vec<DependencyData>, Option<DependencyData>)> {
) -> Result<(Vec<DependencyData>, Option<DependencyData>)> {
let mut dependencies = Vec::with_capacity(metadata.packages.len());
let mut my_crate = None;

Expand Down
9 changes: 5 additions & 4 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::{fs::File, io::Write, path::Path};

use color_eyre::eyre::Result;
use serde::Serialize;
use toml_edit::{ArrayOfTables, Document, Item, Table};

Expand All @@ -25,7 +26,7 @@ impl<'a> DependencySerialized<'a> {
}
}

pub fn to_toml(&self) -> anyhow::Result<String> {
pub fn to_toml(&self) -> Result<String> {
// Serialize your struct to a TOML string
let toml_str = toml::to_string_pretty(&self)?;

Expand All @@ -48,7 +49,7 @@ impl<'a> DependencySerialized<'a> {
Ok(toml_str)
}

pub fn create_toml(&self, output_dir: &Path) -> anyhow::Result<()> {
pub fn create_toml(&self, output_dir: &Path) -> Result<()> {
let mut file = File::create(output_dir.join(format!("{}.toml", self.file_name)))?;
file.write_all(self.to_toml()?.as_bytes())?;
Ok(())
Expand All @@ -71,7 +72,7 @@ impl<'a> SelfSerialized<'a> {
}
}

pub fn to_toml(&self) -> anyhow::Result<String> {
pub fn to_toml(&self) -> Result<String> {
// Serialize your struct to a TOML string
let toml_str = toml::to_string_pretty(&self)?;

Expand All @@ -95,7 +96,7 @@ impl<'a> SelfSerialized<'a> {
Ok(toml_str)
}

pub fn create_toml(&self, output_dir: &Path) -> anyhow::Result<()> {
pub fn create_toml(&self, output_dir: &Path) -> Result<()> {
let mut file = File::create(output_dir.join(format!("{}.toml", self.file_name)))?;
file.write_all(self.to_toml()?.as_bytes())?;
Ok(())
Expand Down

0 comments on commit 0846bea

Please sign in to comment.