ron icon indicating copy to clipboard operation
ron copied to clipboard

make value roundtrip

Open wiiznokes opened this issue 5 months ago • 4 comments

previous pr: #553

todo:

  • [ ] add more test cases (currently, only Enum::A pass)
  • [ ] handle all struct type in handle_any_struct
  • [ ] finish implementing visit_enum
  • [ ] impl Serialize for Value
  • [ ] clean up code
  • [ ] I've included my change in CHANGELOG.md

wiiznokes avatar Jul 25 '25 01:07 wiiznokes

honestly, after having passed way to much time on this, i think its just not possible. We just need to attach a name to Tuple and Map. To create a Value, we need a Visitor. The only method capable of this in Visitor is visit_enum.

But,

  • the visitor must be generic. It doesn't know in advance the type of the enum.
  • the VariantAccess trait have
    fn unit_variant(self) -> Result<(), Self::Error>;
    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
    fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
    fn struct_variant<V>(
          self,
          fields: &'static [&'static str],
          visitor: V,
       ) -> Result<V::Value, Self::Error>
    
    all this method take self, so we can't all call them to know what is the type of the enum.

So we are cooked. I wonder if a language succeed to do this. Maybe i can be proven wrong.

I think we just need another method on VariantAccess:

fn enum_type(&self) -> Option<EnumType>;
enum EnumType {
    Unit,
    NewType,
    Tuple,
    Struct
}

wiiznokes avatar Jul 25 '25 22:07 wiiznokes

https://github.com/serde-rs/serde/issues/2874

wiiznokes avatar Jul 25 '25 22:07 wiiznokes

Thank you for exploring it further @wiiznokes! I personally think that many improvements are still blocked on serde, perhaps we can come back to this once ron is given more agency in deserialising data without type information

juntyr avatar Jul 26 '25 05:07 juntyr

Another solution could be to not implement Deserialize for Value. I don't think it's very used in its current shape anyway since it doesn't store enum

wiiznokes avatar Jul 26 '25 16:07 wiiznokes