D8156: rust-nodemap: add utils for propagating errors

2020-03-11 Thread gracinet (Georges Racinet)
Closed by commit rHG26dd35ac59b8: rust-nodemap: add utils for propagating 
errors (authored by gracinet).
This revision was automatically updated to reflect the committed changes.
This revision was not accepted when it landed; it landed in state "Needs 
Review".

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D8156?vs=20655=20694

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8156/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8156

AFFECTED FILES
  rust/hg-cpython/src/revlog.rs

CHANGE DETAILS

diff --git a/rust/hg-cpython/src/revlog.rs b/rust/hg-cpython/src/revlog.rs
--- a/rust/hg-cpython/src/revlog.rs
+++ b/rust/hg-cpython/src/revlog.rs
@@ -1,16 +1,16 @@
 // revlog.rs
 //
-// Copyright 2019 Georges Racinet 
+// Copyright 2019-2020 Georges Racinet 
 //
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
 use crate::cindex;
 use cpython::{
-ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple,
-Python, PythonObject, ToPyObject,
+exc::ValueError, ObjectProtocol, PyClone, PyDict, PyErr, PyModule,
+PyObject, PyResult, PyTuple, Python, PythonObject, ToPyObject,
 };
-use hg::Revision;
+use hg::{nodemap::NodeMapError, NodeError, Revision};
 use std::cell::RefCell;
 
 /// Return a Struct implementing the Graph trait
@@ -224,6 +224,43 @@
 }
 }
 
+fn revlog_error(py: Python) -> PyErr {
+match py
+.import("mercurial.error")
+.and_then(|m| m.get(py, "RevlogError"))
+{
+Err(e) => e,
+Ok(cls) => PyErr::from_instance(py, cls),
+}
+}
+
+fn rev_not_in_index(py: Python, rev: Revision) -> PyErr {
+PyErr::new::(
+py,
+format!(
+"Inconsistency: Revision {} found in nodemap \
+ is not in revlog index",
+rev
+),
+)
+}
+
+/// Standard treatment of NodeMapError
+fn nodemap_error(py: Python, err: NodeMapError) -> PyErr {
+match err {
+NodeMapError::MultipleResults => revlog_error(py),
+NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
+NodeMapError::InvalidNodePrefix(s) => invalid_node_prefix(py, ),
+}
+}
+
+fn invalid_node_prefix(py: Python, ne: ) -> PyErr {
+PyErr::new::(
+py,
+format!("Invalid node or prefix: {:?}", ne),
+)
+}
+
 /// Create the module, with __package__ given from parent
 pub fn init_module(py: Python, package: ) -> PyResult {
 let dotted_name = !("{}.revlog", package);



To: Alphare, #hg-reviewers
Cc: mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


D8156: rust-nodemap: add utils for propagating errors

2020-03-10 Thread Raphaël Gomès
Alphare updated this revision to Diff 20655.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D8156?vs=20331=20655

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8156/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8156

AFFECTED FILES
  rust/hg-cpython/src/revlog.rs

CHANGE DETAILS

diff --git a/rust/hg-cpython/src/revlog.rs b/rust/hg-cpython/src/revlog.rs
--- a/rust/hg-cpython/src/revlog.rs
+++ b/rust/hg-cpython/src/revlog.rs
@@ -1,16 +1,16 @@
 // revlog.rs
 //
-// Copyright 2019 Georges Racinet 
+// Copyright 2019-2020 Georges Racinet 
 //
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
 use crate::cindex;
 use cpython::{
-ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple,
-Python, PythonObject, ToPyObject,
+exc::ValueError, ObjectProtocol, PyClone, PyDict, PyErr, PyModule,
+PyObject, PyResult, PyTuple, Python, PythonObject, ToPyObject,
 };
-use hg::Revision;
+use hg::{nodemap::NodeMapError, NodeError, Revision};
 use std::cell::RefCell;
 
 /// Return a Struct implementing the Graph trait
@@ -224,6 +224,43 @@
 }
 }
 
+fn revlog_error(py: Python) -> PyErr {
+match py
+.import("mercurial.error")
+.and_then(|m| m.get(py, "RevlogError"))
+{
+Err(e) => e,
+Ok(cls) => PyErr::from_instance(py, cls),
+}
+}
+
+fn rev_not_in_index(py: Python, rev: Revision) -> PyErr {
+PyErr::new::(
+py,
+format!(
+"Inconsistency: Revision {} found in nodemap \
+ is not in revlog index",
+rev
+),
+)
+}
+
+/// Standard treatment of NodeMapError
+fn nodemap_error(py: Python, err: NodeMapError) -> PyErr {
+match err {
+NodeMapError::MultipleResults => revlog_error(py),
+NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
+NodeMapError::InvalidNodePrefix(s) => invalid_node_prefix(py, ),
+}
+}
+
+fn invalid_node_prefix(py: Python, ne: ) -> PyErr {
+PyErr::new::(
+py,
+format!("Invalid node or prefix: {:?}", ne),
+)
+}
+
 /// Create the module, with __package__ given from parent
 pub fn init_module(py: Python, package: ) -> PyResult {
 let dotted_name = !("{}.revlog", package);



To: Alphare, #hg-reviewers
Cc: mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


D8156: rust-nodemap: add utils for propagating errors

2020-02-26 Thread Raphaël Gomès
Alphare created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This also updates the copyright notice

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8156

AFFECTED FILES
  rust/hg-cpython/src/revlog.rs

CHANGE DETAILS

diff --git a/rust/hg-cpython/src/revlog.rs b/rust/hg-cpython/src/revlog.rs
--- a/rust/hg-cpython/src/revlog.rs
+++ b/rust/hg-cpython/src/revlog.rs
@@ -1,16 +1,16 @@
 // revlog.rs
 //
-// Copyright 2019 Georges Racinet 
+// Copyright 2019-2020 Georges Racinet 
 //
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
 use crate::cindex;
 use cpython::{
-ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple,
-Python, PythonObject, ToPyObject,
+exc::ValueError, ObjectProtocol, PyClone, PyDict, PyErr, PyModule,
+PyObject, PyResult, PyTuple, Python, PythonObject, ToPyObject,
 };
-use hg::Revision;
+use hg::{nodemap::NodeMapError, NodeError, Revision};
 use std::cell::RefCell;
 
 /// Return a Struct implementing the Graph trait
@@ -224,6 +224,43 @@
 }
 }
 
+fn revlog_error(py: Python) -> PyErr {
+match py
+.import("mercurial.error")
+.and_then(|m| m.get(py, "RevlogError"))
+{
+Err(e) => e,
+Ok(cls) => PyErr::from_instance(py, cls),
+}
+}
+
+fn rev_not_in_index(py: Python, rev: Revision) -> PyErr {
+PyErr::new::(
+py,
+format!(
+"Inconsistency: Revision {} found in nodemap \
+ is not in revlog index",
+rev
+),
+)
+}
+
+/// Standard treatment of NodeMapError
+fn nodemap_error(py: Python, err: NodeMapError) -> PyErr {
+match err {
+NodeMapError::MultipleResults => revlog_error(py),
+NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
+NodeMapError::InvalidNodePrefix(s) => invalid_node_prefix(py, ),
+}
+}
+
+fn invalid_node_prefix(py: Python, ne: ) -> PyErr {
+PyErr::new::(
+py,
+format!("Invalid node or prefix: {:?}", ne),
+)
+}
+
 /// Create the module, with __package__ given from parent
 pub fn init_module(py: Python, package: ) -> PyResult {
 let dotted_name = !("{}.revlog", package);



To: Alphare, #hg-reviewers
Cc: mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel