use std::ffi::{CStr, CString, NulError};
use std::os::raw::c_char;
use std::ptr::null;
use std::str::Utf8Error;
pub type GMLDouble = f64;
pub type GMLString = *const c_char;
pub trait GMLStringRead {
fn as_cstr(&self) -> &CStr;
fn as_str(&self) -> Result<&str, Utf8Error>;
}
pub trait GMLStringWrite {
fn into_gml(self) -> Result<GMLString, NulError>;
}
pub trait None {
fn none() -> Self;
}
impl GMLStringRead for GMLString {
fn as_cstr(&self) -> &CStr {
unsafe { &CStr::from_ptr(*self) }
}
fn as_str(&self) -> Result<&str, Utf8Error> {
if self.is_null() {
return Ok("");
}
unsafe { CStr::from_ptr(*self).to_str() }
}
}
impl GMLStringWrite for &str {
fn into_gml(self) -> Result<GMLString, NulError> {
CString::new(self)
.map(|c_string| c_string.into_raw() as GMLString)
}
}
impl GMLStringWrite for String {
fn into_gml(self) -> Result<GMLString, NulError> {
self.as_str().into_gml()
}
}
impl None for GMLString {
fn none() -> Self {
null()
}
}
impl None for GMLDouble {
fn none() -> Self {
0.0
}
}