use std::fmt;
use std::marker::PhantomData;
use cxx::UniquePtr;
use crate::bridge::sources;
use crate::style::{GeoJson, SourceId};
pub struct GeoJsonSource {
source_id: String,
source: UniquePtr<sources::GeoJSONSource>,
}
impl GeoJsonSource {
#[must_use]
pub fn new(id: &str) -> Self {
Self { source_id: id.to_owned(), source: sources::create(id) }
}
pub fn set_url(&mut self, url: &str) {
sources::setURL(&self.source, url);
}
pub(crate) fn source_id(&self) -> &str {
&self.source_id
}
pub fn set_geojson(&mut self, geojson: &GeoJson) {
sources::setGeoJson(self.source.pin_mut(), geojson.as_inner());
}
pub(crate) fn into_inner(self) -> UniquePtr<sources::GeoJSONSource> {
self.source
}
}
impl fmt::Debug for GeoJsonSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GeoJsonSource")
.field("source_id", &self.source_id)
.field("Pointer", &self.source.as_ptr())
.finish()
}
}
pub struct GeoJsonSourceRefMut<'a> {
source: UniquePtr<sources::GeoJSONSourceHandle>,
_marker: PhantomData<&'a mut ()>,
_not_send: PhantomData<*mut ()>,
}
impl GeoJsonSourceRefMut<'_> {
pub(crate) fn new(source: UniquePtr<sources::GeoJSONSourceHandle>) -> Self {
Self { source, _marker: PhantomData, _not_send: PhantomData }
}
#[must_use]
pub fn source_id(&self) -> SourceId {
SourceId::new(self.source.sourceId())
}
pub fn set_geojson(&mut self, geojson: &GeoJson) {
self.source.pin_mut().setGeoJson(geojson.as_inner());
}
}
impl fmt::Debug for GeoJsonSourceRefMut<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GeoJsonSourceRefMut").field("source_id", &self.source_id()).finish()
}
}