complish 0.0.1

Core library for project-aware task management with git integration
Documentation
use std::{env, path::PathBuf};

const HOME_KEY: &str = "COMPLISH_HOME";

pub fn home() -> Option<PathBuf> {
  get_var(HOME_KEY).and_then(
    |path: PathBuf| {
      if path.is_absolute() { Some(path) } else { None }
    },
  )
}

fn get_var<T>(key: &str) -> Option<T>
where
  T: From<String>,
{
  env::var(key).ok().map(T::from)
}

#[cfg(test)]
mod tests {
  use super::*;

  mod home {
    use pretty_assertions::assert_eq;
    use temp_env::{with_var, with_var_unset};

    use super::*;

    #[test]
    fn it_returns_complish_home_when_set() {
      let test_path = "/test/path";

      with_var(HOME_KEY, Some(test_path), || {
        assert!(home().is_some());
        assert_eq!(home().unwrap(), PathBuf::from(test_path));
      });
    }

    #[test]
    fn it_returns_none_when_complish_home_is_set_to_non_absolute_path() {
      let test_path = "test/path";

      with_var(HOME_KEY, Some(test_path), || {
        assert!(home().is_none());
      });
    }

    #[test]
    fn it_returns_none_when_complish_home_is_unset() {
      with_var_unset(HOME_KEY, || {
        assert!(home().is_none());
      });
    }
  }
}