google_fonts_sources/
error.rs1use std::{fmt::Display, path::PathBuf};
2
3use crate::metadata::BadMetadata;
4
5pub(crate) trait UnwrapOrDie<T, E> {
9 fn unwrap_or_die(self, print_msg: impl FnOnce(E)) -> T;
11}
12
13impl<T, E: Display> UnwrapOrDie<T, E> for Result<T, E> {
14 fn unwrap_or_die(self, print_msg: impl FnOnce(E)) -> T {
15 match self {
16 Ok(val) => val,
17 Err(e) => {
18 print_msg(e);
19 std::process::exit(1)
20 }
21 }
22 }
23}
24
25#[derive(Debug, thiserror::Error)]
27pub enum Error {
28 #[error(transparent)]
30 Io(#[from] std::io::Error),
31 #[error(transparent)]
33 Git(#[from] GitFail),
34}
35
36#[derive(Debug, thiserror::Error)]
38pub enum BadConfig {
39 #[error(transparent)]
41 Read(#[from] std::io::Error),
42 #[error(transparent)]
44 Yaml(serde_yaml::Error),
45}
46
47#[derive(Debug, thiserror::Error)]
49pub enum LoadRepoError {
50 #[error("could not create local directory: '{0}'")]
51 Io(
52 #[from]
53 #[source]
54 std::io::Error,
55 ),
56 #[error(transparent)]
57 GitFail(#[from] GitFail),
58 #[error("could not find commit '{sha}'")]
60 NoCommit { sha: String },
61
62 #[error("no config file was found")]
64 NoConfig,
65 #[error("couldn't load config file: '{0}'")]
66 BadConfig(
67 #[source]
68 #[from]
69 BadConfig,
70 ),
71 #[error("reposity requires an auth token but GITHUB_TOKEN not set")]
72 MissingAuth,
73}
74
75#[derive(Debug, thiserror::Error)]
77pub enum GitFail {
78 #[error("git process failed: '{0}'")]
80 ProcessFailed(
81 #[from]
82 #[source]
83 std::io::Error,
84 ),
85 #[error("git failed: '{stderr}'")]
87 GitError { path: PathBuf, stderr: String },
88}
89
90pub(crate) enum MetadataError {
91 Read(std::io::Error),
92 Parse(BadMetadata),
93}
94
95impl Display for MetadataError {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 match self {
98 MetadataError::Read(e) => e.fmt(f),
99 MetadataError::Parse(e) => e.fmt(f),
100 }
101 }
102}