Skip to content

Vec<T> types should support meta attribute filter #3

@zexa

Description

@zexa
#![derive(de_hypertext::Deserialize)
struct Target {
    #[de_hypertext(selector = "div")]
    items: Vec<Listing>
}

#![derive(de_hypertext::Deserialize)
struct Listing {
    #[de_hypertext(selector = "li")]
    name: String,
}

This code will crash if at least one listing does not contain the element li when trying to parse Target.

The current solution is to make these Option and then filter out the unnecessary listings

#![derive(de_hypertext::Deserialize)
struct Target {
    #[de_hypertext(selector = "div")]
    items: Vec<Listing>
}

#![derive(de_hypertext::Deserialize)
struct Listing {
    #[de_hypertext(selector = "li")]
    name: Option<String>,
}

fn post_process(target: Target) -> Target {
    Target {
        items: target
            .items
            .into_iter()
            .filter_map(|listing| {
                if listing.name.is_some() {
                    Some(listing)
                } else {
                    None
                }
            })
            .collect(),
    }
}

However, this is tedious. Instead, we could edit the generated code to ignore the ones that failed and return everything else if we added an attribute such as

#![derive(de_hypertext::Deserialize)
struct Target {
    #[de_hypertext(selector = "div", filter)]
    items: Vec<Listing>
}

#![derive(de_hypertext::Deserialize)
struct Listing {
    #[de_hypertext(selector = "li")]
    name: String,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions