-3

I can filter the active classes as follows, but how can I achieve bringing all the classes where their status is not Online? Status for not active can be Cancelled, Deferred, or Optional.

pub enum Status {
    Online,
    Cancelled,
    Deferred,
    Optional,
}

pub struct CustomFilter {
    pub status: Option<Status>,
}

fn example() {
    let mut custom_filter = CustomFilter::default();
    custom_filter.status = Some(Status::Online);
    let online_classes = self.get_classes_with_filter(custom_filter).await?;

    // where Status -> Cancelled, Deferred, Optional
    let non_active_classes = todo!("??");
}
casillas
  • 16,351
  • 19
  • 115
  • 215
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Nov 24 '20 at 20:29
  • I have updated my question, would you mind check it again. – casillas Nov 24 '20 at 20:30
  • It still does not contain a [MRE]. Please check my comment again for instructions on how to provide one. – Shepmaster Nov 24 '20 at 20:31
  • 1
    Your question might be answered by the answers of [How do I conditionally check if an enum is one variant or another?](https://stackoverflow.com/q/51429501/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 24 '20 at 20:32
  • And to double-check basics, you know about the `!=` operator, yeah? – Shepmaster Nov 24 '20 at 20:35
  • can I write as `Some(Status::!=Active)` ? It is not allowed. – casillas Nov 24 '20 at 20:50
  • No. Once you provide a [MRE], I'm sure you'll get useful answers. – Shepmaster Nov 24 '20 at 20:52
  • I have added `Status` Enum in the question as well, is that enough? – casillas Nov 24 '20 at 20:56
  • what is `Filter`? is it [`std::iter::Filter`](https://doc.rust-lang.org/std/iter/struct.Filter.html)? doesn't look like it. are you looking for a solution that uses this class? – kmdreko Nov 24 '20 at 21:00
  • I have just added `CustomFilter` struct as well. Updated question accrodingly – casillas Nov 24 '20 at 21:01
  • ``cannot find derive macro `Serialize` in this scope``; ``error: cannot find derive macro `Deserialize` in this scope``; ``error[E0424]: expected value, found module self``; ``error[E0728]: `await` is only allowed inside `async` functions and blocks``; ``error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `Try`)`` – Shepmaster Nov 24 '20 at 21:13
  • It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org), then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. – Shepmaster Nov 24 '20 at 21:13
  • Your question might be answered by the answers of [How to filter a vector of custom structs in Rust?](https://stackoverflow.com/q/44662312/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 24 '20 at 21:15

2 Answers2

0

How about something along the lines of:

let foo: Vec<_> = whatever;

let active_classes: Vec<_> = foo.iter().filter(|x| x.status == Status::Active).collect();
let non_active_classes: Vec<_> = foo.iter().filter(|x| x.status != Status::Active).collect();

Not sure if this is what you intended.

Luke
  • 565
  • 5
  • 19
0

If your question is "Can I store !Active or Cancelled | Deferred | Optional in an Status?" then the answer is no. Maybe if your enum worked like bitflags but that doesn't appear to be the case here.

You'd have to store them individually in a Vec or something more complicated like:

enum StatusFilter {
    Include(Vec<Status>),
    Exclude(Vec<Status>),
}

pub struct CustomFilter {
    pub status: StatusFilter,
}
custom_filter.status = StatusFilter::Exclude(vec![Status::Active]);

I'd recommend not doing this and use functor-based filtering like @Luke has suggested if at all possible. Its much more straightforward, more flexible to use and implement, and it follows conventions in the standard library.

kmdreko
  • 42,554
  • 6
  • 57
  • 106