google_fonts_sources/
error.rs

1use std::{fmt::Display, path::PathBuf};
2
3use crate::metadata::BadMetadata;
4
5//use protobuf::text_format::ParseError;
6
7/// A little helper trait for reporting results we can't recover from
8pub(crate) trait UnwrapOrDie<T, E> {
9    // print_msg should be a closure that eprints a message before termination
10    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/// Errors that occur while trying to find sources
26#[derive(Debug, thiserror::Error)]
27pub enum Error {
28    /// An io error occurred
29    #[error(transparent)]
30    Io(#[from] std::io::Error),
31    /// an error with reading the google/fonts repo
32    #[error(transparent)]
33    Git(#[from] GitFail),
34}
35
36/// Errors that occur while trying to load a config file
37#[derive(Debug, thiserror::Error)]
38pub enum BadConfig {
39    /// The file could not be read
40    #[error(transparent)]
41    Read(#[from] std::io::Error),
42    /// The yaml could not be parsed
43    #[error(transparent)]
44    Yaml(serde_yaml::Error),
45}
46
47/// Things that go wrong when trying to clone and read a font repo
48#[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    /// The expected commit could not be found
59    #[error("could not find commit '{sha}'")]
60    NoCommit { sha: String },
61
62    /// No config file was found
63    #[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/// Things that go wrong when trying to run a git command
76#[derive(Debug, thiserror::Error)]
77pub enum GitFail {
78    /// The git command itself does not execute
79    #[error("git process failed: '{0}'")]
80    ProcessFailed(
81        #[from]
82        #[source]
83        std::io::Error,
84    ),
85    /// The git command returns a non-zero status
86    #[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}