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
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate reqwest;
extern crate git2;
extern crate tempdir;

use tempdir::TempDir;
use git2::Repository;

use std::io::{self, Read, Write};
use std::path::Path;
use std::fs::{self, File};

pub static PACKAGE_FILENAME: &'static str = "undule.json";

#[derive(Debug)]
pub enum InitError {
    /// Couldn't initialize new undule because `undule.json` already existed.
    AlreadyExists,

    /// Couldn't create `undule.json`
    FailedToCreate,
}

#[derive(Debug)]
pub enum AddError {
}

#[derive(Debug)]
pub enum RemoveError {
}

#[derive(Debug)]
pub enum InstallError {
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Package {
    name: String,
}

impl Package {
    pub fn new() -> Self {
        Package {
            name: "<package name>".to_string(),
        }
    }
}

impl Default for Package {
    fn default() -> Self {
        Package {
            name: "".to_string(),
        }
    }
}

pub fn init<P: AsRef<Path>>(dir: P) -> Result<(), InitError> {
    let dir = dir.as_ref();
    let package_path = dir.join(PACKAGE_FILENAME);

    match fs::metadata(&package_path) {
        Ok(_) => return Err(InitError::AlreadyExists),
        Err(_) => {},
    }

    let mut file = match File::create(&package_path) {
        Ok(f) => f,
        Err(_) => return Err(InitError::FailedToCreate),
    };

    let package = Package::new();
    let serialized = serde_json::to_string_pretty(&package).unwrap();

    file.write(serialized.as_bytes()).unwrap();

    Ok(())
}

pub fn add<T: AsRef<str>>(specifier: T) -> Result<(), AddError> {
    let specifier = specifier.as_ref();

    if let Ok(dir) = TempDir::new("undulate") {
        let _repo = match Repository::clone(specifier, dir.path()) {
            Ok(repo) => repo,
            Err(e) => panic!("Failed to clone: {}", e),
        };
    }

    Ok(())
}

pub fn remove<T: AsRef<str>>(specifier: T) -> Result<(), RemoveError> {
    Ok(())
}

pub fn install(url: &str) -> Result<(), InstallError> {
    Ok(())
}