#[derive(Serialize)] #[derive(Deserialize)] #[derive(Default)] #[derive(Debug)] pub struct Application { pub name: String, // "type" is a reversed keyword, so rename it during JSON parsing #[serde(rename(serialize = "app_type", deserialize = "type"))] pub app_type: String, pub os: String, pub architecture: String, pub executable: String, pub dependencies: Vec, pub parameters: Vec, pub use_service_level: u8, pub needs_gui: u8, pub autostart: u8, } #[derive(Serialize)] #[derive(Deserialize)] #[derive(Default)] #[derive(Debug)] pub struct Dependency { pub name: String, pub os: String, pub architecture: String, } pub struct Config { // fqdn of the destination server pub fqdn: String, // absolute or relative path to a file to upload pub filepath: String, // path to JWT granting access to the server at fqdn pub token_path: String, // application or dependency pub object_kind: String, // One of these is filled during Config creation while the other remains // initialized to default values pub dependency: Dependency, pub application: Application, } impl Config { pub fn new(args: &[String]) -> Result { if args.len() < 5 { return Err("not enough arguments"); } match &*args[1] { "foobar" => (), "foobar2" => (), "foobar3" => (), "foobar4" => (), _ => { return Err("Invalid FQDN"); } } match &*args[4] { "dependency" => (), "application" => (), _ => { return Err("Invalid object kind. Expecting application or dependency."); } } let json_data = match fs::read_to_string(&args[5]) { Ok(s) => s, Err(e) => { println!("Could not read file: {}, {}", args[5], e); return Err("Failed reading json file containing upload data"); } }; let fqdn = args[1].clone(); let token_path = args[2].clone(); let filepath = args[3].clone(); let object_kind = args[4].clone(); let mut application = Application { ..Default::default() }; let mut dependency = Dependency { ..Default::default() }; if object_kind == "application" { application = match serde_json::from_str(&json_data) { Ok(d) => d, Err(e) => { println!("JSON parse error: {}", e); return Err("application json file is invalid"); } }; } else { dependency = match serde_json::from_str(&json_data) { Ok(d) => d, Err(e) => { println!("JSON parse error: {}", e); return Err("dependency json file is invalid"); } }; } Ok(Config { fqdn, filepath, token_path, object_kind, dependency, application, }) } }