use neopdf::manage::{ManageData, PdfSetFormat};
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
#[pyclass(name = "PdfSetFormat")]
#[derive(Clone)]
pub enum PyPdfSetFormat {
Lhapdf,
Neopdf,
}
impl From<PyPdfSetFormat> for PdfSetFormat {
fn from(fmt: PyPdfSetFormat) -> Self {
match fmt {
PyPdfSetFormat::Lhapdf => Self::Lhapdf,
PyPdfSetFormat::Neopdf => Self::Neopdf,
}
}
}
#[pyclass(name = "ManageData")]
pub struct PyManageData {
pub(crate) inner: ManageData,
}
#[pymethods]
impl PyManageData {
#[new]
#[must_use]
pub fn new(set_name: &str, format: PyPdfSetFormat) -> Self {
Self {
inner: ManageData::new(set_name, format.into()),
}
}
pub fn download_pdf(&self) -> PyResult<()> {
self.inner
.download_pdf()
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[must_use]
pub fn is_pdf_installed(&self) -> bool {
self.inner.is_pdf_installed()
}
pub fn ensure_pdf_installed(&self) -> PyResult<()> {
self.inner
.ensure_pdf_installed()
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[must_use]
pub fn set_name(&self) -> &str {
self.inner.set_name()
}
#[must_use]
pub fn data_path(&self) -> String {
self.inner.data_path().to_string_lossy().to_string()
}
#[must_use]
pub fn set_path(&self) -> String {
self.inner.set_path().to_string_lossy().to_string()
}
}
pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
let m = PyModule::new(parent_module.py(), "manage")?;
m.setattr(
pyo3::intern!(m.py(), "__doc__"),
"PDF set management utilities.",
)?;
pyo3::py_run!(
parent_module.py(),
m,
"import sys; sys.modules['neopdf.manage'] = m"
);
m.add_class::<PyPdfSetFormat>()?;
m.add_class::<PyManageData>()?;
parent_module.add_submodule(&m)
}