use super::util::{from_json, into_py_result};
use crate::python_facade as facade;
use pyo3::prelude::*;
use pyo3::types::PyModule;
use pyo3::wrap_pyfunction;

#[pyfunction]
pub(crate) fn compute_source_root_hash_json(source_files_json: &str) -> PyResult<String> {
    let source_files: Vec<facade::SourceFileEntry> =
        from_json("parse source files for source root hash", source_files_json)?;
    Ok(hex::encode(
        facade::compute_source_root_hash(&source_files).0,
    ))
}

#[pyfunction]
pub(crate) fn validate_source_manifest_json(manifest_json: &str) -> PyResult<()> {
    let manifest: facade::SourceManifest =
        from_json("parse source manifest for validation", manifest_json)?;
    into_py_result(facade::validate_source_manifest(&manifest))
}

pub(crate) fn register_manifest_bindings(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(compute_source_root_hash_json, m)?)?;
    m.add_function(wrap_pyfunction!(validate_source_manifest_json, m)?)?;
    Ok(())
}
