rust/hg-pyo3/src/utils.rs
changeset 52977 0c7ac026ed63
parent 52976 d934d730c6c2
child 52978 69d40a9778fe
equal deleted inserted replaced
52976:d934d730c6c2 52977:0c7ac026ed63
     1 use hg::errors::HgError;
     1 use hg::errors::HgError;
     2 use hg::revlog::index::Index as CoreIndex;
     2 use hg::revlog::index::Index as CoreIndex;
     3 use hg::revlog::inner_revlog::RevisionBuffer;
     3 use hg::revlog::inner_revlog::RevisionBuffer;
     4 use pyo3::buffer::{Element, PyBuffer};
     4 use pyo3::buffer::{Element, PyBuffer};
     5 use pyo3::exceptions::PyValueError;
     5 use pyo3::exceptions::{
     6 use pyo3::prelude::*;
     6     PyIOError, PyKeyboardInterrupt, PyRuntimeError, PyValueError,
       
     7 };
     7 use pyo3::types::{PyBytes, PyDict};
     8 use pyo3::types::{PyBytes, PyDict};
       
     9 use pyo3::{intern, prelude::*};
     8 use pyo3_sharedref::SharedByPyObject;
    10 use pyo3_sharedref::SharedByPyObject;
     9 use stable_deref_trait::StableDeref;
    11 use stable_deref_trait::StableDeref;
    10 
    12 
       
    13 use crate::exceptions::FallbackError;
    11 use crate::revlog::{InnerRevlog, PySharedIndex};
    14 use crate::revlog::{InnerRevlog, PySharedIndex};
    12 
    15 
    13 /// Create the module, with `__package__` given from parent
    16 /// Create the module, with `__package__` given from parent
    14 ///
    17 ///
    15 /// According to PyO3 documentation, which links to
    18 /// According to PyO3 documentation, which links to
   299             "not enough bytes read for revision"
   302             "not enough bytes read for revision"
   300         );
   303         );
   301         self.py_bytes
   304         self.py_bytes
   302     }
   305     }
   303 }
   306 }
       
   307 
       
   308 /// Extension trait to help with generic error conversions from hg-core to
       
   309 /// Python.
       
   310 pub(crate) trait HgPyErrExt<T> {
       
   311     fn into_pyerr(self, py: Python) -> PyResult<T>;
       
   312 }
       
   313 
       
   314 impl<T, E> HgPyErrExt<T> for Result<T, E>
       
   315 where
       
   316     HgError: From<E>,
       
   317 {
       
   318     fn into_pyerr(self, py: Python) -> PyResult<T> {
       
   319         self.map_err(|e| match e.into() {
       
   320             err @ HgError::IoError { .. } => {
       
   321                 PyIOError::new_err(err.to_string())
       
   322             }
       
   323             HgError::UnsupportedFeature(e) => {
       
   324                 FallbackError::new_err(e.to_string())
       
   325             }
       
   326             HgError::RaceDetected(_) => {
       
   327                 unreachable!("must not surface to the user")
       
   328             }
       
   329             HgError::Path(path_error) => {
       
   330                 let msg = PyBytes::new(py, path_error.to_string().as_bytes());
       
   331                 let cls = py
       
   332                     .import(intern!(py, "mercurial.error"))
       
   333                     .and_then(|m| m.getattr(intern!(py, "InputError")))
       
   334                     .expect("failed to import InputError");
       
   335                 PyErr::from_value(
       
   336                     cls.call1((msg,))
       
   337                         .expect("initializing an InputError failed"),
       
   338                 )
       
   339             }
       
   340             HgError::InterruptReceived => PyKeyboardInterrupt::new_err(()),
       
   341             e => PyRuntimeError::new_err(e.to_string()),
       
   342         })
       
   343     }
       
   344 }
       
   345