rust/hg-cpython/src/exceptions.rs
author Georges Racinet <georges.racinet@octobus.net>
Wed, 23 Jan 2019 07:47:04 -0500
branchstable
changeset 41349 ee943a920606
parent 41184 dcf818267bc1
child 42328 94f3a73b6672
permissions -rw-r--r--
rust: error for WdirUnsupported with cpython conversion as exception This introduces WorkingDirectoryUnsupported as an enum variant of GraphError in the core and converts it to the expected `mercurial.error.WdirUnsupported`.

// ancestors.rs
//
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.

//! Bindings for Rust errors
//!
//! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError`
//! but some variants of `hg::GraphError` can be converted directly to other
//! existing Python exceptions if appropriate.
//!
//! [`GraphError`]: struct.GraphError.html
use cpython::exc::ValueError;
use cpython::{PyErr, Python};
use hg;

py_exception!(rustext, GraphError, ValueError);

impl GraphError {
    pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr {
        match inner {
            hg::GraphError::ParentOutOfRange(r) => {
                GraphError::new(py, ("ParentOutOfRange", r))
            }
            hg::GraphError::WorkingDirectoryUnsupported => {
                match py
                    .import("mercurial.error")
                    .and_then(|m| m.get(py, "WdirUnsupported"))
                {
                    Err(e) => e,
                    Ok(cls) => PyErr::from_instance(py, cls),
                }
            }
        }
    }
}