use crate::context_handle::with_context;
use crate::functions::*;
use crate::traits::as_raw_mut_impl;
#[cfg(feature = "v3_14_0")]
use crate::CoordDimensions;
use crate::{AsRaw, AsRawMut, GResult, Geom};
use geos_sys::*;
use std::ptr::NonNull;
pub struct GeoJSONWriter {
ptr: NonNull<GEOSGeoJSONWriter>,
}
impl GeoJSONWriter {
pub fn new() -> GResult<Self> {
with_context(|ctx| unsafe {
let ptr = nullcheck!(GEOSGeoJSONWriter_create_r(ctx.as_raw()))?;
Ok(Self { ptr })
})
}
pub fn write<G: Geom>(&mut self, geometry: &G) -> GResult<String> {
self.write_formatted(geometry, -1)
}
pub fn write_formatted<G: Geom>(&mut self, geometry: &G, indent: i32) -> GResult<String> {
with_context(|ctx| unsafe {
let ptr = nullcheck!(GEOSGeoJSONWriter_writeGeometry_r(
ctx.as_raw(),
self.as_raw_mut(),
geometry.as_raw(),
indent,
))?;
managed_string(ptr, ctx)
})
}
#[cfg(feature = "v3_14_0")]
pub fn set_output_dimension(&mut self, dimension: CoordDimensions) {
with_context(|ctx| unsafe {
GEOSGeoJSONWriter_setOutputDimension_r(
ctx.as_raw(),
self.as_raw_mut(),
dimension.into(),
);
});
}
#[cfg(feature = "v3_14_0")]
pub fn get_out_dimension(&self) -> GResult<CoordDimensions> {
with_context(|ctx| unsafe {
let out = errcheck!(
-1,
GEOSGeoJSONWriter_getOutputDimension_r(ctx.as_raw(), self.as_raw_mut_override())
)?;
CoordDimensions::try_from(out)
})
}
}
unsafe impl Send for GeoJSONWriter {}
unsafe impl Sync for GeoJSONWriter {}
impl Drop for GeoJSONWriter {
fn drop(&mut self) {
with_context(|ctx| unsafe { GEOSGeoJSONWriter_destroy_r(ctx.as_raw(), self.as_raw_mut()) });
}
}
as_raw_mut_impl!(GeoJSONWriter, GEOSGeoJSONWriter);