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>>;
}
}
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 = format!(
"{{\"mjolnir\":{{\"tile_extract\":\"{}\"}}}}",
tile_extract.as_ref().display()
);
Self::from_json(&config)
}
pub(crate) fn inner(&self) -> &ffi::ptree {
self.0.as_ref().unwrap()
}
}