diff options
Diffstat (limited to 'third_party/rust/quick-error')
-rw-r--r-- | third_party/rust/quick-error/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/quick-error/Cargo.lock | 6 | ||||
-rw-r--r-- | third_party/rust/quick-error/Cargo.toml | 23 | ||||
-rw-r--r-- | third_party/rust/quick-error/LICENSE-APACHE | 202 | ||||
-rw-r--r-- | third_party/rust/quick-error/LICENSE-MIT | 19 | ||||
-rw-r--r-- | third_party/rust/quick-error/README.rst | 66 | ||||
-rw-r--r-- | third_party/rust/quick-error/bulk.yaml | 8 | ||||
-rw-r--r-- | third_party/rust/quick-error/examples/context.rs | 48 | ||||
-rw-r--r-- | third_party/rust/quick-error/src/lib.rs | 1262 | ||||
-rw-r--r-- | third_party/rust/quick-error/vagga.yaml | 36 |
10 files changed, 1671 insertions, 0 deletions
diff --git a/third_party/rust/quick-error/.cargo-checksum.json b/third_party/rust/quick-error/.cargo-checksum.json new file mode 100644 index 0000000000..5f19af61cb --- /dev/null +++ b/third_party/rust/quick-error/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"b88df71c076815e9922973e1f54af221486c3a83d5be2c1af9ca650a8f8f526f","Cargo.toml":"1f24b567c7f10b4ec44f5baf033d27da39f659c09df0e8a057437d10ecdd551a","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"058f01fe181608d027fcde7e528fc03ea3cf90f30903c407644b0a9bbc54f500","README.rst":"8bd690e0089d4c38eb71f1327f5144741009f4b2f49f0b16f2547f528171e2e6","bulk.yaml":"17c2548388e0cd3a63473021a2f1e4ddedee082d79d9167cb31ad06a1890d3fc","examples/context.rs":"b9be9a4ca021a1f0ba659932bfc0cf891728bfaea49d48a8be183644c492515b","src/lib.rs":"a02c8a3c40fa1dcee8285b9c238d40cb3078cf4dded1c972fa6361a485d1667f","vagga.yaml":"b01ad1fd3aa25de2439c0f7a437c6517808ba3a7eeeb0363eb209f08e326cc8e"},"package":"a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"}
\ No newline at end of file diff --git a/third_party/rust/quick-error/Cargo.lock b/third_party/rust/quick-error/Cargo.lock new file mode 100644 index 0000000000..da1963ecf4 --- /dev/null +++ b/third_party/rust/quick-error/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "quick-error" +version = "1.2.3" + diff --git a/third_party/rust/quick-error/Cargo.toml b/third_party/rust/quick-error/Cargo.toml new file mode 100644 index 0000000000..b5d5f60b15 --- /dev/null +++ b/third_party/rust/quick-error/Cargo.toml @@ -0,0 +1,23 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "quick-error" +version = "1.2.3" +authors = ["Paul Colomiets <paul@colomiets.name>", "Colin Kiegel <kiegel@gmx.de>"] +description = " A macro which makes error types pleasant to write.\n" +homepage = "http://github.com/tailhook/quick-error" +documentation = "http://docs.rs/quick-error" +keywords = ["macro", "error", "type", "enum"] +categories = ["rust-patterns"] +license = "MIT/Apache-2.0" +repository = "http://github.com/tailhook/quick-error" diff --git a/third_party/rust/quick-error/LICENSE-APACHE b/third_party/rust/quick-error/LICENSE-APACHE new file mode 100644 index 0000000000..8f71f43fee --- /dev/null +++ b/third_party/rust/quick-error/LICENSE-APACHE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/third_party/rust/quick-error/LICENSE-MIT b/third_party/rust/quick-error/LICENSE-MIT new file mode 100644 index 0000000000..14f715b805 --- /dev/null +++ b/third_party/rust/quick-error/LICENSE-MIT @@ -0,0 +1,19 @@ +Copyright (c) 2015 The quick-error Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/rust/quick-error/README.rst b/third_party/rust/quick-error/README.rst new file mode 100644 index 0000000000..e54c055c4e --- /dev/null +++ b/third_party/rust/quick-error/README.rst @@ -0,0 +1,66 @@ +=========== +Quick Error +=========== + +:Status: production-ready +:Documentation: http://tailhook.github.io/quick-error/ + +A macro which makes error types pleasant to write. + +Features: + +* Define enum type with arbitrary parameters +* Concise notation of ``Display`` and ``Error`` traits +* Full control of ``Display`` and ``Error`` trait implementation +* Any number of ``From`` traits +* Support for all enum-variants ``Unit``, ``Tuple`` and ``Struct`` + +Here is the comprehensive example: + +.. code-block:: rust + + quick_error! { + #[derive(Debug)] + pub enum IoWrapper { + Io(err: io::Error) { + from() + display("I/O error: {}", err) + cause(err) + } + Other(descr: &'static str) { + display("Error {}", descr) + } + IoAt { place: &'static str, err: io::Error } { + cause(err) + display(me) -> ("io error at {}: {}", place, err) + from(s: String) -> { + place: "some string", + err: io::Error::new(io::ErrorKind::Other, s) + } + } + Discard { + from(&'static str) + } + } + } + +======= +License +======= + +Licensed under either of + + * Apache License, Version 2.0, (./LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license (./LICENSE-MIT or http://opensource.org/licenses/MIT) + +at your option. + +------------ +Contribution +------------ + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. + diff --git a/third_party/rust/quick-error/bulk.yaml b/third_party/rust/quick-error/bulk.yaml new file mode 100644 index 0000000000..cdb9763b67 --- /dev/null +++ b/third_party/rust/quick-error/bulk.yaml @@ -0,0 +1,8 @@ +minimum-bulk: v0.4.5 + +versions: + +- file: Cargo.toml + block-start: ^\[package\] + block-end: ^\[.*\] + regex: ^version\s*=\s*"(\S+)" diff --git a/third_party/rust/quick-error/examples/context.rs b/third_party/rust/quick-error/examples/context.rs new file mode 100644 index 0000000000..334700a03f --- /dev/null +++ b/third_party/rust/quick-error/examples/context.rs @@ -0,0 +1,48 @@ +#[macro_use(quick_error)] extern crate quick_error; + +use std::io::{self, stderr, Read, Write}; +use std::fs::File; +use std::env; +use std::num::ParseIntError; +use std::path::{Path, PathBuf}; + +use quick_error::ResultExt; + +quick_error! { + #[derive(Debug)] + pub enum Error { + NoFileName { + description("no file name specified") + } + Io(err: io::Error, path: PathBuf) { + display("could not read file {:?}: {}", path, err) + context(path: &'a Path, err: io::Error) + -> (err, path.to_path_buf()) + } + Parse(err: ParseIntError, path: PathBuf) { + display("could not parse file {:?}: {}", path, err) + context(path: &'a Path, err: ParseIntError) + -> (err, path.to_path_buf()) + } + } +} + +fn parse_file() -> Result<u64, Error> { + let fname = try!(env::args().skip(1).next().ok_or(Error::NoFileName)); + let fname = Path::new(&fname); + let mut file = try!(File::open(fname).context(fname)); + let mut buf = String::new(); + try!(file.read_to_string(&mut buf).context(fname)); + Ok(try!(buf.parse().context(fname))) +} + +fn main() { + match parse_file() { + Ok(val) => { + println!("Read: {}", val); + } + Err(e) => { + writeln!(&mut stderr(), "Error: {}", e).ok(); + } + } +} diff --git a/third_party/rust/quick-error/src/lib.rs b/third_party/rust/quick-error/src/lib.rs new file mode 100644 index 0000000000..17fc455d46 --- /dev/null +++ b/third_party/rust/quick-error/src/lib.rs @@ -0,0 +1,1262 @@ +#![warn(missing_docs)] +//! A macro which makes errors easy to write +//! +//! Minimum type is like this: +//! +//! ```rust +//! #[macro_use] extern crate quick_error; +//! # fn main() {} +//! +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Variant1 {} +//! } +//! } +//! ``` +//! Both ``pub`` and non-public types may be declared, and all meta attributes +//! (such as ``#[derive(Debug)]``) are forwarded as is. The `Debug` must be +//! implemented (but you may do that yourself if you like). The documentation +//! comments ``/// something`` (as well as other meta attrbiutes) on variants +//! are allowed. +//! +//! # Allowed Syntax +//! +//! You may add arbitrary parameters to any struct variant: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! /// IO Error +//! Io(err: std::io::Error) {} +//! /// Utf8 Error +//! Utf8(err: std::str::Utf8Error) {} +//! } +//! } +//! ``` +//! +//! Note unlike in normal Enum declarations you declare names of fields (which +//! are omitted from type). How they can be used is outlined below. +//! +//! Now you might have noticed trailing braces `{}`. They are used to define +//! implementations. By default: +//! +//! * `Error::cause()` returns None (even if type wraps some value) +//! * `Display` outputs debug representation +//! * No `From` implementations are defined +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Io(err: std::io::Error) { +//! display("{}", err) +//! } +//! Utf8(err: std::str::Utf8Error) { +//! display("utf8 error") +//! } +//! } +//! } +//! ``` +//! +//! To change `cause` method to return some error, add `cause(value)`, for +//! example: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Io(err: std::io::Error) { +//! cause(err) +//! } +//! Utf8(err: std::str::Utf8Error) { +//! display("utf8 error") +//! } +//! Other(err: Box<std::error::Error>) { +//! cause(&**err) +//! } +//! } +//! } +//! ``` +//! Note you don't need to wrap value in `Some`, its implicit. In case you want +//! `None` returned just omit the `cause`. You can't return `None` +//! conditionally. +//! +//! To change how each clause is `Display`ed add `display(pattern,..args)`, +//! for example: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Io(err: std::io::Error) { +//! display("I/O error: {}", err) +//! } +//! Utf8(err: std::str::Utf8Error) { +//! display("Utf8 error, valid up to {}", err.valid_up_to()) +//! } +//! } +//! } +//! ``` +//! +//! If you need a reference to the error when `Display`ing, you can instead use +//! `display(x) -> (pattern, ..args)`, where `x` sets the name of the reference. +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! use std::error::Error; // put methods like `source()` of this trait into scope +//! +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Io(err: std::io::Error) { +//! display(x) -> ("I/O: {}", err) +//! } +//! Utf8(err: std::str::Utf8Error) { +//! display(self_) -> ("UTF-8 error. Valid up to {}", err.valid_up_to()) +//! } +//! } +//! } +//! ``` +//! +//! To convert to the type from any other, use one of the three forms of +//! `from` clause. +//! +//! For example, to convert simple wrapper use bare `from()`: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! Io(err: std::io::Error) { +//! from() +//! } +//! } +//! } +//! ``` +//! +//! This implements ``From<io::Error>``. +//! +//! To convert to singleton enumeration type (discarding the value), use +//! the `from(type)` form: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! FormatError { +//! from(std::fmt::Error) +//! } +//! } +//! } +//! ``` +//! +//! And the most powerful form is `from(var: type) -> (arguments...)`. It +//! might be used to convert to type with multiple arguments or for arbitrary +//! value conversions: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # fn main() {} +//! # +//! quick_error! { +//! #[derive(Debug)] +//! pub enum SomeError { +//! FailedOperation(s: &'static str, errno: i32) { +//! from(errno: i32) -> ("os error", errno) +//! from(e: std::io::Error) -> ("io error", e.raw_os_error().unwrap()) +//! } +//! /// Converts from both kinds of utf8 errors +//! Utf8(err: std::str::Utf8Error) { +//! from() +//! from(err: std::string::FromUtf8Error) -> (err.utf8_error()) +//! } +//! } +//! } +//! ``` +//! # Context +//! +//! Since quick-error 1.1 we also have a `context` declaration, which is +//! similar to (the longest form of) `from`, but allows adding some context to +//! the error. We need a longer example to demonstrate this: +//! +//! ```rust +//! # #[macro_use] extern crate quick_error; +//! # use std::io; +//! # use std::fs::File; +//! # use std::path::{Path, PathBuf}; +//! # +//! use quick_error::ResultExt; +//! +//! quick_error! { +//! #[derive(Debug)] +//! pub enum Error { +//! File(filename: PathBuf, err: io::Error) { +//! context(path: &'a Path, err: io::Error) +//! -> (path.to_path_buf(), err) +//! } +//! } +//! } +//! +//! fn openfile(path: &Path) -> Result<(), Error> { +//! try!(File::open(path).context(path)); +//! +//! // If we didn't have context, the line above would be written as; +//! // +//! // try!(File::open(path) +//! // .map_err(|err| Error::File(path.to_path_buf(), err))); +//! +//! Ok(()) +//! } +//! +//! # fn main() { +//! # openfile(Path::new("/etc/somefile")).ok(); +//! # } +//! ``` +//! +//! Each `context(a: A, b: B)` clause implements +//! `From<Context<A, B>> for Error`. Which means multiple `context` clauses +//! are a subject to the normal coherence rules. Unfortunately, we can't +//! provide full support of generics for the context, but you may either use a +//! lifetime `'a` for references or `AsRef<Type>` (the latter means `A: +//! AsRef<Type>`, and `Type` must be concrete). It's also occasionally useful +//! to use a tuple as a type of the first argument. +//! +//! You also need to `use quick_error::ResultExt` extension trait to get +//! working `.context()` method. +//! +//! More info on context in [this article](http://bit.ly/1PsuxDt). +//! +//! All forms of `from`, `display`, `cause`, and `context` +//! clauses can be combined and put in arbitrary order. Only `from` and +//! `context` can be used multiple times in single variant of enumeration. +//! Docstrings are also okay. Empty braces can be omitted as of quick_error +//! 0.1.3. +//! +//! # Private Enums +//! +//! Since quick-error 1.2.0 we have a way to make a private enum that is +//! wrapped by public structure: +//! +//! ```rust +//! #[macro_use] extern crate quick_error; +//! # fn main() {} +//! +//! quick_error! { +//! #[derive(Debug)] +//! pub enum PubError wraps ErrorEnum { +//! Variant1 {} +//! } +//! } +//! ``` +//! +//! This generates data structures like this +//! +//! ```rust +//! +//! pub struct PubError(ErrorEnum); +//! +//! enum ErrorEnum { +//! Variant1, +//! } +//! +//! ``` +//! +//! Which in turn allows you to export just `PubError` in your crate and keep +//! actual enumeration private to the crate. This is useful to keep backwards +//! compatibility for error types. Currently there is no shorcuts to define +//! error constructors for the inner type, but we consider adding some in +//! future versions. +//! +//! It's possible to declare internal enum as public too. +//! +//! + + +/// Main macro that does all the work +#[macro_export] +macro_rules! quick_error { + + ( $(#[$meta:meta])* + pub enum $name:ident { $($chunks:tt)* } + ) => { + quick_error!(SORT [pub enum $name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + ( $(#[$meta:meta])* + enum $name:ident { $($chunks:tt)* } + ) => { + quick_error!(SORT [enum $name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + + ( $(#[$meta:meta])* + pub enum $name:ident wraps $enum_name:ident { $($chunks:tt)* } + ) => { + quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*); + quick_error!(SORT [enum $enum_name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + + ( $(#[$meta:meta])* + pub enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* } + ) => { + quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*); + quick_error!(SORT [pub enum $enum_name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + ( $(#[$meta:meta])* + enum $name:ident wraps $enum_name:ident { $($chunks:tt)* } + ) => { + quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*); + quick_error!(SORT [enum $enum_name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + + ( $(#[$meta:meta])* + enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* } + ) => { + quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*); + quick_error!(SORT [pub enum $enum_name $(#[$meta])* ] + items [] buf [] + queue [ $($chunks)* ]); + }; + + + ( + WRAPPER $internal:ident [ $($strdef:tt)* ] $strname:ident + $(#[$meta:meta])* + ) => { + $(#[$meta])* + $($strdef)* $strname ( $internal ); + + impl ::std::fmt::Display for $strname { + fn fmt(&self, f: &mut ::std::fmt::Formatter) + -> ::std::fmt::Result + { + ::std::fmt::Display::fmt(&self.0, f) + } + } + + impl From<$internal> for $strname { + fn from(err: $internal) -> Self { + $strname(err) + } + } + + impl ::std::error::Error for $strname { + #[allow(deprecated)] + fn cause(&self) -> Option<&::std::error::Error> { + self.0.cause() + } + } + }; + + // Queue is empty, can do the work + (SORT [enum $name:ident $( #[$meta:meta] )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [ ] + queue [ ] + ) => { + quick_error!(ENUM_DEFINITION [enum $name $( #[$meta] )*] + body [] + queue [$($( #[$imeta] )* + => $iitem: $imode [$( $ivar: $ityp ),*] )*] + ); + quick_error!(IMPLEMENTATIONS $name {$( + $iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*} + )*}); + $( + quick_error!(ERROR_CHECK $imode $($ifuncs)*); + )* + }; + (SORT [pub enum $name:ident $( #[$meta:meta] )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [ ] + queue [ ] + ) => { + quick_error!(ENUM_DEFINITION [pub enum $name $( #[$meta] )*] + body [] + queue [$($( #[$imeta] )* + => $iitem: $imode [$( $ivar: $ityp ),*] )*] + ); + quick_error!(IMPLEMENTATIONS $name {$( + $iitem: $imode [$(#[$imeta])*] [$( $ivar: $ityp ),*] {$( $ifuncs )*} + )*}); + $( + quick_error!(ERROR_CHECK $imode $($ifuncs)*); + )* + }; + // Add meta to buffer + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )*] + queue [ #[$qmeta:meta] $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] + buf [$( #[$bmeta] )* #[$qmeta] ] + queue [$( $tail )*]); + }; + // Add ident to buffer + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )*] + queue [ $qitem:ident $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* + => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] + buf [$(#[$bmeta])* => $qitem : UNIT [ ] ] + queue [$( $tail )*]); + }; + // Flush buffer on meta after ident + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* + => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ] + queue [ #[$qmeta:meta] $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )* + $(#[$bmeta])* => $bitem: $bmode $(( $($btyp),* ))*] + items [$($( #[$imeta:meta] )* + => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )* + $bitem: $bmode [$( $bvar:$btyp ),*] {} ] + buf [ #[$qmeta] ] + queue [$( $tail )*]); + }; + // Add tuple enum-variant + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ] + queue [($( $qvar:ident: $qtyp:ty ),+) $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] + buf [$( #[$bmeta] )* => $bitem: TUPLE [$( $qvar:$qtyp ),*] ] + queue [$( $tail )*] + ); + }; + // Add struct enum-variant - e.g. { descr: &'static str } + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ] + queue [{ $( $qvar:ident: $qtyp:ty ),+} $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] + buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ] + queue [$( $tail )*]); + }; + // Add struct enum-variant, with excess comma - e.g. { descr: &'static str, } + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* => $bitem:ident: UNIT [ ] ] + queue [{$( $qvar:ident: $qtyp:ty ),+ ,} $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] + buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ] + queue [$( $tail )*]); + }; + // Add braces and flush always on braces + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* + => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ] + queue [ {$( $qfuncs:tt )*} $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )* + $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {$( $qfuncs )*} ] + buf [ ] + queue [$( $tail )*]); + }; + // Flush buffer on double ident + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* + => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ] + queue [ $qitem:ident $( $tail:tt )*] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )* + $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {} ] + buf [ => $qitem : UNIT [ ] ] + queue [$( $tail )*]); + }; + // Flush buffer on end + (SORT [$( $def:tt )*] + items [$($( #[$imeta:meta] )* + => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*] + {$( $ifuncs:tt )*} )* ] + buf [$( #[$bmeta:meta] )* + => $bitem:ident: $bmode:tt [$( $bvar:ident: $btyp:ty ),*] ] + queue [ ] + ) => { + quick_error!(SORT [$( $def )*] + items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )* + $(#[$bmeta])* => $bitem: $bmode [$( $bvar:$btyp ),*] {} ] + buf [ ] + queue [ ]); + }; + // Public enum (Queue Empty) + (ENUM_DEFINITION [pub enum $name:ident $( #[$meta:meta] )*] + body [$($( #[$imeta:meta] )* + => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ] + queue [ ] + ) => { + #[allow(unknown_lints)] // no unused_doc_comments in older rust + #[allow(renamed_and_removed_lints)] + #[allow(unused_doc_comment)] + #[allow(unused_doc_comments)] + $(#[$meta])* + pub enum $name { + $( + $(#[$imeta])* + $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*, + )* + } + }; + // Private enum (Queue Empty) + (ENUM_DEFINITION [enum $name:ident $( #[$meta:meta] )*] + body [$($( #[$imeta:meta] )* + => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ] + queue [ ] + ) => { + #[allow(unknown_lints)] // no unused_doc_comments in older rust + #[allow(renamed_and_removed_lints)] + #[allow(unused_doc_comment)] + #[allow(unused_doc_comments)] + $(#[$meta])* + enum $name { + $( + $(#[$imeta])* + $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*, + )* + } + }; + // Unit variant + (ENUM_DEFINITION [$( $def:tt )*] + body [$($( #[$imeta:meta] )* + => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ] + queue [$( #[$qmeta:meta] )* + => $qitem:ident: UNIT [ ] $( $queue:tt )*] + ) => { + quick_error!(ENUM_DEFINITION [ $($def)* ] + body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )* + $( #[$qmeta] )* => $qitem () {} ] + queue [ $($queue)* ] + ); + }; + // Tuple variant + (ENUM_DEFINITION [$( $def:tt )*] + body [$($( #[$imeta:meta] )* + => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ] + queue [$( #[$qmeta:meta] )* + => $qitem:ident: TUPLE [$( $qvar:ident: $qtyp:ty ),+] $( $queue:tt )*] + ) => { + quick_error!(ENUM_DEFINITION [ $($def)* ] + body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )* + $( #[$qmeta] )* => $qitem (($( $qtyp ),*)) {} ] + queue [ $($queue)* ] + ); + }; + // Struct variant + (ENUM_DEFINITION [$( $def:tt )*] + body [$($( #[$imeta:meta] )* + => $iitem:ident ($(($( $ttyp:ty ),+))*) {$({$( $svar:ident: $styp:ty ),*})*} )* ] + queue [$( #[$qmeta:meta] )* + => $qitem:ident: STRUCT [$( $qvar:ident: $qtyp:ty ),*] $( $queue:tt )*] + ) => { + quick_error!(ENUM_DEFINITION [ $($def)* ] + body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )* + $( #[$qmeta] )* => $qitem () {{$( $qvar: $qtyp ),*}} ] + queue [ $($queue)* ] + ); + }; + (IMPLEMENTATIONS + $name:ident {$( + $item:ident: $imode:tt [$(#[$imeta:meta])*] [$( $var:ident: $typ:ty ),*] {$( $funcs:tt )*} + )*} + ) => { + #[allow(unused)] + #[allow(unknown_lints)] // no unused_doc_comments in older rust + #[allow(renamed_and_removed_lints)] + #[allow(unused_doc_comment)] + #[allow(unused_doc_comments)] + impl ::std::fmt::Display for $name { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) + -> ::std::fmt::Result + { + match *self { + $( + $(#[$imeta])* + quick_error!(ITEM_PATTERN + $name $item: $imode [$( ref $var ),*] + ) => { + let display_fn = quick_error!(FIND_DISPLAY_IMPL + $name $item: $imode + {$( $funcs )*}); + + display_fn(self, fmt) + } + )* + } + } + } + #[allow(unused)] + #[allow(unknown_lints)] // no unused_doc_comments in older rust + #[allow(renamed_and_removed_lints)] + #[allow(unused_doc_comment)] + #[allow(unused_doc_comments)] + impl ::std::error::Error for $name { + fn cause(&self) -> Option<&::std::error::Error> { + match *self { + $( + $(#[$imeta])* + quick_error!(ITEM_PATTERN + $name $item: $imode [$( ref $var ),*] + ) => { + quick_error!(FIND_CAUSE_IMPL + $item: $imode [$( $var ),*] + {$( $funcs )*}) + } + )* + } + } + } + $( + quick_error!(FIND_FROM_IMPL + $name $item: $imode [$( $var:$typ ),*] + {$( $funcs )*}); + )* + $( + quick_error!(FIND_CONTEXT_IMPL + $name $item: $imode [$( $var:$typ ),*] + {$( $funcs )*}); + )* + }; + (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt + { display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*} + ) => { + |quick_error!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| { write!(f, $( $exprs )*) } + }; + (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt + { display($pattern:expr) $( $tail:tt )*} + ) => { + |_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) } + }; + (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt + { display($pattern:expr, $( $exprs:tt )*) $( $tail:tt )*} + ) => { + |_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $( $exprs )*) } + }; + (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt + { $t:tt $( $tail:tt )*} + ) => { + quick_error!(FIND_DISPLAY_IMPL + $name $item: $imode + {$( $tail )*}) + }; + (FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt + { } + ) => { + |self_: &$name, f: &mut ::std::fmt::Formatter| { + write!(f, "{:?}", self_) + } + }; + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { description($expr:expr) $( $tail:tt )*} + ) => {}; + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { $t:tt $( $tail:tt )*} + ) => {}; + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { } + ) => {}; + (FIND_CAUSE_IMPL $item:ident: $imode:tt + [$( $var:ident ),*] + { cause($expr:expr) $( $tail:tt )*} + ) => { + Some($expr) + }; + (FIND_CAUSE_IMPL $item:ident: $imode:tt + [$( $var:ident ),*] + { $t:tt $( $tail:tt )*} + ) => { + quick_error!(FIND_CAUSE_IMPL + $item: $imode [$( $var ),*] + { $($tail)* }) + }; + (FIND_CAUSE_IMPL $item:ident: $imode:tt + [$( $var:ident ),*] + { } + ) => { + None + }; + // ----------------------------- FROM IMPL -------------------------- + (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt + [$( $var:ident: $typ:ty ),*] + { from() $( $tail:tt )*} + ) => { + $( + impl From<$typ> for $name { + fn from($var: $typ) -> $name { + $name::$item($var) + } + } + )* + quick_error!(FIND_FROM_IMPL + $name $item: $imode [$( $var:$typ ),*] + {$( $tail )*}); + }; + (FIND_FROM_IMPL $name:ident $item:ident: UNIT + [ ] + { from($ftyp:ty) $( $tail:tt )*} + ) => { + impl From<$ftyp> for $name { + fn from(_discarded_error: $ftyp) -> $name { + $name::$item + } + } + quick_error!(FIND_FROM_IMPL + $name $item: UNIT [ ] + {$( $tail )*}); + }; + (FIND_FROM_IMPL $name:ident $item:ident: TUPLE + [$( $var:ident: $typ:ty ),*] + { from($fvar:ident: $ftyp:ty) -> ($( $texpr:expr ),*) $( $tail:tt )*} + ) => { + impl From<$ftyp> for $name { + fn from($fvar: $ftyp) -> $name { + $name::$item($( $texpr ),*) + } + } + quick_error!(FIND_FROM_IMPL + $name $item: TUPLE [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_FROM_IMPL $name:ident $item:ident: STRUCT + [$( $var:ident: $typ:ty ),*] + { from($fvar:ident: $ftyp:ty) -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )*} + ) => { + impl From<$ftyp> for $name { + fn from($fvar: $ftyp) -> $name { + $name::$item { + $( $tvar: $texpr ),* + } + } + } + quick_error!(FIND_FROM_IMPL + $name $item: STRUCT [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt + [$( $var:ident: $typ:ty ),*] + { $t:tt $( $tail:tt )*} + ) => { + quick_error!(FIND_FROM_IMPL + $name $item: $imode [$( $var:$typ ),*] + {$( $tail )*} + ); + }; + (FIND_FROM_IMPL $name:ident $item:ident: $imode:tt + [$( $var:ident: $typ:ty ),*] + { } + ) => { + }; + // ----------------------------- CONTEXT IMPL -------------------------- + (FIND_CONTEXT_IMPL $name:ident $item:ident: TUPLE + [$( $var:ident: $typ:ty ),*] + { context($cvar:ident: AsRef<$ctyp:ty>, $fvar:ident: $ftyp:ty) + -> ($( $texpr:expr ),*) $( $tail:tt )* } + ) => { + impl<T: AsRef<$ctyp>> From<$crate::Context<T, $ftyp>> for $name { + fn from( + $crate::Context($cvar, $fvar): $crate::Context<T, $ftyp>) + -> $name + { + $name::$item($( $texpr ),*) + } + } + quick_error!(FIND_CONTEXT_IMPL + $name $item: TUPLE [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_CONTEXT_IMPL $name:ident $item:ident: TUPLE + [$( $var:ident: $typ:ty ),*] + { context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty) + -> ($( $texpr:expr ),*) $( $tail:tt )* } + ) => { + impl<'a> From<$crate::Context<$ctyp, $ftyp>> for $name { + fn from( + $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>) + -> $name + { + $name::$item($( $texpr ),*) + } + } + quick_error!(FIND_CONTEXT_IMPL + $name $item: TUPLE [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT + [$( $var:ident: $typ:ty ),*] + { context($cvar:ident: AsRef<$ctyp:ty>, $fvar:ident: $ftyp:ty) + -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )* } + ) => { + impl<T: AsRef<$ctyp>> From<$crate::Context<T, $ftyp>> for $name { + fn from( + $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>) + -> $name + { + $name::$item { + $( $tvar: $texpr ),* + } + } + } + quick_error!(FIND_CONTEXT_IMPL + $name $item: STRUCT [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT + [$( $var:ident: $typ:ty ),*] + { context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty) + -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )* } + ) => { + impl<'a> From<$crate::Context<$ctyp, $ftyp>> for $name { + fn from( + $crate::Context($cvar, $fvar): $crate::Context<$ctyp, $ftyp>) + -> $name + { + $name::$item { + $( $tvar: $texpr ),* + } + } + } + quick_error!(FIND_CONTEXT_IMPL + $name $item: STRUCT [$( $var:$typ ),*] + { $($tail)* }); + }; + (FIND_CONTEXT_IMPL $name:ident $item:ident: $imode:tt + [$( $var:ident: $typ:ty ),*] + { $t:tt $( $tail:tt )*} + ) => { + quick_error!(FIND_CONTEXT_IMPL + $name $item: $imode [$( $var:$typ ),*] + {$( $tail )*} + ); + }; + (FIND_CONTEXT_IMPL $name:ident $item:ident: $imode:tt + [$( $var:ident: $typ:ty ),*] + { } + ) => { + }; + // ----------------------------- ITEM IMPL -------------------------- + (ITEM_BODY $(#[$imeta:meta])* $item:ident: UNIT + ) => { }; + (ITEM_BODY $(#[$imeta:meta])* $item:ident: TUPLE + [$( $typ:ty ),*] + ) => { + ($( $typ ),*) + }; + (ITEM_BODY $(#[$imeta:meta])* $item:ident: STRUCT + [$( $var:ident: $typ:ty ),*] + ) => { + {$( $var:$typ ),*} + }; + (ITEM_PATTERN $name:ident $item:ident: UNIT [] + ) => { + $name::$item + }; + (ITEM_PATTERN $name:ident $item:ident: TUPLE + [$( ref $var:ident ),*] + ) => { + $name::$item ($( ref $var ),*) + }; + (ITEM_PATTERN $name:ident $item:ident: STRUCT + [$( ref $var:ident ),*] + ) => { + $name::$item {$( ref $var ),*} + }; + // This one should match all allowed sequences in "funcs" but not match + // anything else. + // This is to contrast FIND_* clauses which just find stuff they need and + // skip everything else completely + (ERROR_CHECK $imode:tt display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt display($pattern: expr) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt display($pattern: expr, $( $exprs:tt )*) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt description($expr:expr) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt cause($expr:expr) $($tail:tt)*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt from() $($tail:tt)*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt from($ftyp:ty) $($tail:tt)*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK TUPLE from($fvar:ident: $ftyp:ty) -> ($( $e:expr ),*) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK TUPLE $($tail)*); }; + (ERROR_CHECK STRUCT from($fvar:ident: $ftyp:ty) -> {$( $v:ident: $e:expr ),*} $( $tail:tt )*) + => { quick_error!(ERROR_CHECK STRUCT $($tail)*); }; + + (ERROR_CHECK TUPLE context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty) + -> ($( $e:expr ),*) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK TUPLE $($tail)*); }; + (ERROR_CHECK STRUCT context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty) + -> {$( $v:ident: $e:expr ),*} $( $tail:tt )*) + => { quick_error!(ERROR_CHECK STRUCT $($tail)*); }; + + (ERROR_CHECK $imode:tt ) => {}; + // Utility functions + (IDENT $ident:ident) => { $ident } +} + + +/// Generic context type +/// +/// Used mostly as a transport for `ResultExt::context` method +#[derive(Debug)] +pub struct Context<X, E>(pub X, pub E); + +/// Result extension trait adding a `context` method +pub trait ResultExt<T, E> { + /// The method is use to add context information to current operation + /// + /// The context data is then used in error constructor to store additional + /// information within error. For example, you may add a filename as a + /// context for file operation. See crate documentation for the actual + /// example. + fn context<X>(self, x: X) -> Result<T, Context<X, E>>; +} + +impl<T, E> ResultExt<T, E> for Result<T, E> { + fn context<X>(self, x: X) -> Result<T, Context<X, E>> { + self.map_err(|e| Context(x, e)) + } +} + + + +#[cfg(test)] +#[allow(deprecated)] +mod test { + use std::num::{ParseFloatError, ParseIntError}; + use std::str::Utf8Error; + use std::string::FromUtf8Error; + use std::error::Error; + use std::path::{Path, PathBuf}; + + use super::ResultExt; + + quick_error! { + #[derive(Debug)] + pub enum Bare { + One + Two + } + } + + #[test] + fn bare_item_direct() { + assert_eq!(format!("{}", Bare::One), "One".to_string()); + assert_eq!(format!("{:?}", Bare::One), "One".to_string()); + assert!(Bare::One.cause().is_none()); + } + #[test] + fn bare_item_trait() { + let err: &Error = &Bare::Two; + assert_eq!(format!("{}", err), "Two".to_string()); + assert_eq!(format!("{:?}", err), "Two".to_string()); + assert!(err.cause().is_none()); + } + + quick_error! { + #[derive(Debug)] + pub enum Wrapper wraps Wrapped { + One + Two(s: String) { + display("two: {}", s) + from() + } + } + } + + #[test] + fn wrapper() { + assert_eq!(format!("{}", Wrapper::from(Wrapped::One)), + "One".to_string()); + assert_eq!(format!("{}", + Wrapper::from(Wrapped::from(String::from("hello")))), + "two: hello".to_string()); + assert_eq!(format!("{:?}", Wrapper::from(Wrapped::One)), + "Wrapper(One)".to_string()); + } + + quick_error! { + #[derive(Debug, PartialEq)] + pub enum TupleWrapper { + /// ParseFloat Error + ParseFloatError(err: ParseFloatError) { + from() + display("parse float error: {err}", err=err) + cause(err) + } + Other(descr: &'static str) { + display("Error: {}", descr) + } + /// FromUtf8 Error + FromUtf8Error(err: Utf8Error, source: Vec<u8>) { + cause(err) + display(me) -> ("{desc} at index {pos}: {err}", desc="utf8 error", pos=err.valid_up_to(), err=err) + from(err: FromUtf8Error) -> (err.utf8_error().clone(), err.into_bytes()) + } + Discard { + from(&'static str) + } + Singleton { + display("Just a string") + } + } + } + + #[test] + fn tuple_wrapper_err() { + let cause = "one and a half times pi".parse::<f32>().unwrap_err(); + let err = TupleWrapper::ParseFloatError(cause.clone()); + assert_eq!(format!("{}", err), format!("parse float error: {}", cause)); + assert_eq!(format!("{:?}", err), format!("ParseFloatError({:?})", cause)); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); + } + + #[test] + fn tuple_wrapper_trait_str() { + let desc = "hello"; + let err: &Error = &TupleWrapper::Other(desc); + assert_eq!(format!("{}", err), format!("Error: {}", desc)); + assert_eq!(format!("{:?}", err), format!("Other({:?})", desc)); + assert!(err.cause().is_none()); + } + + #[test] + fn tuple_wrapper_trait_two_fields() { + let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err: &Error = &TupleWrapper::FromUtf8Error(cause.clone(), invalid_utf8.clone()); + assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc="utf8 error", pos=cause.valid_up_to(), cause=cause)); + assert_eq!(format!("{:?}", err), format!("FromUtf8Error({:?}, {:?})", cause, invalid_utf8)); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); + } + + #[test] + fn tuple_wrapper_from() { + let cause = "one and a half times pi".parse::<f32>().unwrap_err(); + let err = TupleWrapper::ParseFloatError(cause.clone()); + let err_from: TupleWrapper = From::from(cause); + assert_eq!(err_from, err); + } + + #[test] + fn tuple_wrapper_custom_from() { + let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err(); + let err = TupleWrapper::FromUtf8Error(cause.utf8_error().clone(), invalid_utf8); + let err_from: TupleWrapper = From::from(cause); + assert_eq!(err_from, err); + } + + #[test] + fn tuple_wrapper_discard() { + let err: TupleWrapper = From::from("hello"); + assert_eq!(format!("{}", err), format!("Discard")); + assert_eq!(format!("{:?}", err), format!("Discard")); + assert!(err.cause().is_none()); + } + + #[test] + fn tuple_wrapper_singleton() { + let err: TupleWrapper = TupleWrapper::Singleton; + assert_eq!(format!("{}", err), format!("Just a string")); + assert_eq!(format!("{:?}", err), format!("Singleton")); + assert!(err.cause().is_none()); + } + + quick_error! { + #[derive(Debug, PartialEq)] + pub enum StructWrapper { + // Utf8 Error + Utf8Error{ err: Utf8Error, hint: Option<&'static str> } { + cause(err) + display(me) -> ("{desc} at index {pos}: {err}", desc="utf8 error", pos=err.valid_up_to(), err=err) + from(err: Utf8Error) -> { err: err, hint: None } + } + // Utf8 Error + ExcessComma { descr: &'static str, } { + display("Error: {}", descr) + } + } + } + + #[test] + fn struct_wrapper_err() { + let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err: &Error = &StructWrapper::Utf8Error{ err: cause.clone(), hint: Some("nonsense") }; + assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc="utf8 error", pos=cause.valid_up_to(), cause=cause)); + assert_eq!(format!("{:?}", err), format!("Utf8Error {{ err: {:?}, hint: {:?} }}", cause, Some("nonsense"))); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); + } + + #[test] + fn struct_wrapper_struct_from() { + let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err = StructWrapper::Utf8Error{ err: cause.clone(), hint: None }; + let err_from: StructWrapper = From::from(cause); + assert_eq!(err_from, err); + } + + #[test] + fn struct_wrapper_excess_comma() { + let descr = "hello"; + let err = StructWrapper::ExcessComma { descr: descr }; + assert_eq!(format!("{}", err), format!("Error: {}", descr)); + assert_eq!(format!("{:?}", err), format!("ExcessComma {{ descr: {:?} }}", descr)); + assert!(err.cause().is_none()); + } + + quick_error! { + #[derive(Debug)] + pub enum ContextErr { + Float(src: String, err: ParseFloatError) { + context(s: &'a str, e: ParseFloatError) -> (s.to_string(), e) + display("Float error {:?}: {}", src, err) + } + Int { src: String, err: ParseIntError } { + context(s: &'a str, e: ParseIntError) + -> {src: s.to_string(), err: e} + display("Int error {:?}: {}", src, err) + } + Utf8(path: PathBuf, err: Utf8Error) { + context(p: AsRef<Path>, e: Utf8Error) + -> (p.as_ref().to_path_buf(), e) + display("Path error at {:?}: {}", path, err) + } + Utf8Str(s: String, err: ::std::io::Error) { + context(s: AsRef<str>, e: ::std::io::Error) + -> (s.as_ref().to_string(), e) + display("Str error {:?}: {}", s, err) + } + } + } + + #[test] + fn parse_float_error() { + fn parse_float(s: &str) -> Result<f32, ContextErr> { + Ok(try!(s.parse().context(s))) + } + assert_eq!(format!("{}", parse_float("12ab").unwrap_err()), + r#"Float error "12ab": invalid float literal"#); + } + + #[test] + fn parse_int_error() { + fn parse_int(s: &str) -> Result<i32, ContextErr> { + Ok(try!(s.parse().context(s))) + } + assert_eq!(format!("{}", parse_int("12.5").unwrap_err()), + r#"Int error "12.5": invalid digit found in string"#); + } + + #[test] + fn debug_context() { + fn parse_int(s: &str) -> i32 { + s.parse().context(s).unwrap() + } + assert_eq!(parse_int("12"), 12); + assert_eq!(format!("{:?}", "x".parse::<i32>().context("x")), + r#"Err(Context("x", ParseIntError { kind: InvalidDigit }))"#); + } + + #[test] + fn path_context() { + fn parse_utf<P: AsRef<Path>>(s: &[u8], p: P) + -> Result<(), ContextErr> + { + try!(::std::str::from_utf8(s).context(p)); + Ok(()) + } + let etext = parse_utf(b"a\x80\x80", "/etc").unwrap_err().to_string(); + assert!(etext.starts_with( + "Path error at \"/etc\": invalid utf-8")); + let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp")).unwrap_err() + .to_string(); + assert!(etext.starts_with( + "Path error at \"/tmp\": invalid utf-8")); + } + + #[test] + fn conditional_compilation() { + quick_error! { + #[allow(dead_code)] + #[derive(Debug)] + pub enum Test { + #[cfg(feature = "foo")] + Variant + } + } + } +} diff --git a/third_party/rust/quick-error/vagga.yaml b/third_party/rust/quick-error/vagga.yaml new file mode 100644 index 0000000000..71b9be44e5 --- /dev/null +++ b/third_party/rust/quick-error/vagga.yaml @@ -0,0 +1,36 @@ +commands: + + cargo: !Command + description: Run any cargo command + container: ubuntu + run: [cargo] + + test: !Command + description: Run unit tests + container: ubuntu + run: [cargo, test] + + _bulk: !Command + description: Run `bulk` command (for version bookkeeping) + container: ubuntu + run: [bulk] + +containers: + + ubuntu: + setup: + - !Ubuntu xenial + - !Install [ca-certificates, build-essential, vim] + + - !TarInstall + url: "https://static.rust-lang.org/dist/rust-1.16.0-x86_64-unknown-linux-gnu.tar.gz" + script: "./install.sh --prefix=/usr \ + --components=rustc,rust-std-x86_64-unknown-linux-gnu,cargo" + - &bulk !Tar + url: "https://github.com/tailhook/bulk/releases/download/v0.4.9/bulk-v0.4.9.tar.gz" + sha256: 23471a9986274bb4b7098c03e2eb7e1204171869b72c45385fcee1c64db2d111 + path: / + + environ: + HOME: /work/target + USER: pc |