use std::{fmt::Debug, path::Path};
use pyo3::{
prelude::{PyAnyMethods, PyTypeMethods},
Bound, PyAny,
};
use crate::builders::{cmake::CMake, make::Make};
pub trait BuilderImpl: Sized + Clone {
fn from_py(object: &Bound<PyAny>) -> Result<Self, String>;
fn build<
P0: AsRef<Path> + Debug,
P1: AsRef<Path> + Debug,
P2: AsRef<Path>,
>(
&self,
source_path: &P0,
build_path: &P1,
install_path: &P2, dependencies: &[String],
) -> Result<(), String>;
fn install<P0: AsRef<Path>, P1: AsRef<Path>, P2: AsRef<Path>>(
&self,
source_path: &P0,
build_path: &P1,
install_path: &P2,
dependencies: &[String],
) -> Result<(), String>;
}
#[derive(Debug, Clone)]
pub enum Builder {
CMake(CMake),
Make(Make),
}
impl BuilderImpl for Builder {
fn from_py(object: &Bound<PyAny>) -> Result<Self, String> {
let name = object.get_type().name().unwrap().to_string();
match name.as_str() {
"CMake" => Ok(Self::CMake(CMake::from_py(object)?)),
"Make" => Ok(Self::Make(Make::from_py(object)?)),
_ => Err("Invalid builder type".to_string()),
}
}
fn build<
P0: AsRef<Path> + Debug,
P1: AsRef<Path> + Debug,
P2: AsRef<Path>,
>(
&self,
source_path: &P0,
build_path: &P1,
install_path: &P2,
dependencies: &[String],
) -> Result<(), String> {
match self {
Self::CMake(cmake) => {
cmake.build(source_path, build_path, install_path, dependencies)
}
Self::Make(make) => {
make.build(source_path, build_path, install_path, dependencies)
}
}
}
fn install<P0: AsRef<Path>, P1: AsRef<Path>, P2: AsRef<Path>>(
&self,
source_path: &P0,
build_path: &P1,
install_path: &P2,
dependencies: &[String],
) -> Result<(), String> {
match self {
Self::CMake(cmake) => cmake.install(
source_path,
build_path,
install_path,
dependencies,
),
Self::Make(make) => make.install(
source_path,
build_path,
install_path,
dependencies,
),
}
}
}