Skip to content

Instantly share code, notes, and snippets.

@Songbird0
Created April 12, 2018 21:37
Show Gist options
  • Save Songbird0/450869d4a00234a0636be760d3a55be8 to your computer and use it in GitHub Desktop.
Save Songbird0/450869d4a00234a0636be760d3a55be8 to your computer and use it in GitHub Desktop.
Somewhat theorical draft
extern crate toml;
#[macro_use]
extern crate serde_derive;
macro_rules! generate_struct {
($struct_name:ident, $( $property_name:ident, $property_type:ty ),*) => (
use std::fmt::Debug;
#[derive(Debug)]
struct $struct_name {
$(
$property_name: $property_type
),*
}
);
}
use std::{
fmt::Debug,
convert,
path,
fs
};
#[derive(Debug, Deserialize)]
struct Config {
// will be converted into `Path` later
project_home: String,
// will be converted into `Path` later
project_root: String,
subroots: Option<Vec<SubrootConfig>>
}
#[derive(Debug, Deserialize)]
struct SubrootConfig {
path: String,
}
struct Project {
configuration_file_path: Option<path::PathBuf>,
configuration_file: Option<Config>,
transition_directory: Option<TransitionDirectory>,
root_path: path::PathBuf,
sub_roots: Option<Vec<SubRoot>>
}
impl Project {
/// Create a new `Project`.
pub fn new<P>(configuration_file_path: P) -> Self
where
P: Option<convert::AsRef<path::Path>>
{
let configuration_file: Config = ... configuration_file_path ...;
let temp_root_path = {
let tmp = path::Path::new(configuration_file.project_home);
if !tmp.exists() {
panic!("The `project_home` must be present. See your path: {}", tmp);
}
if !tmp.is_directory() {
panic!("`project_home` must be a directory.");
}
let mut buffer = path::PathBuf::new();
buffer.push(configuration_file.project_home);
buffer.push(configuration_file.project_root);
let hypothetical_path: &path::Path = buffer.as_path();
if !hypothetical_path.exists() {
fs::create_dir(hypothetical_path)
.map_err(|error| panic!("An error was occured while creating the \"{0}\" directory.\n
See the error: \"{1}\"", hypothetical_path, error))
.unwrap();
}
else {
if !hypothetical_path.is_directory() {
panic!("\"{}\" must be a directory.", hypothetical_path);
}
}
buffer
};
let temp_sub_roots = match configuration_file.subroots {
Some(content) => if content.is_empty() {
panic!("A non-null `Vec` cannot be empty in this case!");
},
None => None
};
Project {
configuration_file: configuration_file,
root_path: temp_root_path,
sub_roots: temp_sub_roots
};
}
pub fn has_subroots(self) -> bool {
self.sub_roots.is_some()
}
}
fn main() {
// possible path:
// "foo/bar"
// "foo/bang"
let subroots_only = r#"
[root]
path = "foo"
[[subroots]]
path = "bar"
[[subroots]]
path = "bang"
"#;
let subroots_only_content: Config = toml::from_str(subroots_only).unwrap();
let path_list: Option<Vec<String>> = match subroots_only_content.subroots {
// The user has submitted a list of sub-roots.
Some(config_list) => {
let iter = config_list.iter();
let mut temp_path_list: Vec<String> = vec![];
for &config in iter {
temp_path_list.push(config.path);
}
Some(temp_path_list)
},
None => None
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment