1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pub mod items;

use std::process::Command;

use crate::items::traits::{Decorate, Builder};
use crate::items::apache::ApacheBuilder;
use crate::items::example::ExamplesBuilder;
use crate::items::gitignore::GitIgnoreBuilder;
use crate::items::mit::MitBuilder;
use crate::items::readme::ReadmeBuilder;
use crate::items::toml::TomlBuilder;
use crate::items::yml::YmlBuilder;

const APACHE   : &'static str = "apache";
const EXAMPLE  : &'static str = "example";
const GITIGNORE: &'static str = "gitignore";
const MIT      : &'static str = "mit";
const README   : &'static str = "readme";
const TOML     : &'static str = "toml";
const YML      : &'static str = "yml";

static ITEMS: [&'static str; 7] = [APACHE, EXAMPLE, GITIGNORE, MIT, README, TOML, YML];

pub struct Items {
    pub components: Vec<Box<dyn Decorate>>,
}

impl Items {
    pub fn new(components: Vec<Box<dyn Decorate>>) -> Self {
        Items {
            components: components,
        }
    }
}

impl Decorate for Items {
    fn decorate(&mut self, path: &String) {
        for component in self.components.as_mut_slice() {
            component.decorate(&path);
        }
    }
}

pub struct ItemsBuilder{}
impl ItemsBuilder {
    pub fn new() -> Self {
        ItemsBuilder{}
    }
}
impl Builder for ItemsBuilder {
    type Output = Box<dyn Decorate>;
    fn build(&mut self) -> Box<dyn Decorate> {
        let mut components = vec![];
         for &x in &ITEMS {
            match x {
                APACHE => {
                    let v = ApacheBuilder::new().build();
                    components.push(v);
                },

                EXAMPLE => {
                    let v = ExamplesBuilder::new().build();
                    components.push(v);
                },

                GITIGNORE => {
                    let v = GitIgnoreBuilder::new().build();
                    components.push(v);
                },

                MIT => {
                    let v = MitBuilder::new().build();
                    components.push(v);
                },

                README => {
                    let v = ReadmeBuilder::new().build();
                    components.push(v);
                },

                TOML => {
                    let v = TomlBuilder::new().build();
                    components.push(v);
                },

                YML => {
                    let v = YmlBuilder::new().build();
                    components.push(v);
                },

                _ => {
                    panic!("Error.");
                }
            }
        }

        Box::new(
            Items::new( components )
        )
    }
}


pub fn new(name: &str) {
    let _output = Command::new("cargo")
                         .arg("new")
                         .arg(&name)
                         .output()
                         .expect("Failed to execute command");
}

pub fn decorate(name: &str) {
    let path = "./".to_string() + name;

    ItemsBuilder::new().build().decorate(&path);
}