Skip to content

Child or Property decoding #28

Open
@squili

Description

Hello! I would like to parse the following document:

image "nginx" {
    reference "docker.io/library/nginx:1.23.4"
}

image "nginx" reference="docker.io/library/nginx:1.23.4"

Imagine that image could have 100 files. Or, it could have 1. Opening an entire block for just one argument seems excessive, but specifying 100 options using properties is similar. It would be nice if there was a way to decode either #knuffel(child, unwrap(argument)) or #knuffel(property) with a single derive argument. Here is my current, though admittedly fairly digusting, solution:

enum Image {
    Explicit(ExplicitImage),
    Inline(InlineImage),
}

impl<S: ErrorSpan> Decode<S> for Image {
    fn decode_node(
        node: &knuffel::ast::SpannedNode<S>,
        ctx: &mut knuffel::decode::Context<S>,
    ) -> Result<Self, knuffel::errors::DecodeError<S>> {
        if node.properties.is_empty() {
            ::knuffel::Decode::decode_node(node, ctx).map(Self::Explicit)
        } else {
            ::knuffel::Decode::decode_node(node, ctx).map(Self::Inline)
        }
    }
}

#[derive(knuffel::Decode)]
struct ExplicitImage {
    #[knuffel(argument)]
    name: String,
    #[knuffel(child, unwrap(argument))]
    reference: String,
}

#[derive(knuffel::Decode)]
struct InlineImage {
    #[knuffel(argument)]
    name: String,
    #[knuffel(property)]
    reference: String,
}

At the end of the day, we end up with two identical structs and a manual Decode implementation. That's a lot of boilerplate!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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