amazon-ion / ion-rust

Rust implementation of Amazon Ion
Apache License 2.0
147 stars 35 forks source link

`List` and `Sexp` should implement `FromIterator<Element>` #811

Open popematt opened 1 month ago

popematt commented 1 month ago

It's a little awkward to turn an iterator of Element into a List right now. You must collect as a Sequence or Vec<Element> and then convert to a List. E.g.:

fn json_to_ion(value: serde_json::Value) -> Element {
    match value {
    // ...
    Value::Array(values) => List(
            values
                .into_iter()
                .map(|v| json_to_ion(v))
                .collect::<Sequence>()
        )
        .into(),
    }
}

Since Sequence already implements FromIterator<Element>, why not Sexp and List? It would be nice to collect directly as a List or Sexp`. E.g.:

fn json_to_ion(value: serde_json::Value) -> Element {
    match value {
        // ...
        Value::Array(values) => values
            .into_iter()
            .map(|v| json_to_ion(v))
            .collect::<List>()
            .into(),
    }
}