use plotly_derive::FieldSetter;
use serde::Serialize;
use crate::color::Color;
use crate::layout::{Axis, Center, Projection};
#[derive(Serialize, Clone, Debug, FieldSetter)]
pub struct LayoutGeo {
center: Option<Center>,
zoom: Option<u8>,
#[field_setter(default = "Projection::new().projection_type(ProjectionType::Orthographic)")]
projection: Option<Projection>,
#[field_setter(default = "Some(true)")]
showocean: Option<bool>,
#[field_setter(default = "'rgb(0, 255, 255)'")]
oceancolor: Option<Box<dyn Color>>,
showland: Option<bool>,
landcolor: Option<Box<dyn Color>>,
showlakes: Option<bool>,
lakecolor: Option<Box<dyn Color>>,
showcountries: Option<bool>,
lonaxis: Option<Axis>,
lataxis: Option<Axis>,
#[field_setter(default = "Some(1)")]
coastlinewidth: Option<u8>,
}
impl LayoutGeo {
pub fn new() -> Self {
Default::default()
}
}
#[cfg(test)]
mod tests {
use serde_json::{json, to_value};
use super::*;
use crate::color::Rgb;
use crate::layout::{Axis, Center, Projection, ProjectionType, Rotation};
#[test]
fn serialize_layout_geo() {
let geo = LayoutGeo::new()
.center(Center::new(10.0, 20.0))
.zoom(5)
.projection(
Projection::new()
.projection_type(ProjectionType::Mercator)
.rotation(Rotation::new().lat(1.0).lon(2.0).roll(4.0)),
)
.showocean(true)
.oceancolor(Rgb::new(0, 255, 255))
.showland(true)
.landcolor(Rgb::new(100, 200, 100))
.showlakes(false)
.lakecolor(Rgb::new(50, 50, 200))
.showcountries(true)
.lonaxis(Axis::new().title("Longitude"))
.lataxis(Axis::new().title("Latitude"))
.coastlinewidth(2);
let expected = json!({
"center": {"lat": 10.0, "lon": 20.0},
"zoom": 5,
"projection": {"type": "mercator", "rotation": {"lat": 1.0, "lon": 2.0, "roll": 4.0}},
"showocean": true,
"oceancolor": "rgb(0, 255, 255)",
"showland": true,
"landcolor": "rgb(100, 200, 100)",
"showlakes": false,
"lakecolor": "rgb(50, 50, 200)",
"showcountries": true,
"lataxis": { "title": { "text": "Latitude" } },
"lonaxis": { "title": { "text": "Longitude" } },
"coastlinewidth": 2
});
assert_eq!(to_value(geo).unwrap(), expected);
}
}