use crate::{try_init, Service};
use toml::Value;
#[macro_export]
macro_rules! init_with_cargo {
($e:expr) => {{
let base = include_str!($e);
$crate::macros::read_cargo(base);
}};
() => {{
let base = include_str!("../Cargo.toml");
$crate::macros::read_cargo(base);
}};
}
#[doc(hidden)]
pub fn read_cargo(input: &str) {
input
.parse::<Value>()
.ok()
.and_then(|toml: Value| -> Option<()> {
let service = Service {
name: read_package_key(&toml, "name")?,
version: read_package_key(&toml, "version")?,
};
try_init(Some(service), true).expect("Could not initialize stackdriver_logger");
None
});
}
fn read_package_key(toml: &Value, key: &str) -> Option<String> {
let key = toml
.get("package")?
.as_table()?
.get(key)?
.as_str()?
.to_owned();
Some(key)
}