comparison rust/hg-cpython/src/revlog.rs @ 44506:26dd35ac59b8

rust-nodemap: add utils for propagating errors This also updates the copyright notice Differential Revision: https://phab.mercurial-scm.org/D8156
author Georges Racinet <georges.racinet@octobus.net>
date Tue, 11 Feb 2020 16:30:28 +0100
parents 887d0f921b34
children 857cc79247ac
comparison
equal deleted inserted replaced
44505:d738b7a18438 44506:26dd35ac59b8
1 // revlog.rs 1 // revlog.rs
2 // 2 //
3 // Copyright 2019 Georges Racinet <georges.racinet@octobus.net> 3 // Copyright 2019-2020 Georges Racinet <georges.racinet@octobus.net>
4 // 4 //
5 // This software may be used and distributed according to the terms of the 5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version. 6 // GNU General Public License version 2 or any later version.
7 7
8 use crate::cindex; 8 use crate::cindex;
9 use cpython::{ 9 use cpython::{
10 ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple, 10 exc::ValueError, ObjectProtocol, PyClone, PyDict, PyErr, PyModule,
11 Python, PythonObject, ToPyObject, 11 PyObject, PyResult, PyTuple, Python, PythonObject, ToPyObject,
12 }; 12 };
13 use hg::Revision; 13 use hg::{nodemap::NodeMapError, NodeError, Revision};
14 use std::cell::RefCell; 14 use std::cell::RefCell;
15 15
16 /// Return a Struct implementing the Graph trait 16 /// Return a Struct implementing the Graph trait
17 pub(crate) fn pyindex_to_graph( 17 pub(crate) fn pyindex_to_graph(
18 py: Python, 18 py: Python,
222 pub fn clone_cindex(&self, py: Python) -> cindex::Index { 222 pub fn clone_cindex(&self, py: Python) -> cindex::Index {
223 self.cindex(py).borrow().clone_ref(py) 223 self.cindex(py).borrow().clone_ref(py)
224 } 224 }
225 } 225 }
226 226
227 fn revlog_error(py: Python) -> PyErr {
228 match py
229 .import("mercurial.error")
230 .and_then(|m| m.get(py, "RevlogError"))
231 {
232 Err(e) => e,
233 Ok(cls) => PyErr::from_instance(py, cls),
234 }
235 }
236
237 fn rev_not_in_index(py: Python, rev: Revision) -> PyErr {
238 PyErr::new::<ValueError, _>(
239 py,
240 format!(
241 "Inconsistency: Revision {} found in nodemap \
242 is not in revlog index",
243 rev
244 ),
245 )
246 }
247
248 /// Standard treatment of NodeMapError
249 fn nodemap_error(py: Python, err: NodeMapError) -> PyErr {
250 match err {
251 NodeMapError::MultipleResults => revlog_error(py),
252 NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
253 NodeMapError::InvalidNodePrefix(s) => invalid_node_prefix(py, &s),
254 }
255 }
256
257 fn invalid_node_prefix(py: Python, ne: &NodeError) -> PyErr {
258 PyErr::new::<ValueError, _>(
259 py,
260 format!("Invalid node or prefix: {:?}", ne),
261 )
262 }
263
227 /// Create the module, with __package__ given from parent 264 /// Create the module, with __package__ given from parent
228 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { 265 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
229 let dotted_name = &format!("{}.revlog", package); 266 let dotted_name = &format!("{}.revlog", package);
230 let m = PyModule::new(py, dotted_name)?; 267 let m = PyModule::new(py, dotted_name)?;
231 m.add(py, "__package__", package)?; 268 m.add(py, "__package__", package)?;