Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions src/schemes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod template;
use crate::schemes::{collections::Collections, template::Template};
use crate::{Error, Result};
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

#[derive(Debug, Default, Clone)]
pub enum Configuration {
Expand All @@ -20,47 +20,44 @@ pub enum Configuration {
}

impl Configuration {
pub fn parse(path: PathBuf) -> Self {
let config = Some(path.clone())
.filter(|p| p.exists())
.map(|p| p.join("bleur.toml"))
.filter(|p| p.exists())
.and_then(|p| fs::read_to_string(p).ok());

// If there's string inside config file
if let Some(text) = config {
// And if it's parsible to Template type
if let Ok(t) = toml::from_str::<Template>(&text) {
return Configuration::Template(t.with_path(path));
}

// And if it's parsible to Collection type
if let Ok(c) = toml::from_str::<Collections>(&text) {
return Configuration::Collections(c);
}
};

// Nothing's there + invalid config file
Self::Empty
pub fn parse(path: &Path) -> Self {
fs::read_to_string(path.join("bleur.toml"))
.ok()
.map(|ref text| {
toml::from_str::<Template>(text)
.map(|t| Configuration::Template(t.with_path(path.to_path_buf())))
.or_else(|_| {
toml::from_str::<Collections>(text).map(Configuration::Collections)
})
.unwrap_or(Configuration::Empty)
})
.unwrap_or(Configuration::Empty)
}

pub fn surely_template(path: PathBuf) -> Result<Self> {
use Configuration::*;

match Self::parse(path.clone()) {
Template(t) => Ok(Self::Template(t)),
match Self::parse(&path) {
// Template found
Template(template) => Ok(Template(template)),

// Config not found
Empty => Err(Error::NoTemplateConfiguration),
Collections(c) => {
let option = inquire::Select::new(

// Collection not found
Collections(collections) => {
let choice = inquire::Select::new(
"Choose the template you would like to bootstrap:",
c.keys(),
collections.keys(),
)
.prompt()
.map_err(Error::CantParseUserPrompt)?;

let option = c.select(option).ok_or(Error::NoSuchTemplateInCollection)?;
let template = collections
.select(choice)
.ok_or(Error::NoSuchTemplateInCollection)?;

Self::surely_template(option.path(path))
Self::surely_template(template.path(path))
}
}
}
Expand Down