use std::{os::unix::ffi::OsStrExt, path::Path};
use crate::Error;
#[cxx::bridge]
pub(crate) mod ffi {
unsafe extern "C++" {
include!("valhalla/src/config.hpp");
#[namespace = "boost::property_tree"]
type ptree;
fn from_file(path: &[u8]) -> Result<UniquePtr<ptree>>;
fn from_json(config: &str) -> Result<UniquePtr<ptree>>;
fn ptree_new() -> UniquePtr<ptree>;
fn ptree_put_str(pt: Pin<&mut ptree>, path: &str, value: &str);
fn ptree_put_bool(pt: Pin<&mut ptree>, path: &str, value: bool);
fn ptree_put_int(pt: Pin<&mut ptree>, path: &str, value: i64);
fn ptree_put_float(pt: Pin<&mut ptree>, path: &str, value: f64);
fn ptree_put_str_array(pt: Pin<&mut ptree>, path: &str, values: &[String]);
fn ptree_put_int_array(pt: Pin<&mut ptree>, path: &str, values: &[i64]);
}
}
pub struct Config(cxx::UniquePtr<ffi::ptree>);
impl Config {
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(Config(ffi::from_file(
path.as_ref().as_os_str().as_bytes(),
)?))
}
pub fn from_json(config: &str) -> Result<Self, Error> {
Ok(Config(ffi::from_json(config)?))
}
pub fn from_tile_extract(tile_extract: impl AsRef<Path>) -> Result<Self, Error> {
let config = ConfigBuilder {
mjolnir: Mjolnir {
tile_extract: tile_extract.as_ref().display().to_string(),
..Default::default()
},
..Default::default()
}
.build();
Ok(config)
}
pub(crate) fn inner(&self) -> &ffi::ptree {
self.0.as_ref().unwrap()
}
}
include!(concat!(env!("OUT_DIR"), "/config_builder.rs"));