use super::super::types::OxiGdalBbox;
use super::geometry::FfiGeometry;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum FieldValue {
Null,
String(String),
Integer(i64),
Double(f64),
Bool(bool),
}
impl FieldValue {
#[must_use]
pub fn to_string_value(&self) -> String {
match self {
Self::Null => String::new(),
Self::String(s) => s.clone(),
Self::Integer(i) => i.to_string(),
Self::Double(d) => d.to_string(),
Self::Bool(b) => b.to_string(),
}
}
#[must_use]
pub fn as_integer(&self) -> Option<i64> {
match self {
Self::Integer(i) => Some(*i),
Self::Double(d) => Some(*d as i64),
Self::Bool(b) => Some(if *b { 1 } else { 0 }),
Self::String(s) => s.parse().ok(),
Self::Null => None,
}
}
#[must_use]
pub fn as_double(&self) -> Option<f64> {
match self {
Self::Double(d) => Some(*d),
Self::Integer(i) => Some(*i as f64),
Self::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
Self::String(s) => s.parse().ok(),
Self::Null => None,
}
}
}
#[derive(Debug, Clone)]
pub struct FfiFeature {
pub fid: i64,
pub geometry: Option<FfiGeometry>,
pub fields: HashMap<String, FieldValue>,
}
impl FfiFeature {
#[must_use]
pub fn new(fid: i64) -> Self {
Self {
fid,
geometry: None,
fields: HashMap::new(),
}
}
#[must_use]
pub fn bounds(&self) -> Option<(f64, f64, f64, f64)> {
self.geometry.as_ref().and_then(FfiGeometry::bounds)
}
#[must_use]
pub fn intersects_bbox(&self, bbox: &OxiGdalBbox) -> bool {
if let Some((min_x, min_y, max_x, max_y)) = self.bounds() {
!(max_x < bbox.min_x || min_x > bbox.max_x || max_y < bbox.min_y || min_y > bbox.max_y)
} else {
false
}
}
}
pub struct FeatureHandle {
feature: FfiFeature,
}
impl FeatureHandle {
#[must_use]
pub fn new(feature: FfiFeature) -> Self {
Self { feature }
}
#[must_use]
pub fn inner(&self) -> &FfiFeature {
&self.feature
}
}