Nadrieril / dhall-rust

Maintainable configuration files, for Rust users
Other
303 stars 27 forks source link

Serializing record of enums: `Serialize` is not implemented for `SimpleType` #231

Closed wlcx closed 2 years ago

wlcx commented 2 years ago

First up, thanks for the project! I'm deserialising dhall into a config struct for my application and it works great. At the moment I'm "including" some of my rust enums in the dhall via .with_builtin_type.

I'd like to generate an application-specific "prelude" with these enums in so that I can use standalone dhall tooling/language server etc, but I'm struggling to find a nice way to generate a record with these enum types as values, something akin to https://prelude.dhall-lang.org/package.dhall.

I got to here (where Action and Mapping are two rust enums)

let mut m = HashMap::new();
m.insert("Action".to_string(), Action::static_type());
m.insert("Mapping".to_string(), Mapping::static_type());
println!("{}", SimpleType::Record(m));

but then realised this generates a type ({ Action : <foo>, Mapping : <bar>} rather than { Action = <foo>, Mapping = <bar> }).

So, I try serializing:

serialize(&m).to_string()?;

but get the trait `Serialize` is not implemented for `SimpleType`.

Does what I'm trying to do make any sense, or is my best bet going to be constructing the dhall string myself?

        println!(
            "{{ Action = {}, Mapping = {} }}",
            Action::static_type(),
            Mapping::static_type()
        );
Nadrieril commented 2 years ago

Hey, glad you're enjoying the project! At the moment serialize only works with serde-compatible values, and dhall types don't fit in there. I should be able to make it work with SimpleTypes though; I'll try when I come back home in two weeks

Nadrieril commented 2 years ago

Hey, turns out I had misread your issue. I made serialize work on SimpleType but that's not actually your issue. You want a record that contains types, which doesn't fit into SimpleValue/SimpleType. I've voluntarily not exposed more advanced dhall constructs than that because I don't know how to make a sane API that won't break all the time. I'm afraid the easiest for you would indeed be to construct the strings by hand :(

wlcx commented 2 years ago

Ah, ok. Thanks for looking and sorry for the misleading issue title!