1

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!

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • You can use `string.as_ref()` to get `&str` from a `String` and then use it in a match. But what is the type of `args`? `to_string()` may be unnecessary if you already have `&str`. – Pavel Strakhov Oct 23 '16 at 02:10
  • 1
    Please add the exact error message and a [MCVE]. Then you are more likely to get any answers :) – Lukas Kalbertodt Oct 23 '16 at 07:43
  • @Pavel Strakhov the type of args follows simple from Cargo:getopts as a Vec. I missed the obvious option to `match args[2].as_str()` which appears to work! – David Brown Oct 23 '16 at 09:45
  • I'm seconding @LukasKalbertodt's point about an MCVE. For example, this question has *nothing* to do with an enum. I'd bet that [your code looks like this](http://play.integer32.com/?gist=2707ee26ba9429582a72fbed55b7a9e5&version=stable). – Shepmaster Oct 23 '16 at 13:15

1 Answers1

1

Noting the answer looks to be to match args[2].as_str()!