view rust/hg-pyo3/src/exceptions.rs @ 52795:827889802d11

rust-pyo3-revlog: nodemap filling accessor This follows closely the original version in `hg-cpython`, with perhaps room for improvement to spare the caller with obscure and scary calls to `except()`.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Wed, 25 Dec 2024 14:00:34 +0100
parents 918239b55b3a
children 5e3e8876fd9e
line wrap: on
line source

use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::import_exception;
use pyo3::{create_exception, PyErr};

use hg::revlog::nodemap::NodeMapError;

use crate::revision::PyRevision;

create_exception!(pyo3_rustext, GraphError, PyValueError);
import_exception!(mercurial.error, WdirUnsupported);

impl GraphError {
    pub fn from_hg(inner: hg::GraphError) -> PyErr {
        match inner {
            hg::GraphError::ParentOutOfRange(r) => {
                GraphError::new_err(("ParentOutOfRange", PyRevision(r.0)))
            }
            hg::GraphError::ParentOutOfOrder(r) => {
                GraphError::new_err(("ParentOutOfOrder", PyRevision(r.0)))
            }
        }
    }
    pub fn from_vcsgraph(inner: vcsgraph::graph::GraphReadError) -> PyErr {
        match inner {
            vcsgraph::graph::GraphReadError::InconsistentGraphData => {
                GraphError::new_err("InconsistentGraphData")
            }
            vcsgraph::graph::GraphReadError::InvalidKey => {
                GraphError::new_err("ParentOutOfRange")
            }
            vcsgraph::graph::GraphReadError::KeyedInvalidKey(r) => {
                GraphError::new_err(("ParentOutOfRange", r))
            }
            vcsgraph::graph::GraphReadError::WorkingDirectoryUnsupported => {
                WdirUnsupported::new_err(())
            }
        }
    }
}

pub fn map_lock_error<T>(e: std::sync::PoisonError<T>) -> PyErr {
    PyRuntimeError::new_err(format!("In Rust PyO3 bindings: {e}"))
}

/// Submodule to hold Mercurial errors defined on the Python side
///
/// This is better for clarity, as many hg-core errors have the same names
/// as their Python world counterparts
pub mod mercurial_py_errors {
    pyo3::import_exception!(mercurial.error, RevlogError);
}

pub fn revlog_error_from_msg(e: impl ToString) -> PyErr {
    mercurial_py_errors::RevlogError::new_err(e.to_string().into_bytes())
}

pub fn nodemap_error(err: NodeMapError) -> PyErr {
    match err {
        NodeMapError::MultipleResults => {
            mercurial_py_errors::RevlogError::new_err("")
        }

        NodeMapError::RevisionNotInIndex(rev) => {
            PyValueError::new_err(format!(
                "Inconsistency: Revision {} found in nodemap \
             is not in revlog index",
                rev
            ))
        }
    }
}