Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "random panic" in DecodeScalar #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

the-eater
Copy link

Currently you can force a panic while decoding a Value wrapped in a container because all containers delegate decode to the default implementation, while Value only accepts calling decode directly,

This PR makes all containers call ::decode of their child type instead

code proof
use std::rc::Rc;
use knuffel::ast::Value;
use knuffel::span::Span;

fn main() {
    #[derive(Debug, knuffel::Decode)]
    #[knuffel(span_type = Span)]
    struct X {
        #[knuffel(property)]
        d: Rc<Value<Span>>,
    }

    #[derive(Debug, knuffel::Decode)]
    #[knuffel(span_type = Span)]
    struct Document {
        #[knuffel(child)]
        gamer: X,
    }

    let doc = "gamer d=(uwu)\":3\"";

    let parsed: Document = knuffel::parse("<inline>", doc).unwrap();
    println!("{:?}", parsed);
}

I would also like to note that the trait for DecodeScalar seems, confusing?

it's advised not to implement ::decode by hand, however there's no access to the type name in ::raw_decode, requiring weird workarounds to implement a polymorphic scalar while also following said advice

I e.g. currently want to support the following:

match "literal-match" 
match (re)"regex\smatch"

this either requires storing a MyScalarType enum in the context, or not following given advice lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant