use pyo3::prelude::*;
pub struct NullCommitReporter(Py<PyAny>);
impl NullCommitReporter {
pub fn new() -> Self {
Python::attach(|py| {
let m = py.import("breezy.commit").unwrap();
let ncr = m.getattr("NullCommitReporter").unwrap();
NullCommitReporter(ncr.call0().unwrap().into())
})
}
}
impl Default for NullCommitReporter {
fn default() -> Self {
Self::new()
}
}
impl From<Py<PyAny>> for NullCommitReporter {
fn from(obj: Py<PyAny>) -> Self {
NullCommitReporter(obj)
}
}
impl<'py> IntoPyObject<'py> for NullCommitReporter {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self.0.into_bound(py))
}
}
pub trait PyCommitReporter: std::any::Any + std::fmt::Debug {
fn to_object(&self, py: Python) -> Py<PyAny>;
}
pub trait CommitReporter: std::fmt::Debug {}
impl<T: PyCommitReporter> CommitReporter for T {}
pub struct GenericCommitReporter(Py<PyAny>);
impl<'py> IntoPyObject<'py> for GenericCommitReporter {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self.0.into_bound(py))
}
}
impl<'a, 'py> FromPyObject<'a, 'py> for GenericCommitReporter {
type Error = PyErr;
fn extract(obj: Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
Ok(GenericCommitReporter(obj.to_owned().unbind()))
}
}
impl PyCommitReporter for GenericCommitReporter {
fn to_object(&self, py: Python) -> Py<PyAny> {
self.0.clone_ref(py)
}
}
impl GenericCommitReporter {
pub fn new(obj: Py<PyAny>) -> Self {
Self(obj)
}
}
impl std::fmt::Debug for GenericCommitReporter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_fmt(format_args!("GenericCommitReporter({:?})", self.0))
}
}
impl PyCommitReporter for NullCommitReporter {
fn to_object(&self, py: Python) -> Py<PyAny> {
self.0.clone_ref(py)
}
}
impl std::fmt::Debug for NullCommitReporter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_fmt(format_args!("NullCommitReporter({:?})", self.0))
}
}
pub struct ReportCommitToLog(Py<PyAny>);
impl ReportCommitToLog {
pub fn new() -> Self {
Python::attach(|py| {
let m = py.import("breezy.commit").unwrap();
let rctl = m.getattr("ReportCommitToLog").unwrap();
ReportCommitToLog(rctl.call0().unwrap().into())
})
}
}
impl Default for ReportCommitToLog {
fn default() -> Self {
Self::new()
}
}
impl From<Py<PyAny>> for ReportCommitToLog {
fn from(obj: Py<PyAny>) -> Self {
ReportCommitToLog(obj)
}
}
impl<'py> IntoPyObject<'py> for ReportCommitToLog {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self.0.into_bound(py))
}
}
impl PyCommitReporter for ReportCommitToLog {
fn to_object(&self, py: Python) -> Py<PyAny> {
self.0.clone_ref(py)
}
}
impl std::fmt::Debug for ReportCommitToLog {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_fmt(format_args!("ReportCommitToLog({:?})", self.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_null_commit_reporter() {
NullCommitReporter::new();
}
#[test]
fn test_report_commit_to_log() {
ReportCommitToLog::new();
}
}