use extendr_api::prelude::*;
use extendr_api::List;
pub fn as_rsgeo_vctr(mut x: List, class: &str) -> Robj {
x.set_class(geom_class(class)).unwrap().clone().into()
}
pub fn geom_class(cls: &str) -> [String; 4] {
let cls = cls.to_uppercase();
let geom_class = "rs_".to_owned() + cls.as_str();
[
geom_class,
String::from("rsgeo"),
String::from("vctrs_vctr"),
String::from("list"),
]
}
pub fn determine_geoms_class(x: &List) -> [String; 4] {
let class = x[0].class().unwrap().nth(0).unwrap();
let all_identical = x
.iter()
.all(|(_, robj)| robj.class().unwrap().nth(0).unwrap() == class);
let class = if all_identical {
x[0].class().unwrap().nth(0).unwrap()
} else {
"geometrycollection"
};
geom_class(class)
}
pub fn is_rsgeo(x: &List) -> Rbool {
if x.is_null() {
return Rbool::na();
} else {
let cls = x.class().unwrap().next().unwrap();
return cls.starts_with("rs_").into();
}
}
pub fn verify_rsgeo(x: &List) {
let cls = x.class().unwrap().next().unwrap();
if !cls.starts_with("rs_") {
panic!("`x` must be a Rust geometry type")
}
}
pub fn rsgeo_type(x: &List) -> String {
if !x.inherits("rsgeo") {
panic!("object is not an `rsgeo` vector")
}
let cls = x.class().unwrap().next().unwrap();
if !cls.starts_with("rs_") {
panic!("Object is not an `rsgeo` vector with `rs_` prefix")
}
let mut cls = cls.to_string();
cls.split_off(3).to_lowercase()
}