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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
pub mod pandora {
use std::{
collections::HashMap,
fs::{self, create_dir, remove_dir_all, File},
io::Write,
path::Path,
process::{exit, Command, ExitCode},
};
pub const EXIT_SUCCESS: i32 = 0;
pub const EXIT_FAILURE: i32 = 1;
pub struct Helpful {}
impl Helpful {
pub fn new() -> Helpful {
Self {}
}
///
/// # Get a shell instance
///
/// - `program` The program to use
///
pub fn shell(self, program: &str) -> Command {
Command::new(program)
}
///
/// # Get a shell instance
///
/// - `program` The program to use
///
pub fn open(self, filename: &str) -> File {
File::open(filename).expect("failed to open filename")
}
///
/// # Print a message with new line
///
/// - `message` The message to print
///
pub fn echo(self, message: &str) -> Helpful {
println!("{}", message);
self
}
///
/// # Check if a directory exist
///
/// - `path` The directory to check
///
///
pub fn directory_exist(self, directory: &str) -> bool {
Path::new(directory).is_dir()
}
///
/// # Check if a path is a symlink path
///
/// - `path` The path to check
///
///
pub fn symlink_exist(self, filename: &str) -> bool {
Path::new(filename).is_symlink()
}
///
/// # Check if a path is a ralative path
///
/// - `path` The path to check
///
///
pub fn is_relative(self, path: &str) -> bool {
Path::new(path).is_relative()
}
///
/// # Check if a path is an absolute path
///
/// - `path` The path to check
///
///
pub fn is_absolute(&mut self, path: &str) -> bool {
Path::new(path).is_absolute()
}
///
/// # Check if a file exist
///
/// - `filename` The filename to check
///
///
pub fn file_exist(self, filename: &str) -> bool {
Path::new(filename).is_file()
}
///
/// # Print a message without new line
///
/// - `message` The message to print
///
pub fn put(self, message: &str) -> Helpful {
print!("{}", message);
self
}
}
impl Default for Helpful {
fn default() -> Self {
Self::new()
}
}
///
/// # To create or test program
///
pub struct Program {
program: String,
description: String,
doc: String,
args: Vec<String>,
helper: Helpful,
}
impl Program {
///
/// # Program constructor
///
/// - `name` The program name
/// - `description` The program description
/// - `docs` The program documentation
///
pub fn new(
name: &str,
description: &str,
docs: HashMap<&str, &str>,
arguments: Vec<String>,
) -> Program {
let mut r = Self {
program: name.to_string(),
description: description.to_string(),
doc: String::new(),
args: arguments,
helper: Helpful::default(),
};
for (&k, &v) in docs.iter() {
r.doc.push_str(format!("{} : {}\n", k, v).as_str());
}
r
}
///
/// # Program help
///
/// Display the program help
///
pub fn help(self) -> Result<String, String> {
println!("{}", self.doc);
self.ok()
}
///
/// # Exit the program
///
/// - `code` The exit code
///
///
pub fn quit(self, code: i32) -> ExitCode {
exit(code);
}
///
/// # Check if args contains a value
///
/// - `expected` The value to search
///
///
///
pub fn has(self, expected: &str) -> bool {
self.args.contains(&expected.to_string())
}
///
/// # Get arguments
///
pub fn args(self) -> Vec<String> {
self.args
}
///
/// Check if a param matche a pattern
///
/// - `arg` The arg to check
/// - `x` The expected value
///
///
pub fn matches(self, arg: &String, x: &str) -> bool {
arg.eq(x)
}
///
/// # Create a directory
///
/// - `d` The directory to create
///
pub fn mkdir(self, d: &str) -> bool {
if self.helper.directory_exist(d) {
return false;
}
create_dir(d).expect("Failed to create the directory");
true
}
///
/// # Remove a directory
///
/// - `d` The directory to remove
///
pub fn rmdir(self, d: &str) -> bool {
if self.helper.directory_exist(d) {
remove_dir_all(d).expect("Failed to remove the directory");
return true;
}
false
}
///
/// # Create a file
///
/// - `file` The filename to create
///
pub fn touch(self, file: &str) -> bool {
if self.helper.file_exist(file) {
return false;
}
std::fs::File::create(file).expect("failed to create the file");
true
}
///
/// # Remove a file
///
/// - `file` The filename to remove
///
pub fn rm(self, file: &str) -> bool {
if self.helper.file_exist(file) {
fs::remove_file(file).expect("failed to remove the file");
return true;
}
false
}
///
/// # Rename a file
///
/// - `file` The filename to remove
/// - `x` The new filename
///
pub fn rename(self, file: &str, x: &str) -> bool {
if Path::new(file).is_file() && !Path::new(x).is_file() {
std::fs::rename(file, x).expect("Failed to rename filename");
return true;
}
false
}
///
/// # Create a file with a content
///
/// - `file` The filename to create
/// - `content` The file content
///
pub fn touch_with_content(self, file: &str, content: &str) -> bool {
if Path::new(file).is_file() {
return false;
}
let mut f = std::fs::File::create(file).expect("failed to create the file");
f.write_all(content.as_bytes())
.expect("Failed to write fiel content");
true
}
///
/// # Call the a or b method in function to the boolean
///
/// - `boolean` The condition
/// - `a` The first callback
/// - `b` The second callback
///
pub fn call(
self,
boolean: bool,
a: fn(Program) -> Result<String, String>,
b: fn(Program) -> Result<String, String>,
) -> Result<String, String> {
if boolean {
a(self)
} else {
b(self)
}
}
///
/// # Call the a or b method in function to the boolean
///
/// - `boolean` The condition
/// - `to` The callback
///
pub fn run(self, callback: fn() -> Result<String, String>) -> Result<String, String> {
callback()
}
pub fn no_args(self) -> bool {
self.args.is_empty()
}
pub fn get(self, index: usize) -> Option<String> {
let x: bool = self.args.get(index).is_none();
if x {
None
} else {
Some(x.to_string())
}
}
///
/// # Return a ok Result
///
pub fn ok(self) -> Result<String, String> {
Ok(String::from("success"))
}
///
/// # Return a Err Result
///
pub fn ko(self) -> Result<String, String> {
Err(String::from("failure"))
}
///
/// # Get the program description
///
pub fn get_description(self) -> String {
self.description
}
///
/// # Get the program name
///
pub fn get_program(self) -> String {
self.program
}
///
/// # Call the main function of the program
///
/// - `callback` The main function
///
pub fn go(self, callback: fn(Program) -> Result<String, String>) -> Result<String, String> {
callback(self)
}
}
}
#[cfg(test)]
mod tests {
use super::pandora::Program;
use std::{collections::HashMap, vec};
fn init_project(p: Program) -> Result<String, String> {
p.ok()
}
fn main(p: Program) -> Result<String, String> {
init_project(p)
}
#[test]
pub fn ok() {
let mut doc = HashMap::new();
doc.insert("zuu", "Run all test based on zuu.yml");
doc.insert("zuu init", "Initialyse a new repository");
let program: Program = Program::new("zuu", "A toolkit program", doc, vec![]);
assert!(program.go(main).is_ok());
}
#[test]
pub fn program() {
let mut doc: HashMap<&str, &str> = HashMap::new();
doc.insert("zuu", "Run all test based on zuu.yml");
doc.insert("zuu init", "Initialyse a new repository");
let program: Program = Program::new("zuu", "A toolkit program", doc, vec![]);
assert!(program.go(main).is_ok());
}
}