use crate::arg::{Arg, Obj};
use crate::graph::{Graph, Result};
#[derive(Clone, Debug, PartialEq)]
pub struct Features(pub(crate) Arg);
#[derive(Clone, Debug, PartialEq)]
pub struct Area(pub(crate) Arg);
#[derive(Clone, Debug, PartialEq)]
pub struct Lines(pub(crate) Arg);
macro_rules! handle {
($t:ident) => {
impl From<&$t> for Arg {
fn from(h: &$t) -> Arg {
h.0.clone()
}
}
impl From<$t> for Arg {
fn from(h: $t) -> Arg {
h.0
}
}
impl $t {
pub fn arg(&self) -> &Arg {
&self.0
}
}
};
}
handle!(Features);
handle!(Area);
handle!(Lines);
impl Features {
fn of(a: Arg) -> Features {
Features(a)
}
}
impl Area {
fn of(a: Arg) -> Area {
Area(a)
}
}
impl Lines {
fn of(a: Arg) -> Lines {
Lines(a)
}
}
#[derive(Clone, Debug)]
pub struct Roads {
pub area: Area,
pub outlines: Lines,
pub details: Lines,
pub trails: Lines,
pub centerlines: Lines,
pub seams: Lines,
pub rails: Lines,
}
#[derive(Clone, Debug)]
pub struct Module(pub(crate) Arg);
fn list<T: Into<Arg> + Clone>(items: &[T]) -> Arg {
Arg::List(items.iter().cloned().map(Into::into).collect())
}
fn many<T>(items: &[&T]) -> Vec<Arg>
where
for<'a> &'a T: Into<Arg>,
{
items.iter().map(|h| (*h).into()).collect()
}
pub struct FeaturesApi<'g>(&'g mut Graph);
pub struct AreasApi<'g>(&'g mut Graph);
pub struct LinesApi<'g>(&'g mut Graph);
pub struct FillsApi<'g>(&'g mut Graph);
pub struct ModulesApi<'g>(&'g mut Graph);
pub struct ComposeApi<'g>(&'g mut Graph);
pub struct PageApi<'g>(&'g mut Graph);
pub struct WorldApi<'g>(&'g mut Graph);
impl Graph {
pub fn features(&mut self) -> FeaturesApi<'_> {
FeaturesApi(self)
}
pub fn areas(&mut self) -> AreasApi<'_> {
AreasApi(self)
}
pub fn lines(&mut self) -> LinesApi<'_> {
LinesApi(self)
}
pub fn fills(&mut self) -> FillsApi<'_> {
FillsApi(self)
}
pub fn modules(&mut self) -> ModulesApi<'_> {
ModulesApi(self)
}
pub fn compose(&mut self) -> ComposeApi<'_> {
ComposeApi(self)
}
pub fn page(&mut self) -> PageApi<'_> {
PageApi(self)
}
pub fn world(&mut self) -> WorldApi<'_> {
WorldApi(self)
}
pub fn output(&mut self, spec: Obj) -> Result<()> {
self.call("output", &[Arg::Map(spec)]).map(|_| ())
}
pub fn output_remap(&mut self, mut spec: Obj, remaps: &mut [&mut dyn FnMut(&mut Graph, [f64; 2]) -> Result<[f64; 2]>]) -> Result<()> {
let warps: Vec<Arg> = match spec.get("warp") {
None | Some(Arg::Null) => vec![],
Some(Arg::List(l)) => l.clone(),
Some(w) => vec![w.clone()],
};
let marked: Vec<Arg> = warps
.iter()
.map(|w| match w {
Arg::Map(o) if o.has("remap") => {
let mut o = o.clone();
o.set("remap", Arg::Func);
Arg::Map(o)
}
other => other.clone(),
})
.collect();
spec.set("warp", Arg::List(marked.clone()));
if !self.output_begin(&spec)? {
return Ok(());
}
let mut done = Vec::new();
let mut next = 0;
for (n, w) in marked.iter().enumerate() {
match w {
Arg::Map(o) if o.has("remap") => {
self.output_warps(&done)?;
let (columns, rows, points) = self.remap_grid(o, n)?;
let f = remaps.get_mut(next).ok_or_else(|| self.error(format!("output.warp[{n}]: no function given for this remap")))?;
next += 1;
let mut went = Vec::with_capacity(points.len());
for p in &points {
let [x, y] = f(self, *p)?;
went.push(Arg::List(vec![Arg::Num(x), Arg::Num(y)]));
}
done.push(self.remap_mesh(n, columns, rows, &points, &went)?);
}
other => done.push(other.clone()),
}
}
self.output_warps(&done)
}
pub fn title(&mut self, text: &str) -> Result<()> {
self.call("title", &[Arg::str(text)]).map(|_| ())
}
pub fn draw(&mut self, spec: Obj) -> Result<String> {
match self.call("draw", &[Arg::Map(spec)])? {
Arg::Str(id) => Ok(id),
_ => Ok(String::new()),
}
}
pub fn view(&mut self, spec: Obj, style: impl FnOnce(&mut Graph) -> Result<()>) -> Result<String> {
self.view_begin(&Arg::Map(spec))?;
let built = style(self);
let id = self.view_end(built.is_ok())?;
built?;
Ok(id.as_str().unwrap_or_default().to_string())
}
}
impl FeaturesApi<'_> {
pub fn tiles(self, items: &[&str]) -> Result<Features> {
self.0.call("features.tiles", &[list(items)]).map(Features::of)
}
pub fn osm(self, tags: Obj) -> Result<Features> {
self.0.call("features.osm", &[Arg::Map(tags)]).map(Features::of)
}
pub fn geojson(self, source: impl Into<Arg>) -> Result<Features> {
self.0.call("features.geojson", &[source.into()]).map(Features::of)
}
pub fn json(self, url: &str, paths: Obj) -> Result<Features> {
self.0.call("features.json", &[Arg::str(url), Arg::Map(paths)]).map(Features::of)
}
pub fn contours(self, options: Obj) -> Result<Features> {
self.0.call("features.contours", &[Arg::Map(options)]).map(Features::of)
}
pub fn soundings(self, options: Obj) -> Result<Features> {
self.0.call("features.soundings", &[Arg::Map(options)]).map(Features::of)
}
pub fn filter(self, of: &Features, tags: Obj) -> Result<Features> {
self.0.call("features.where", &[of.into(), Arg::Map(tags)]).map(Features::of)
}
}
impl AreasApi<'_> {
pub fn polygons(self, of: &Features) -> Result<Area> {
self.0.call("areas.polygons", &[of.into()]).map(Area::of)
}
pub fn union(self, areas: &[&Area]) -> Result<Area> {
self.0.call("areas.union", &many(areas)).map(Area::of)
}
pub fn difference(self, base: &Area, cut: &[&Area]) -> Result<Area> {
let mut args = vec![base.into()];
args.extend(many(cut));
self.0.call("areas.difference", &args).map(Area::of)
}
pub fn intersect(self, base: &Area, keep: &Area) -> Result<Area> {
self.0.call("areas.intersect", &[base.into(), keep.into()]).map(Area::of)
}
pub fn fill_holes(self, of: &Area) -> Result<Area> {
self.0.call("areas.fillHoles", &[of.into()]).map(Area::of)
}
pub fn grow(self, of: &Area, by: impl Into<Arg>) -> Result<Area> {
self.0.call("areas.grow", &[of.into(), by.into()]).map(Area::of)
}
pub fn moved(self, of: &Area, by: [impl Into<Arg> + Clone; 2]) -> Result<Area> {
self.0.call("areas.move", &[of.into(), Arg::from(by)]).map(Area::of)
}
pub fn around(self, of: &Features, by: impl Into<Arg>) -> Result<Area> {
self.0.call("areas.around", &[of.into(), by.into()]).map(Area::of)
}
pub fn buffer(self, of: &Lines, by: impl Into<Arg>, options: Obj) -> Result<Area> {
self.0.call("areas.buffer", &[of.into(), by.into(), Arg::Map(options)]).map(Area::of)
}
pub fn shape(self, spec: Obj) -> Result<Area> {
self.0.call("areas.shape", &[Arg::Map(spec)]).map(Area::of)
}
pub fn text(self, spec: Obj) -> Result<Area> {
self.0.call("areas.text", &[Arg::Map(spec)]).map(Area::of)
}
pub fn band(self, band: Obj, options: Obj) -> Result<Area> {
self.0.call("areas.band", &[Arg::Map(band), Arg::Map(options)]).map(Area::of)
}
pub fn extend_to_frame(self, of: &Area, within: &Area, options: Obj) -> Result<Area> {
self.0.call("areas.extendToFrame", &[of.into(), within.into(), Arg::Map(options)]).map(Area::of)
}
pub fn ocean(self) -> Result<Area> {
self.0.call("engine.ocean", &[]).map(Area::of)
}
}
impl LinesApi<'_> {
pub fn of(self, features: &Features) -> Result<Lines> {
self.0.call("lines.of", &[features.into()]).map(Lines::of)
}
pub fn rings(self, of: &Area, options: Obj) -> Result<Lines> {
self.0.call("lines.rings", &[of.into(), Arg::Map(options)]).map(Lines::of)
}
pub fn segments(self, features: &Features) -> Result<Lines> {
self.0.call("lines.segments", &[features.into()]).map(Lines::of)
}
pub fn shape(self, spec: Obj) -> Result<Lines> {
self.0.call("lines.shape", &[Arg::Map(spec)]).map(Lines::of)
}
pub fn text(self, spec: Obj) -> Result<Lines> {
self.0.call("lines.text", &[Arg::Map(spec)]).map(Lines::of)
}
pub fn dedupe(self, of: &Lines) -> Result<Lines> {
self.0.call("lines.dedupe", &[of.into()]).map(Lines::of)
}
pub fn merge(self, of: &Lines) -> Result<Lines> {
self.0.call("lines.merge", &[of.into()]).map(Lines::of)
}
pub fn join(self, sets: &[&Lines]) -> Result<Lines> {
self.0.call("lines.join", &many(sets)).map(Lines::of)
}
pub fn clip(self, of: &Lines, within: &Area, options: Obj) -> Result<Lines> {
self.0.call("lines.clip", &[of.into(), within.into(), Arg::Map(options)]).map(Lines::of)
}
pub fn terrain(self, options: Obj) -> Result<Lines> {
self.0.call("lines.terrain", &[Arg::Map(options)]).map(Lines::of)
}
pub fn contours(self, options: Obj) -> Result<Lines> {
self.0.call("lines.contours", &[Arg::Map(options)]).map(Lines::of)
}
pub fn burin(self, atlas: serde_json::Value, options: Obj) -> Result<Lines> {
self.0.call("burin.terrain", &[Arg::from_json(&atlas), Arg::Map(options)]).map(Lines::of)
}
}
impl FillsApi<'_> {
pub fn fill(self, spec: Obj) -> Result<Lines> {
self.0.call("fills.fill", &[Arg::Map(spec)]).map(Lines::of)
}
pub fn cover(self, spec: Obj) -> Result<Area> {
self.0.call("fills.cover", &[Arg::Map(spec)]).map(Area::of)
}
}
impl ModulesApi<'_> {
pub fn roads(self, options: Obj) -> Result<Roads> {
let out = self.0.call("modules.roads", &[Arg::Map(options)])?;
let o = out.as_map().cloned().unwrap_or_default();
let lines = |k: &str| Lines(o.get(k).cloned().unwrap_or(Arg::Null));
Ok(Roads {
area: Area(o.get("area").cloned().unwrap_or(Arg::Null)),
outlines: lines("outlines"),
details: lines("details"),
trails: lines("trails"),
centerlines: lines("centerlines"),
seams: lines("seams"),
rails: lines("rails"),
})
}
pub fn labels(self, options: Obj) -> Result<Module> {
self.0.call("modules.labels", &[Arg::Map(options)]).map(Module)
}
pub fn graticule(self, options: Obj) -> Result<Module> {
self.0.call("modules.graticule", &[Arg::Map(options)]).map(Module)
}
}
impl Module {
pub fn lines(&self, g: &mut Graph, part: &str) -> Result<Lines> {
g.get(&self.0, part).map(Lines)
}
pub fn area(&self, g: &mut Graph, part: &str) -> Result<Area> {
g.get(&self.0, part).map(Area)
}
pub fn rule(&self, g: &mut Graph, id: &str) -> Result<Module> {
g.get(&self.0, id).map(Module)
}
}
impl ComposeApi<'_> {
pub fn order(self, ids: &[&str]) -> Result<()> {
self.0.call("compose.order", &[list(ids)]).map(|_| ())
}
pub fn mask(self, spec: Obj) -> Result<()> {
self.0.call("compose.mask", &[Arg::Map(spec)]).map(|_| ())
}
pub fn clip(self, clip: impl Into<Arg>) -> Result<()> {
self.0.call("compose.clip", &[clip.into()]).map(|_| ())
}
pub fn step(self, step: &str, layers: &[&str], options: Obj) -> Result<()> {
let mut args = Vec::new();
if !layers.is_empty() {
args.push(list(layers));
}
args.push(Arg::Map(options));
self.0.call(&format!("compose.{step}"), &args).map(|_| ())
}
}
impl PageApi<'_> {
pub fn width(self) -> Result<f64> {
self.0.get_property("page.width").map(|v| v.as_num().unwrap_or(0.0))
}
pub fn height(self) -> Result<f64> {
self.0.get_property("page.height").map(|v| v.as_num().unwrap_or(0.0))
}
pub fn center(self) -> Result<[f64; 2]> {
let w = self.0.get_property("page.width")?.as_num().unwrap_or(0.0);
let h = self.0.get_property("page.height")?.as_num().unwrap_or(0.0);
Ok([w / 2.0, h / 2.0])
}
pub fn frame(self) -> Result<Area> {
self.0.get_property("page.frame").map(Area)
}
pub fn to_world(self, p: [f64; 2]) -> Result<(f64, f64)> {
let v = self.0.call("page.toWorld", &[Arg::from(p)])?;
let o = v.as_map().cloned().unwrap_or_default();
Ok((o.get("lat").and_then(Arg::as_num).unwrap_or(0.0), o.get("lng").and_then(Arg::as_num).unwrap_or(0.0)))
}
}
impl WorldApi<'_> {
pub fn scale(self) -> Result<f64> {
self.0.get_property("world.scale").map(|v| v.as_num().unwrap_or(0.0))
}
pub fn bearing(self) -> Result<f64> {
self.0.get_property("world.bearing").map(|v| v.as_num().unwrap_or(0.0))
}
pub fn center(self) -> Result<(f64, f64)> {
let o = self.0.get_property("world.center")?.as_map().cloned().unwrap_or_default();
Ok((o.get("lat").and_then(Arg::as_num).unwrap_or(0.0), o.get("lng").and_then(Arg::as_num).unwrap_or(0.0)))
}
pub fn bounds(self) -> Result<(f64, f64, f64, f64)> {
let o = self.0.get_property("world.bounds")?.as_map().cloned().unwrap_or_default();
let n = |k: &str| o.get(k).and_then(Arg::as_num).unwrap_or(0.0);
Ok((n("north"), n("south"), n("east"), n("west")))
}
pub fn to_page(self, lat: f64, lng: f64) -> Result<[f64; 2]> {
let v = self.0.call("world.toPage", &[Arg::Map(crate::obj! { "lat" => lat, "lng" => lng })])?;
let l = v.as_list().unwrap_or(&[]);
Ok([l.first().and_then(Arg::as_num).unwrap_or(0.0), l.get(1).and_then(Arg::as_num).unwrap_or(0.0)])
}
}