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
use core::fmt;
use std::{error::Error, fs::write, path::PathBuf};
mod constants;
mod dependencies;
mod graph;
mod managers;
mod project;
mod utils;
mod workspace;
use serde::Serialize;
pub use {
    cli::{
        perform_add, perform_init, perform_install_isolated, perform_remove,
        perform_workspace_add, perform_workspace_remove, utils_get_dependencies,
    },
    utils::{get_all_project_names, get_projects_with_config_path},
};
mod cli;

#[macro_use]
extern crate log;

#[derive(Debug)]
pub struct LibraryError(String);

impl fmt::Display for LibraryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Library error {:?}", self.0)
    }
}

impl From<LibraryError> for String {
    fn from(err: LibraryError) -> Self {
        format!("Encountered an unrecoverable error: {}", err.0)
    }
}

impl Error for LibraryError {}

pub trait Command {
    fn execute(&self);
}

/// Used to add a required dependency to a project or workspace
pub trait AddEsteemRequiredDependency {
    fn add_required_dependency(&mut self, dependency: String);
}

/// Used to add a development dependency to a project or workspace
pub trait AddEsteemDevelopmentDependency {
    fn add_development_dependency(&mut self, dependency: String);
}

/// Used to remove a required dependency to a project or workspace
pub trait RemoveEsteemRequiredDependency {
    fn remove_required_dependency(
        &mut self,
        dependency: String,
    ) -> Result<(), LibraryError>;
}

/// Used to remove a development dependency to a project or workspace
pub trait RemoveEsteemDevelopmentDependency {
    fn remove_development_dependency(
        &mut self,
        dependency: String,
    ) -> Result<(), LibraryError>;
}

/// Used to write dependencies to a file
pub trait WriteDependencies
where
    Self: Serialize,
{
    fn get_path(&self) -> PathBuf;

    fn write_dependencies(&self) {
        info!("Writing new dependencies to {:?}", self.get_path());
        let to_write = serde_json::to_string_pretty(self).unwrap();
        write(self.get_path(), to_write).unwrap();
    }
}