While learning Rust and in an attempt to do something useful, I'm looking to craft a SAFE API call for DNS:Add service. The API is well defined and should by rights be a simple PUT request, the body of which is a number of strings and one variable that is limited to two options.
I've looked to define a struct and enum for the body of the put with its real limits, rather than just using strings; as I expect defining close the reality, is better practice.
I think I must be close but then have a mismatch, trying to limit input to an enum. The third argument should be only 'drive' or 'app' as RootPathVariant
.
So, I'm looking to assign the root_path
with a match of arguments=arg[2]
but atm the left-side of the match is &'static str
, which is wrong where it wants a String
.
enum is defined:
pub enum RootPathVariant { App, Drive }
impl RootPathVariant {
pub fn as_str(&self) -> &str {
match self {
&RootPathVariant::App => "app",
&RootPathVariant::Drive => "drive",
}
}
}
How should I put the left side of this match, to only accept content of that enum?
let mut root_path: RootPathVariant = RootPathVariant::Drive;
match args[2].to_string() {
"drive" => { root_path = RootPathVariant::Drive },
"app" => { root_path = RootPathVariant::App },
_ => { println!( "Error: root_path not [drive | app]"); return }
};
let put_body = AddServiceBody {
long_Name: args[0],
service_Name: args[1],
root_Path: root_path,
service_Home_Dir_Path: args[3]
};
Thanks for any help!