mod clean;
mod edit;
mod file;
mod normalize;
mod profile;
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::str;
use anyhow::{Context as ResultExt, Error, Result};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use url::Url;
pub use crate::config::clean::clean;
pub use crate::config::edit::{EditConfig, EditPlugin};
pub use crate::config::file::{GistRepository, GitHubRepository, GitProtocol, RawPlugin};
pub use crate::config::profile::MatchesProfile;
#[derive(Debug)]
pub struct Config {
pub shell: Option<Shell>,
pub matches: Option<Vec<String>>,
pub apply: Option<Vec<String>>,
pub templates: IndexMap<String, String>,
pub plugins: Vec<Plugin>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Shell {
Bash,
Zsh,
}
#[derive(Debug, PartialEq, Eq)]
pub enum Plugin {
External(ExternalPlugin),
Inline(InlinePlugin),
}
#[derive(Debug, PartialEq, Eq)]
pub struct ExternalPlugin {
pub name: String,
pub source: Source,
pub dir: Option<String>,
pub uses: Option<Vec<String>>,
pub apply: Option<Vec<String>>,
pub profiles: Option<Vec<String>>,
pub hooks: Option<BTreeMap<String, String>>,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Source {
Git {
url: Url,
reference: Option<GitReference>,
},
Remote { url: Url },
Local { dir: PathBuf },
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum GitReference {
Branch(String),
Rev(String),
Tag(String),
}
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct InlinePlugin {
pub name: String,
pub raw: String,
pub profiles: Option<Vec<String>>,
pub hooks: Option<BTreeMap<String, String>>,
}
pub fn from_path<P>(path: P, warnings: &mut Vec<Error>) -> Result<Config>
where
P: AsRef<Path>,
{
let path = path.as_ref();
let bytes =
fs::read(path).with_context(|| format!("failed to read from `{}`", path.display()))?;
let contents = String::from_utf8(bytes).context("config file contents are not valid UTF-8")?;
let raw_config = toml::from_str(&contents).context("failed to deserialize contents as TOML")?;
normalize::normalize(raw_config, warnings)
}