From d071c5e456367ed0cc4f907e9e5c7dc46c331399 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 May 2021 16:06:41 +0200 Subject: Replace boxed errors with concrete error types --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/git.rs | 13 +++++++++++-- src/github.rs | 13 ++++++++++++- src/main.rs | 3 ++- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74750ec..35e7af6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" + [[package]] name = "arrayvec" version = "0.5.2" @@ -1003,6 +1009,7 @@ dependencies = [ name = "reflectub" version = "0.0.1" dependencies = [ + "anyhow", "git2", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 4099777..175f63f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.0.1" edition = "2018" [dependencies] +anyhow = "1.0.40" git2 = "0.13.20" reqwest = { version = "0.11.3", features = ["blocking", "json"] } serde = { version = "1.0.126", features = ["derive"] } diff --git a/src/git.rs b/src/git.rs index d83d2b6..7ad919f 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,6 +1,15 @@ +use thiserror; + use std::path::Path; +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("git error")] + Git(#[from] git2::Error), +} + + /// Mirror a repository. /// /// Works like: @@ -12,7 +21,7 @@ pub fn mirror>( url: &str, path: P, description: Option<&str>, -) -> Result<(), Box> { +) -> Result<(), Error> { let mut repo_init_options = git2::RepositoryInitOptions::new(); repo_init_options.bare(true); @@ -51,7 +60,7 @@ pub fn mirror>( /// ``` pub fn update>( path: P, -) -> Result<(), Box> { +) -> Result<(), Error> { let repo = git2::Repository::open_bare(path)?; for remote_opt in &repo.remotes()? { diff --git a/src/github.rs b/src/github.rs index 8c853da..0b20abd 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,5 +1,6 @@ use reqwest::blocking::ClientBuilder; use serde::Deserialize; +use thiserror; const USER_AGENT: &'static str = concat!( @@ -9,6 +10,16 @@ const USER_AGENT: &'static str = concat!( ); +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("request error")] + Http(#[from] reqwest::Error), + + #[error("request header error")] + Header(#[from] reqwest::header::InvalidHeaderValue), +} + + #[derive(Debug, Deserialize)] pub struct Repo { pub id: i64, @@ -21,7 +32,7 @@ pub struct Repo { } -pub fn fetch_repos() -> Result, Box> { +pub fn fetch_repos() -> Result, Error> { let mut headers = reqwest::header::HeaderMap::new(); headers.insert("Accept", "application/vnd.github.v3+json".parse()?); diff --git a/src/main.rs b/src/main.rs index 162c743..ffda552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use anyhow; use sqlx; use tokio; @@ -80,7 +81,7 @@ async fn main() { } -fn mirror(repo: &github::Repo) -> Result<(), Box> { +fn mirror(repo: &github::Repo) -> anyhow::Result<()> { // mirror database // update description // copy cgitrc -- cgit v1.2.3