sapling/eden/scm/lib
David Tolnay 066f461bcf Replace reliance on From<Context<_>> with an extension trait and constructor
Summary:
Edenapi uses a group of impls like the following as the canonical way to construct ApiError.

```
impl From<Context<ApiErrorKind>> for ApiError {
```

Downstream code would write:

```
use failure::ResultExt;

let stream_trees = config
    .get_or_default("edenapi", "streamtrees")
    .context(ApiErrorKind::BadConfig("edenapi.streamtrees"))?;
```

This relies on the way that ResultExt::context returns a unique type that is different from failure::Error.

This diff introduces a dedicated extension trait to allow the same code in the caller to continue to work even without a failure::Context<T> type, which will be required for dropping our dependency on failure::Fail and failure.

```
pub trait ApiErrorContext<T> {
    fn context(self, kind: ApiErrorKind) -> ApiResult<T>;
}
```

We also introduce a public constructor for ApiError and replace indirect construction that used to look like this:

```
error.context(ApiErrorKind::Curl).into()
```

with this instead:

```
ApiError::new(ApiErrorKind::Curl, error)
```

which is the same number of characters but clearer. The argument order matches [std::io::Error::new](https://doc.rust-lang.org/std/io/struct.Error.html#method.new).

Reviewed By: kulshrax

Differential Revision: D18574668

fbshipit-source-id: 0a56297bb942a26d75a62ca39fc16abeb4486345
2019-11-18 12:12:43 -08:00
..
asyncrevisionstore/src rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
auth rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
backingstore Replace Folly Format with fmt in logger to reduce binary size 2019-11-18 05:53:08 -08:00
blackbox rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
bookmarkstore rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
cdatapack Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
clib Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
clidispatch rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
cliparser rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
commitcloudsubscriber commitcloudsubscriber: Consolidate CommitCloudHttpError into ErrorKind enum 2019-11-18 11:44:52 -08:00
commitstore/bench-serialize Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
configparser rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
cpython-ext rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
cpython-failure Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
dag rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
drawdag Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
edenapi Replace reliance on From<Context<_>> with an extension trait and constructor 2019-11-18 12:12:43 -08:00
encoding Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
hg_watchman_client rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
hgcommands rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
hgtime Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
indexedlog Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
linelog Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
lz4-pyframe rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
manifest rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
metalog Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
mincode Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
minibench Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
mpatch Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
mpatch-sys Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
mutationstore rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
nodemap rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
pathmatcher pathmatcher: hint globset to use prefix strategy instead of regexp strategy 2019-11-14 14:27:39 -08:00
procinfo Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
radixbuf rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
revisionstore rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
stackdesc Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
third-party Merge fb-mercurial sources into the eden repository 2019-11-13 20:20:32 -08:00
tracing-collector Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
treestate rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
types rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
util rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
vlqencoding Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
watchman_client rust: Replace derive(Fail) with derive(Error) 2019-11-14 22:04:38 -08:00
workingcopy rust: Rename Fallible<T> to Result<T> 2019-11-14 14:11:01 -08:00
xdiff Merge fb-mercurial sources into the eden repository 2019-11-13 20:20:32 -08:00
xdiff-sys Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
zstdelta Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
zstore Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
CMakeLists.txt Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00
README.md Move fb-mercurial sources into an eden/scm subdirectory. 2019-11-13 16:04:48 -08:00

lib

Any native code (C/C++/Rust) that Mercurial (either core or extensions) depends on should go here. Python code, or native code that depends on Python code (e.g. #include <Python.h> or use cpython) is disallowed.

As we start to convert more of Mercurial into Rust, and write new paths entrirely in native code, we'll want to limit our dependency on Python, which is why this barrier exists.

See also hgext/extlib/README.md, mercurial/cext/README.mb.

How do I choose between lib and extlib (and cext)?

If your code is native and doesn't depend on Python (awesome!), it goes here.

Otherwise, put it in hgext/extlib (if it's only used by extensions) or mercurial/cext (if it's used by extensions or core).