Determine geometry field at build time
#geoserde #rust
https://github.com/p4ken/geoserde/issues/9
big picture
https://gyazo.com/32f082df6cd28a2401fe34cdb055aed0
table: decisions
o Data format selects whether to process properties or geometry first
o Nested / child structs are allowed
? Struct fields can be in a different order from actual format
? Compatible with all serde_derive attributes
code:plan0(rs)
#derive(Serialize)
struct MyFeature {
title: String,
location: Point,
}
pros
no additional code needed
easy to learn
cons
dynamic geometry detection is slow
hard to implement various geometry types
code:plan1(rs)
use geoserde::geometry;
#derive(Serialize)
struct MyFeature {
title: String,
#serde(with = "geometry")
location: Point,
}
cons
missing geometry => runtime error
hard to implement
how to tell "this is the geometry" to Serializer?
code: plan1-a(rs)
#derive(Serialize)
struct MyFeature {
title: String,
#serde(rename = "geometry")
location: Point,
}
pros
minimum aditional code
cons
can't contain "geometry" field as an property
missing geometry => runtime error
code:plan2(rs)
use geoserde::Feature;
#derive(Serialize, Feature)
struct MyFeature {
title: String,
#geometry
location: Point,
}
pros
missing geometry => build error
cons
hard to implement
geometry in child struct
code:plan2a(rs)
use geoserde::Feature;
#derive(Serialize, Feature)
struct MyFeature {
title: String,
#geoserde(geometry)
location: Point,
}
code:api2(rs)
pub trait Feature {
fn serialize_geometry(ser);
fn serialize_properties(ser);
}
code:api2(rs)
pub trait Feature {
fn geometry(&self) -> impl Serialize + '_;
fn properties(&self) -> impl Serialize + '_;
}
code:plan3.rs
#geoserde
#derive(Serialize)
struct MyFeature {
title: String,
#geoserde(geometry)
location: Point,
}