gclone/src/lib.rs

84 lines
2.1 KiB
Rust

//! This crate contains the gclone and pgd commands.
//!
//! The `gclone` command automatically clones a git repository and puts it in the right place.
//! The `pgd` command manages a cache for efficiently being able to print a directory corresponding
//! to a repo or an owner. You can then add the following to automatically `cd` to a directory:
//! ``` zsh
//! cdg() {
//! local newdir
//! newdir=$(pgd $1) && cd $newdir
//! }
//! ```
#![warn(missing_docs)]
use std::{env, fmt, error, result};
use std::process::exit;
use std::io;
use colored::*;
use lazy_static::lazy_static;
mod cache;
pub use cache::Cache;
lazy_static! {
/// The directory in which the repositories will be cloned and searched.
pub static ref GCLONE_PATH: String = {
match env::var("GCLONE_PATH") {
Ok(d) => d,
Err(e) => {
let e: Error = e.into();
eprintln!("{}", e);
exit(1);
},
}
};
}
macro_rules! impl_from_error {
($type: ty, $variant: path, $from: ty) => {
impl From<$from> for $type {
fn from(e: $from) -> $type {
$variant(e)
}
}
}
}
#[derive(Debug)]
/// The error type of gclone.
pub enum Error {
/// Couldn't read the GCLONE_PATH env.
NoGclonePath(env::VarError),
/// An error occured while manipulating files.
IoError(io::Error),
}
impl_from_error!(Error, Error::NoGclonePath, env::VarError);
impl_from_error!(Error, Error::IoError, io::Error);
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::NoGclonePath(e) =>
write!(fmt, "{} {} {}",
"error:".bold().red(),
"couldn't read environment variable GCLONE_PATH".bold(),
e),
Error::IoError(e) =>
write!(fmt, "{} {} {}",
"error:".bold().red(),
"couldn't read or write cache:".bold(),
e),
}
}
}
impl error::Error for Error {}
/// The result type of gclone.
pub type Result<T> = result::Result<T, Error>;