#![feature(try_trait)]
use {
anyhow::{Context, Result},
std::process,
};
struct Command {
name: String,
rep: process::Command,
}
impl Command {
pub fn new(name: &'static str) -> Self {
let rep = process::Command::new(&name);
let name = String::from(name);
Command { name, rep }
}
pub fn run(&mut self, args: &[&str]) -> Result<String> {
self.rep.args(args);
let stdout = self.stdout()?;
Ok(String::from(&stdout).trim().to_string())
}
fn stdout(&mut self) -> Result<String> {
let output = self
.rep
.output()
.with_context(|| format!("could not execute command: {}", self.name))?;
let result = String::from_utf8(output.stdout)
.with_context(|| format!("could not convert output to UTF8"))?;
Ok(result.trim().to_string())
}
}
struct ICUConfig {
rep: Command,
}
impl ICUConfig {
fn new() -> Self {
ICUConfig {
rep: Command::new("pkg-config"),
}
}
fn version(&mut self) -> Result<String> {
self.rep
.run(&["--modversion", "icu-i18n"])
.with_context(|| format!("while getting ICU version; is icu-config in $PATH?"))
}
fn version_major() -> Result<String> {
let version = ICUConfig::new().version()?;
let components = version.split(".");
let last = components
.take(1)
.last()
.with_context(|| format!("could not parse version number: {}", version))?;
Ok(last.to_string())
}
fn version_major_int() -> Result<i32> {
let version_str = ICUConfig::version_major()?;
Ok(version_str.parse().unwrap())
}
}
fn main() -> Result<()> {
std::env::set_var("RUST_BACKTRACE", "full");
let icu_major_version = ICUConfig::version_major_int()?;
println!("icu-major-version: {}", icu_major_version);
if icu_major_version >= 64 {
println!("cargo:rustc-cfg=features=\"icu_version_64_plus\"");
}
if icu_major_version >= 67 {
println!("cargo:rustc-cfg=features=\"icu_version_67_plus\"");
}
println!("done");
Ok(())
}