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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use log::error;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use thiserror::Error;
use crate::{
    folders::{FlatFolderGraph, Item},
    ProjectMetadata, SerializedData,
};
use yy_typings::ViewPath;

#[derive(Debug, Serialize, Deserialize)]
#[must_use = "this `Output` must be printed"]
#[serde(tag = "type")]
#[allow(clippy::large_enum_variant)]
pub enum Output {
    Startup(Startup),
    Command(CommandOutput),
    Shutdown(Shutdown),
}

impl Output {
    pub fn print(self) {
        let output = serde_json::to_string_pretty(&self).unwrap();
        println!("{}", output);
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Startup {
    pub success: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_metadata: Option<ProjectMetadata>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CommandOutput {
    pub success: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<YypBossError>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub exists: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub name_is_valid: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource: Option<SerializedData>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub associated_data: Option<SerializedData>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub flat_folder_graph: Option<FlatFolderGraph>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub path_kind: Option<Item>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_folder: Option<ViewPath>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_names: Option<Vec<(String, String)>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub requested_path: Option<PathBuf>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_metadata: Option<ProjectMetadata>,
}

impl CommandOutput {
    pub fn error(yyp_boss_error: YypBossError) -> Self {
        Self {
            success: false,
            error: Some(yyp_boss_error),
            ..Self::default()
        }
    }

    pub fn ok() -> Self {
        Self {
            success: true,
            ..Self::default()
        }
    }

    pub fn ok_metadata(pd: ProjectMetadata) -> Self {
        Self {
            success: true,
            project_metadata: Some(pd),
            ..Self::default()
        }
    }

    pub fn ok_datum(resource: SerializedData, associated_data: Option<SerializedData>) -> Self {
        Self {
            success: true,
            resource: Some(resource),
            associated_data,
            ..Self::default()
        }
    }

    pub fn ok_resource(resource: SerializedData) -> Self {
        Self {
            success: true,
            resource: Some(resource),
            ..Self::default()
        }
    }

    pub fn ok_associated_data(associated_data: SerializedData) -> Self {
        Self {
            success: true,
            associated_data: Some(associated_data),
            ..Self::default()
        }
    }

    pub fn ok_exists(exists: bool) -> Self {
        Self {
            success: true,
            exists: Some(exists),
            ..Self::default()
        }
    }

    pub fn ok_name_is_valid(valid: bool) -> Self {
        Self {
            success: true,
            name_is_valid: Some(valid),
            ..Self::default()
        }
    }

    pub fn ok_folder_graph(ff_graph: FlatFolderGraph) -> Self {
        Self {
            success: true,
            flat_folder_graph: Some(ff_graph),
            ..Self::default()
        }
    }

    pub fn ok_path_kind(item: Item) -> Self {
        Self {
            success: true,
            path_kind: Some(item),
            ..Self::default()
        }
    }

    pub fn ok_created_folder(f: ViewPath) -> Self {
        Self {
            success: true,
            created_folder: Some(f),
            ..Self::default()
        }
    }

    pub fn ok_event_names(ev: Vec<(String, String)>) -> Self {
        Self {
            success: true,
            event_names: Some(ev),
            ..Self::default()
        }
    }

    pub fn ok_path(path: PathBuf) -> Self {
        Self {
            success: true,
            requested_path: Some(path),
            ..Self::default()
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Shutdown {
    pub msg: String,
}

#[derive(Debug, Error, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum YypBossError {
    #[error("could not read Command, error: {}", .data)]
    CouldNotReadCommand { data: String },

    #[error("error manipulating resource, error: {}", .data)]
    ResourceManipulation { data: String },

    #[error("folder graph error, error: {}", .data)]
    FolderGraphError { data: String },

    #[error("could not read yyfile, error: {}", .data)]
    YyParseError { data: String },

    #[error("could not read associated data, error: {}", .data)]
    AssociatedDataParseError { data: String },

    #[error("could not output data -- operation was SUCCESFUL, but data could not be returned because {}", .data)]
    CouldNotOutputData { data: String },

    #[error("could not serialize yypboss...coarse error {}", .data)]
    CouldNotSerializeYypBoss { data: String },

    #[error("internal error -- command could not be executed. error is fatal: {}", .fatal)]
    InternalError { fatal: bool },
}