Mercurial > public > mercurial-scm > hg
comparison rust/hg-cpython/src/revlog.rs @ 46432:18a261b11b20
rust: Remove hex parsing from the nodemap
Separating concerns simplifies error types.
Differential Revision: https://phab.mercurial-scm.org/D9864
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 25 Jan 2021 18:25:26 +0100 |
parents | 645ee7225fab |
children | 3c9208702db3 |
comparison
equal
deleted
inserted
replaced
46431:645ee7225fab | 46432:18a261b11b20 |
---|---|
15 ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyModule, PyObject, | 15 ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyModule, PyObject, |
16 PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject, | 16 PyResult, PyString, PyTuple, Python, PythonObject, ToPyObject, |
17 }; | 17 }; |
18 use hg::{ | 18 use hg::{ |
19 nodemap::{Block, NodeMapError, NodeTree}, | 19 nodemap::{Block, NodeMapError, NodeTree}, |
20 revlog::{nodemap::NodeMap, RevlogIndex}, | 20 revlog::{nodemap::NodeMap, NodePrefix, RevlogIndex}, |
21 Revision, | 21 Revision, |
22 }; | 22 }; |
23 use std::cell::RefCell; | 23 use std::cell::RefCell; |
24 | 24 |
25 /// Return a Struct implementing the Graph trait | 25 /// Return a Struct implementing the Graph trait |
105 else { | 105 else { |
106 let node = node.extract::<PyBytes>(py)?; | 106 let node = node.extract::<PyBytes>(py)?; |
107 String::from_utf8_lossy(node.data(py)).to_string() | 107 String::from_utf8_lossy(node.data(py)).to_string() |
108 }; | 108 }; |
109 | 109 |
110 nt.find_hex(idx, &node_as_string) | 110 let prefix = NodePrefix::from_hex(&node_as_string).map_err(|_| PyErr::new::<ValueError, _>(py, "Invalid node or prefix"))?; |
111 | |
112 nt.find_bin(idx, prefix) | |
111 // TODO make an inner API returning the node directly | 113 // TODO make an inner API returning the node directly |
112 .map(|opt| opt.map( | 114 .map(|opt| opt.map( |
113 |rev| PyBytes::new(py, idx.node(rev).unwrap().as_bytes()))) | 115 |rev| PyBytes::new(py, idx.node(rev).unwrap().as_bytes()))) |
114 .map_err(|e| nodemap_error(py, e)) | 116 .map_err(|e| nodemap_error(py, e)) |
115 | 117 |
466 /// Standard treatment of NodeMapError | 468 /// Standard treatment of NodeMapError |
467 fn nodemap_error(py: Python, err: NodeMapError) -> PyErr { | 469 fn nodemap_error(py: Python, err: NodeMapError) -> PyErr { |
468 match err { | 470 match err { |
469 NodeMapError::MultipleResults => revlog_error(py), | 471 NodeMapError::MultipleResults => revlog_error(py), |
470 NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r), | 472 NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r), |
471 NodeMapError::InvalidNodePrefix => { | |
472 PyErr::new::<ValueError, _>(py, "Invalid node or prefix") | |
473 } | |
474 } | 473 } |
475 } | 474 } |
476 | 475 |
477 /// Create the module, with __package__ given from parent | 476 /// Create the module, with __package__ given from parent |
478 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { | 477 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { |