sysexits-0.8.1/.cargo_vcs_info.json0000644000000001360000000000100127350ustar { "git": { "sha1": "d3408d22c8fd5bd7e7a9ddb2c2e9d53843df21c5" }, "path_in_vcs": "" }sysexits-0.8.1/Cargo.lock0000644000000323410000000000100107130ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "anstream" version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys", ] [[package]] name = "anstyle-wincon" version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", "windows-sys", ] [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "bit-set" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" dependencies = [ "bit-vec", ] [[package]] name = "bit-vec" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" dependencies = [ "clap_builder", "clap_derive", ] [[package]] name = "clap_builder" version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" dependencies = [ "anstream", "anstyle", "clap_lex", "strsim", ] [[package]] name = "clap_derive" version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck", "proc-macro2", "quote", "syn", ] [[package]] name = "clap_lex" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "colorchoice" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "errno" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys", ] [[package]] name = "fastrand" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "getrandom" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "is_terminal_polyfill" version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" [[package]] name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libm" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" [[package]] name = "linux-raw-sys" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", ] [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "proptest" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4c2511913b88df1637da85cc8d96ec8e43a3f8bb8ccb71ee1ac240d6f3df58d" dependencies = [ "bit-set", "bit-vec", "bitflags", "lazy_static", "num-traits", "rand", "rand_chacha", "rand_xorshift", "regex-syntax", "rusty-fork", "tempfile", "unarray", ] [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "rand" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", "rand_core", ] [[package]] name = "rand_chacha" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", ] [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom", ] [[package]] name = "rand_xorshift" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" dependencies = [ "rand_core", ] [[package]] name = "regex-syntax" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "rustix" version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "rusty-fork" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f" dependencies = [ "fnv", "quick-error", "tempfile", "wait-timeout", ] [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "structmeta" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e1575d8d40908d70f6fd05537266b90ae71b15dbbe7a8b7dffa2b759306d329" dependencies = [ "proc-macro2", "quote", "structmeta-derive", "syn", ] [[package]] name = "structmeta-derive" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "152a0b65a590ff6c3da95cabe2353ee04e6167c896b28e3b14478c2636c922fc" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "syn" version = "2.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "sysexits" version = "0.8.1" dependencies = [ "clap", "proptest", "test-strategy", ] [[package]] name = "tempfile" version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", "fastrand", "rustix", "windows-sys", ] [[package]] name = "test-strategy" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bf41af45e3f54cc184831d629d41d5b2bda8297e29c81add7ae4f362ed5e01b" dependencies = [ "proc-macro2", "quote", "structmeta", "syn", ] [[package]] name = "unarray" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "wait-timeout" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" dependencies = [ "libc", ] [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" [[package]] name = "windows_i686_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" sysexits-0.8.1/Cargo.toml0000644000000031370000000000100107370ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.74.0" name = "sysexits" version = "0.8.1" authors = ["Shun Sakai "] include = [ "/LICENSES", "/README.md", "/src", ] description = "The system exit codes as defined by " documentation = "https://docs.rs/sysexits" readme = "README.md" keywords = [ "exitcode", "sysexits", ] categories = [ "command-line-interface", "no-std", "os", ] license = "Apache-2.0 OR MIT" repository = "https://github.com/sorairolake/sysexits-rs" [package.metadata.docs.rs] all-features = true rustdoc-args = [ "--cfg", "docsrs", ] [[example]] name = "cat" path = "examples/cat.rs" required-features = ["default"] [[example]] name = "cmp" path = "examples/cmp.rs" required-features = ["default"] [[example]] name = "isutf8" path = "examples/isutf8.rs" required-features = ["default"] [dependencies] [dev-dependencies.clap] version = "4.5.8" features = ["derive"] [dev-dependencies.proptest] version = "1.5.0" [dev-dependencies.test-strategy] version = "0.4.0" [features] default = ["std"] extended_io_error = ["std"] nightly = ["extended_io_error"] std = [] sysexits-0.8.1/Cargo.toml.orig000064400000000000000000000024121046102023000144130ustar 00000000000000# SPDX-FileCopyrightText: 2022 Shun Sakai # SPDX-FileCopyrightText: 2023 zSchoen # # SPDX-License-Identifier: Apache-2.0 OR MIT [package] name = "sysexits" version = "0.8.1" authors = ["Shun Sakai "] edition = "2021" rust-version = "1.74.0" description = "The system exit codes as defined by " documentation = "https://docs.rs/sysexits" readme = "README.md" repository = "https://github.com/sorairolake/sysexits-rs" license = "Apache-2.0 OR MIT" keywords = ["exitcode", "sysexits"] categories = ["command-line-interface", "no-std", "os"] include = ["/LICENSES", "/README.md", "/src"] [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[example]] name = "cat" path = "examples/cat.rs" required-features = ["default"] [[example]] name = "cmp" path = "examples/cmp.rs" required-features = ["default"] [[example]] name = "isutf8" path = "examples/isutf8.rs" required-features = ["default"] [dependencies] [dev-dependencies] clap = { version = "4.5.8", features = ["derive"] } proptest = "1.5.0" test-strategy = "0.4.0" [features] default = ["std"] std = [] nightly = ["extended_io_error"] extended_io_error = ["std"] sysexits-0.8.1/LICENSES/Apache-2.0.txt000064400000000000000000000261351046102023000151600ustar 00000000000000 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. sysexits-0.8.1/LICENSES/MIT.txt000064400000000000000000000020531046102023000141240ustar 00000000000000MIT License Copyright (c) 2022 Shun Sakai 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. sysexits-0.8.1/README.md000064400000000000000000000056651046102023000130200ustar 00000000000000 # sysexits-rs [![CI][ci-badge]][ci-url] [![Version][version-badge]][version-url] ![MSRV][msrv-badge] [![Docs][docs-badge]][docs-url] ![License][license-badge] **sysexits-rs** ([`sysexits`][version-url]) is a library that provides the system exit code constants as defined by [``]. This library implements the [`Termination`] trait, so this can be returned from the `main` function. ## Usage Add this to your `Cargo.toml`: ```toml [dependencies] sysexits = "0.8.1" ``` ### Example ```rust use std::str; use sysexits::ExitCode; fn main() -> ExitCode { let bytes = [0xf0, 0x9f, 0x92, 0x96]; match str::from_utf8(&bytes) { Ok(string) => { println!("{string}"); ExitCode::Ok } Err(err) => { eprintln!("{err}"); ExitCode::DataErr } } } ``` ### Crate features #### `std` Enables features that depend on the standard library. This is enabled by default. #### `nightly` Enables features that depend on the nightly Rust. #### `extended_io_error` Enables features that depend on the `io_error_more` feature. This also enables `std`. This is implied by `nightly`. ### `no_std` support This supports `no_std` mode. Disables the `default` feature to enable this. ### Documentation See the [documentation][docs-url] for more details. ## Minimum supported Rust version The minimum supported Rust version (MSRV) of this library is v1.74.0. ## Changelog Please see [CHANGELOG.adoc]. ## Contributing Please see [CONTRIBUTING.adoc]. ## License Copyright © 2022–2024 Shun Sakai and other contributors (see [AUTHORS.adoc]) This library is distributed under the terms of either the _Apache License 2.0_ or the _MIT License_. This project is compliant with version 3.0 of the [_REUSE Specification_]. See copyright notices of individual files for more details on copyright and licensing information. [ci-badge]: https://img.shields.io/github/actions/workflow/status/sorairolake/sysexits-rs/CI.yaml?branch=develop&style=for-the-badge&logo=github&label=CI [ci-url]: https://github.com/sorairolake/sysexits-rs/actions?query=branch%3Adevelop+workflow%3ACI++ [version-badge]: https://img.shields.io/crates/v/sysexits?style=for-the-badge&logo=rust [version-url]: https://crates.io/crates/sysexits [msrv-badge]: https://img.shields.io/crates/msrv/sysexits?style=for-the-badge&logo=rust [docs-badge]: https://img.shields.io/docsrs/sysexits?style=for-the-badge&logo=docsdotrs&label=Docs.rs [docs-url]: https://docs.rs/sysexits [license-badge]: https://img.shields.io/crates/l/sysexits?style=for-the-badge [``]: https://man.openbsd.org/sysexits [`Termination`]: https://doc.rust-lang.org/std/process/trait.Termination.html [CHANGELOG.adoc]: CHANGELOG.adoc [CONTRIBUTING.adoc]: CONTRIBUTING.adoc [AUTHORS.adoc]: AUTHORS.adoc [_REUSE Specification_]: https://reuse.software/spec/ sysexits-0.8.1/src/error.rs000064400000000000000000000121001046102023000140050ustar 00000000000000// SPDX-FileCopyrightText: 2023 Kevin Matthes // SPDX-FileCopyrightText: 2023 Shun Sakai // // SPDX-License-Identifier: Apache-2.0 OR MIT //! Error types for this crate. use core::fmt; /// The error type indicating that [`ExitCode`](crate::ExitCode) was out of /// range. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[allow(clippy::module_name_repetitions)] pub struct ExitCodeRangeError; impl fmt::Display for ExitCodeRangeError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "value is out of range for `ExitCode`") } } #[cfg(feature = "std")] impl std::error::Error for ExitCodeRangeError {} #[cfg(feature = "std")] /// An error which can be returned when converting an /// [`ExitCode`](crate::ExitCode) from an /// [`ExitStatus`](std::process::ExitStatus). #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[allow(clippy::module_name_repetitions)] pub struct TryFromExitStatusError(Option); #[cfg(feature = "std")] impl TryFromExitStatusError { #[inline] pub(crate) const fn new(code: Option) -> Self { Self(code) } /// Returns the corresponding exit code for this error. /// /// Returns [`None`] if the process was terminated by a signal. #[must_use] #[inline] pub const fn code(&self) -> Option { self.0 } } #[cfg(feature = "std")] impl fmt::Display for TryFromExitStatusError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(code) = self.code() { write!(f, "invalid exit code `{code}`") } else { write!(f, "exit code is unknown") } } } #[cfg(feature = "std")] impl std::error::Error for TryFromExitStatusError {} #[cfg(test)] mod tests { use super::*; #[test] fn clone_exit_code_range_error() { assert_eq!(ExitCodeRangeError.clone(), ExitCodeRangeError); } #[test] fn copy_exit_code_range_error() { let a = ExitCodeRangeError; let b = a; assert_eq!(a, b); } #[test] fn debug_exit_code_range_error() { assert_eq!(format!("{ExitCodeRangeError:?}"), "ExitCodeRangeError"); } #[test] fn exit_code_range_error_equality() { assert_eq!(ExitCodeRangeError, ExitCodeRangeError); } #[test] fn display_exit_code_range_error() { assert_eq!( format!("{ExitCodeRangeError}"), "value is out of range for `ExitCode`" ); } #[cfg(feature = "std")] #[test] fn source_exit_code_range_error() { use std::error::Error; assert!(ExitCodeRangeError.source().is_none()); } #[cfg(feature = "std")] #[test] fn clone_try_from_exit_status_error() { assert_eq!( TryFromExitStatusError::new(Some(1)).clone(), TryFromExitStatusError::new(Some(1)) ); assert_eq!( TryFromExitStatusError::new(None).clone(), TryFromExitStatusError::new(None) ); } #[cfg(feature = "std")] #[test] fn copy_try_from_exit_status_error() { { let a = TryFromExitStatusError::new(Some(1)); let b = a; assert_eq!(a, b); } { let a = TryFromExitStatusError::new(None); let b = a; assert_eq!(a, b); } } #[cfg(feature = "std")] #[test] fn debug_try_from_exit_status_error() { assert_eq!( format!("{:?}", TryFromExitStatusError::new(Some(1))), "TryFromExitStatusError(Some(1))" ); assert_eq!( format!("{:?}", TryFromExitStatusError::new(None)), "TryFromExitStatusError(None)" ); } #[cfg(feature = "std")] #[test] fn try_from_exit_status_error_equality() { assert_eq!( TryFromExitStatusError::new(Some(1)), TryFromExitStatusError::new(Some(1)) ); assert_ne!( TryFromExitStatusError::new(Some(1)), TryFromExitStatusError::new(None) ); assert_ne!( TryFromExitStatusError::new(None), TryFromExitStatusError::new(Some(1)) ); assert_eq!( TryFromExitStatusError::new(None), TryFromExitStatusError::new(None) ); } #[cfg(feature = "std")] #[test] fn code_try_from_exit_status_error() { assert_eq!(TryFromExitStatusError::new(Some(1)).code(), Some(1)); assert_eq!(TryFromExitStatusError::new(None).code(), None); } #[cfg(feature = "std")] #[test] fn display_try_from_exit_status_error() { assert_eq!( format!("{}", TryFromExitStatusError::new(Some(1))), "invalid exit code `1`" ); assert_eq!( format!("{}", TryFromExitStatusError::new(None)), "exit code is unknown" ); } #[cfg(feature = "std")] #[test] fn source_try_from_exit_status_error() { use std::error::Error; assert!(TryFromExitStatusError::new(Some(1)).source().is_none()); assert!(TryFromExitStatusError::new(None).source().is_none()); } } sysexits-0.8.1/src/exit_code/convert.rs000064400000000000000000001141751046102023000163160ustar 00000000000000// SPDX-FileCopyrightText: 2022 Shun Sakai // SPDX-FileCopyrightText: 2023 Kevin Matthes // SPDX-FileCopyrightText: 2023 zSchoen // // SPDX-License-Identifier: Apache-2.0 OR MIT //! Implementations of conversions between [`ExitCode`] and other types. use crate::error::ExitCodeRangeError; use super::ExitCode; macro_rules! impl_from_exit_code_to_integer { ($T:ty, $ok:expr, $usage:expr) => { impl From for $T { /// Converts an `ExitCode` into the raw underlying integer value. /// /// The resulting value is `0` or `64..=78`. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # #[doc = $ok] #[doc = $usage] /// ``` #[inline] fn from(code: ExitCode) -> Self { code as Self } } }; ($T:ty) => { impl_from_exit_code_to_integer!( $T, concat!("assert_eq!(", stringify!($T), "::from(ExitCode::Ok), 0);"), concat!( "assert_eq!(", stringify!($T), "::from(ExitCode::Usage), 64);" ) ); }; } impl_from_exit_code_to_integer!(i8); impl_from_exit_code_to_integer!(i16); impl_from_exit_code_to_integer!(i32); impl_from_exit_code_to_integer!(i64); impl_from_exit_code_to_integer!(i128); impl_from_exit_code_to_integer!(isize); impl_from_exit_code_to_integer!(u8); impl_from_exit_code_to_integer!(u16); impl_from_exit_code_to_integer!(u32); impl_from_exit_code_to_integer!(u64); impl_from_exit_code_to_integer!(u128); impl_from_exit_code_to_integer!(usize); #[cfg(feature = "std")] impl From for std::process::ExitCode { /// Converts an `sysexits::ExitCode` into an [`std::process::ExitCode`]. #[inline] fn from(code: ExitCode) -> Self { use std::process::Termination; code.report() } } macro_rules! impl_try_from_integer_to_exit_code { ($T:ty, $ok:expr, $usage:expr, $err:expr) => { impl TryFrom<$T> for ExitCode { type Error = ExitCodeRangeError; /// Converts an integer value into an `ExitCode`. /// /// # Errors /// /// Returns [`Err`] if `value` is not `0` or `64..=78`. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # #[doc = $ok] #[doc = $usage] /// #[doc = $err] /// ``` #[inline] fn try_from(value: $T) -> core::result::Result { match value { 0 => Ok(Self::Ok), 64 => Ok(Self::Usage), 65 => Ok(Self::DataErr), 66 => Ok(Self::NoInput), 67 => Ok(Self::NoUser), 68 => Ok(Self::NoHost), 69 => Ok(Self::Unavailable), 70 => Ok(Self::Software), 71 => Ok(Self::OsErr), 72 => Ok(Self::OsFile), 73 => Ok(Self::CantCreat), 74 => Ok(Self::IoErr), 75 => Ok(Self::TempFail), 76 => Ok(Self::Protocol), 77 => Ok(Self::NoPerm), 78 => Ok(Self::Config), _ => Err(ExitCodeRangeError), } } } }; ($T:ty) => { impl_try_from_integer_to_exit_code!( $T, concat!( "assert_eq!(ExitCode::try_from(0_", stringify!($T), ").unwrap(), ExitCode::Ok);" ), concat!( "assert_eq!(ExitCode::try_from(64_", stringify!($T), ").unwrap(), ExitCode::Usage);" ), concat!( "assert!(ExitCode::try_from(79_", stringify!($T), ").is_err());" ) ); }; } impl_try_from_integer_to_exit_code!(i8); impl_try_from_integer_to_exit_code!(i16); impl_try_from_integer_to_exit_code!(i32); impl_try_from_integer_to_exit_code!(i64); impl_try_from_integer_to_exit_code!(i128); impl_try_from_integer_to_exit_code!(isize); impl_try_from_integer_to_exit_code!(u8); impl_try_from_integer_to_exit_code!(u16); impl_try_from_integer_to_exit_code!(u32); impl_try_from_integer_to_exit_code!(u64); impl_try_from_integer_to_exit_code!(u128); impl_try_from_integer_to_exit_code!(usize); #[cfg(feature = "std")] impl From for ExitCode { /// Converts an [`Error`](std::io::Error) into an `ExitCode`. /// /// # Examples /// /// ``` /// # use std::io::{Error, ErrorKind}; /// # /// # use sysexits::ExitCode; /// # /// assert_eq!( /// ExitCode::from(Error::from(ErrorKind::NotFound)), /// ExitCode::NoInput /// ); /// ``` fn from(error: std::io::Error) -> Self { error.kind().into() } } #[cfg(feature = "std")] impl From for ExitCode { /// Converts an [`ErrorKind`](std::io::ErrorKind) into an `ExitCode`. /// /// # Examples /// /// ``` /// # use std::io; /// # /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::from(io::ErrorKind::NotFound), ExitCode::NoInput); /// ``` fn from(kind: std::io::ErrorKind) -> Self { use std::io::ErrorKind; match kind { ErrorKind::NotFound => Self::NoInput, ErrorKind::PermissionDenied => Self::NoPerm, ErrorKind::ConnectionRefused | ErrorKind::OutOfMemory => Self::OsErr, ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::BrokenPipe | ErrorKind::TimedOut | ErrorKind::Interrupted => Self::TempFail, #[cfg(feature = "extended_io_error")] ErrorKind::HostUnreachable | ErrorKind::NetworkUnreachable => Self::NoHost, ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable => Self::Unavailable, #[cfg(feature = "extended_io_error")] ErrorKind::NetworkDown => Self::Unavailable, ErrorKind::AlreadyExists => Self::CantCreat, ErrorKind::WouldBlock | ErrorKind::Unsupported => Self::Protocol, #[cfg(feature = "extended_io_error")] ErrorKind::ReadOnlyFilesystem => Self::CantCreat, ErrorKind::InvalidInput | ErrorKind::InvalidData => Self::DataErr, ErrorKind::WriteZero | ErrorKind::UnexpectedEof => Self::Software, _ => Self::IoErr, } } } #[cfg(feature = "std")] impl TryFrom for ExitCode { type Error = crate::error::TryFromExitStatusError; /// Converts an [`ExitStatus`](std::process::ExitStatus) into an `ExitCode`. /// /// # Errors /// /// Returns [`Err`] if any of the following are true: /// /// - The exit code is not `0` or `64..=78`. /// - The exit code is unknown (e.g., the process was terminated by a /// signal). fn try_from(status: std::process::ExitStatus) -> std::result::Result { match status.code() { Some(0) => Ok(Self::Ok), Some(64) => Ok(Self::Usage), Some(65) => Ok(Self::DataErr), Some(66) => Ok(Self::NoInput), Some(67) => Ok(Self::NoUser), Some(68) => Ok(Self::NoHost), Some(69) => Ok(Self::Unavailable), Some(70) => Ok(Self::Software), Some(71) => Ok(Self::OsErr), Some(72) => Ok(Self::OsFile), Some(73) => Ok(Self::CantCreat), Some(74) => Ok(Self::IoErr), Some(75) => Ok(Self::TempFail), Some(76) => Ok(Self::Protocol), Some(77) => Ok(Self::NoPerm), Some(78) => Ok(Self::Config), Some(code) => Err(Self::Error::new(Some(code))), None => Err(Self::Error::new(None)), } } } #[cfg(test)] mod tests { use super::*; #[cfg(all(feature = "std", unix))] fn get_exit_status(status: i32) -> std::process::ExitStatus { use std::process::Command; Command::new("sh") .arg("-c") .arg(format!("exit {status}")) .status() .unwrap() } #[cfg(all(feature = "std", windows))] fn get_exit_status(status: u32) -> std::process::ExitStatus { use std::process::Command; Command::new("cmd") .arg("/c") .arg(format!("exit {status}")) .status() .unwrap() } macro_rules! test_from_exit_code_to_integer { ($T:ty, $name:ident) => { #[test] fn $name() { assert_eq!(<$T>::from(ExitCode::Ok), 0); assert_eq!(<$T>::from(ExitCode::Usage), 64); assert_eq!(<$T>::from(ExitCode::DataErr), 65); assert_eq!(<$T>::from(ExitCode::NoInput), 66); assert_eq!(<$T>::from(ExitCode::NoUser), 67); assert_eq!(<$T>::from(ExitCode::NoHost), 68); assert_eq!(<$T>::from(ExitCode::Unavailable), 69); assert_eq!(<$T>::from(ExitCode::Software), 70); assert_eq!(<$T>::from(ExitCode::OsErr), 71); assert_eq!(<$T>::from(ExitCode::OsFile), 72); assert_eq!(<$T>::from(ExitCode::CantCreat), 73); assert_eq!(<$T>::from(ExitCode::IoErr), 74); assert_eq!(<$T>::from(ExitCode::TempFail), 75); assert_eq!(<$T>::from(ExitCode::Protocol), 76); assert_eq!(<$T>::from(ExitCode::NoPerm), 77); assert_eq!(<$T>::from(ExitCode::Config), 78); } }; } test_from_exit_code_to_integer!(i8, from_exit_code_to_i8); test_from_exit_code_to_integer!(i16, from_exit_code_to_i16); test_from_exit_code_to_integer!(i32, from_exit_code_to_i32); test_from_exit_code_to_integer!(i64, from_exit_code_to_i64); test_from_exit_code_to_integer!(i128, from_exit_code_to_i128); test_from_exit_code_to_integer!(isize, from_exit_code_to_isize); test_from_exit_code_to_integer!(u8, from_exit_code_to_u8); test_from_exit_code_to_integer!(u16, from_exit_code_to_u16); test_from_exit_code_to_integer!(u32, from_exit_code_to_u32); test_from_exit_code_to_integer!(u64, from_exit_code_to_u64); test_from_exit_code_to_integer!(u128, from_exit_code_to_u128); test_from_exit_code_to_integer!(usize, from_exit_code_to_usize); #[cfg(feature = "std")] #[test] fn from_exit_code_to_process_exit_code() { assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Ok)), format!("{:?}", std::process::ExitCode::from(0)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Usage)), format!("{:?}", std::process::ExitCode::from(64)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::DataErr)), format!("{:?}", std::process::ExitCode::from(65)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::NoInput)), format!("{:?}", std::process::ExitCode::from(66)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::NoUser)), format!("{:?}", std::process::ExitCode::from(67)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::NoHost)), format!("{:?}", std::process::ExitCode::from(68)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Unavailable)), format!("{:?}", std::process::ExitCode::from(69)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Software)), format!("{:?}", std::process::ExitCode::from(70)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::OsErr)), format!("{:?}", std::process::ExitCode::from(71)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::OsFile)), format!("{:?}", std::process::ExitCode::from(72)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::CantCreat)), format!("{:?}", std::process::ExitCode::from(73)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::IoErr)), format!("{:?}", std::process::ExitCode::from(74)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::TempFail)), format!("{:?}", std::process::ExitCode::from(75)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Protocol)), format!("{:?}", std::process::ExitCode::from(76)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::NoPerm)), format!("{:?}", std::process::ExitCode::from(77)) ); assert_eq!( format!("{:?}", std::process::ExitCode::from(ExitCode::Config)), format!("{:?}", std::process::ExitCode::from(78)) ); } macro_rules! test_try_from_integer_to_exit_code { ($T:ty, $name:ident) => { #[test] fn $name() { assert_eq!(ExitCode::try_from(0 as $T).unwrap(), ExitCode::Ok); assert_eq!(ExitCode::try_from(64 as $T).unwrap(), ExitCode::Usage); assert_eq!(ExitCode::try_from(65 as $T).unwrap(), ExitCode::DataErr); assert_eq!(ExitCode::try_from(66 as $T).unwrap(), ExitCode::NoInput); assert_eq!(ExitCode::try_from(67 as $T).unwrap(), ExitCode::NoUser); assert_eq!(ExitCode::try_from(68 as $T).unwrap(), ExitCode::NoHost); assert_eq!(ExitCode::try_from(69 as $T).unwrap(), ExitCode::Unavailable); assert_eq!(ExitCode::try_from(70 as $T).unwrap(), ExitCode::Software); assert_eq!(ExitCode::try_from(71 as $T).unwrap(), ExitCode::OsErr); assert_eq!(ExitCode::try_from(72 as $T).unwrap(), ExitCode::OsFile); assert_eq!(ExitCode::try_from(73 as $T).unwrap(), ExitCode::CantCreat); assert_eq!(ExitCode::try_from(74 as $T).unwrap(), ExitCode::IoErr); assert_eq!(ExitCode::try_from(75 as $T).unwrap(), ExitCode::TempFail); assert_eq!(ExitCode::try_from(76 as $T).unwrap(), ExitCode::Protocol); assert_eq!(ExitCode::try_from(77 as $T).unwrap(), ExitCode::NoPerm); assert_eq!(ExitCode::try_from(78 as $T).unwrap(), ExitCode::Config); } }; } test_try_from_integer_to_exit_code!(i8, try_from_i8_to_exit_code); test_try_from_integer_to_exit_code!(i16, try_from_i16_to_exit_code); test_try_from_integer_to_exit_code!(i32, try_from_i32_to_exit_code); test_try_from_integer_to_exit_code!(i64, try_from_i64_to_exit_code); test_try_from_integer_to_exit_code!(i128, try_from_i128_to_exit_code); test_try_from_integer_to_exit_code!(isize, try_from_isize_to_exit_code); test_try_from_integer_to_exit_code!(u8, try_from_u8_to_exit_code); test_try_from_integer_to_exit_code!(u16, try_from_u16_to_exit_code); test_try_from_integer_to_exit_code!(u32, try_from_u32_to_exit_code); test_try_from_integer_to_exit_code!(u64, try_from_u64_to_exit_code); test_try_from_integer_to_exit_code!(u128, try_from_u128_to_exit_code); test_try_from_integer_to_exit_code!(usize, try_from_usize_to_exit_code); macro_rules! test_try_from_integer_to_exit_code_when_out_of_range { ($T:ty, $name:ident) => { #[test] fn $name() { assert_eq!( ExitCode::try_from(79 as $T).unwrap_err(), ExitCodeRangeError ); } }; } test_try_from_integer_to_exit_code_when_out_of_range!( i8, try_from_i8_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( i16, try_from_i16_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( i32, try_from_i32_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( i64, try_from_i64_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( i128, try_from_i128_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( isize, try_from_isize_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( u8, try_from_u8_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( u16, try_from_u16_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( u32, try_from_u32_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( u64, try_from_u64_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( u128, try_from_u128_to_exit_code_when_out_of_range ); test_try_from_integer_to_exit_code_when_out_of_range!( usize, try_from_usize_to_exit_code_when_out_of_range ); macro_rules! test_try_from_integer_to_exit_code_when_negative_integer_roundtrip { ($T:ty, $name:ident) => { #[cfg(feature = "std")] #[test_strategy::proptest] fn $name(#[strategy(..<$T>::default())] v: $T) { use proptest::prop_assert_eq; prop_assert_eq!(ExitCode::try_from(v).unwrap_err(), ExitCodeRangeError); } }; } test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( i8, try_from_i8_to_exit_code_when_negative_i8_roundtrip ); test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( i16, try_from_i16_to_exit_code_when_negative_i16_roundtrip ); test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( i32, try_from_i32_to_exit_code_when_negative_i32_roundtrip ); test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( i64, try_from_i64_to_exit_code_when_negative_i64_roundtrip ); test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( i128, try_from_i128_to_exit_code_when_negative_i128_roundtrip ); test_try_from_integer_to_exit_code_when_negative_integer_roundtrip!( isize, try_from_isize_to_exit_code_when_negative_isize_roundtrip ); macro_rules! test_try_from_integer_to_exit_code_when_middle_integer_roundtrip { ($T:ty, $name:ident) => { #[cfg(feature = "std")] #[test_strategy::proptest] fn $name(#[strategy(1..(64 as $T))] v: $T) { use proptest::prop_assert_eq; prop_assert_eq!(ExitCode::try_from(v).unwrap_err(), ExitCodeRangeError); } }; } test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( i8, try_from_i8_to_exit_code_when_middle_i8_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( i16, try_from_i16_to_exit_code_when_middle_i16_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( i32, try_from_i32_to_exit_code_when_middle_i32_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( i64, try_from_i64_to_exit_code_when_middle_i64_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( i128, try_from_i128_to_exit_code_when_middle_i128_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( isize, try_from_isize_to_exit_code_when_middle_isize_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( u8, try_from_u8_to_exit_code_when_middle_u8_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( u16, try_from_u16_to_exit_code_when_middle_u16_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( u32, try_from_u32_to_exit_code_when_middle_u32_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( u64, try_from_u64_to_exit_code_when_middle_u64_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( u128, try_from_u128_to_exit_code_when_middle_u128_roundtrip ); test_try_from_integer_to_exit_code_when_middle_integer_roundtrip!( usize, try_from_usize_to_exit_code_when_middle_usize_roundtrip ); macro_rules! test_try_from_integer_to_exit_code_when_positive_integer_roundtrip { ($T:ty, $name:ident) => { #[cfg(feature = "std")] #[test_strategy::proptest] fn $name(#[strategy((79 as $T)..)] v: $T) { use proptest::prop_assert_eq; prop_assert_eq!(ExitCode::try_from(v).unwrap_err(), ExitCodeRangeError); } }; } test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( i8, try_from_i8_to_exit_code_when_positive_i8_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( i16, try_from_i16_to_exit_code_when_positive_i16_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( i32, try_from_i32_to_exit_code_when_positive_i32_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( i64, try_from_i64_to_exit_code_when_positive_i64_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( i128, try_from_i128_to_exit_code_when_positive_i128_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( isize, try_from_isize_to_exit_code_when_positive_isize_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( u8, try_from_u8_to_exit_code_when_positive_u8_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( u16, try_from_u16_to_exit_code_when_positive_u16_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( u32, try_from_u32_to_exit_code_when_positive_u32_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( u64, try_from_u64_to_exit_code_when_positive_u64_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( u128, try_from_u128_to_exit_code_when_positive_u128_roundtrip ); test_try_from_integer_to_exit_code_when_positive_integer_roundtrip!( usize, try_from_usize_to_exit_code_when_positive_usize_roundtrip ); #[cfg(feature = "std")] #[test] #[allow(clippy::too_many_lines)] fn from_io_error_to_exit_code() { use std::io::{Error, ErrorKind}; assert_eq!( ExitCode::from(Error::from(ErrorKind::NotFound)), ExitCode::NoInput ); assert_eq!( ExitCode::from(Error::from(ErrorKind::PermissionDenied)), ExitCode::NoPerm ); assert_eq!( ExitCode::from(Error::from(ErrorKind::ConnectionRefused)), ExitCode::OsErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::ConnectionReset)), ExitCode::TempFail ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::HostUnreachable)), ExitCode::NoHost ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::NetworkUnreachable)), ExitCode::NoHost ); assert_eq!( ExitCode::from(Error::from(ErrorKind::ConnectionAborted)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Error::from(ErrorKind::NotConnected)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Error::from(ErrorKind::AddrInUse)), ExitCode::Unavailable ); assert_eq!( ExitCode::from(Error::from(ErrorKind::AddrNotAvailable)), ExitCode::Unavailable ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::NetworkDown)), ExitCode::Unavailable ); assert_eq!( ExitCode::from(Error::from(ErrorKind::BrokenPipe)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Error::from(ErrorKind::AlreadyExists)), ExitCode::CantCreat ); assert_eq!( ExitCode::from(Error::from(ErrorKind::WouldBlock)), ExitCode::Protocol ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::NotADirectory)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::IsADirectory)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::DirectoryNotEmpty)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::ReadOnlyFilesystem)), ExitCode::CantCreat ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::FilesystemLoop)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::StaleNetworkFileHandle)), ExitCode::IoErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::InvalidInput)), ExitCode::DataErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::InvalidData)), ExitCode::DataErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::TimedOut)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Error::from(ErrorKind::WriteZero)), ExitCode::Software ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::StorageFull)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::NotSeekable)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::FilesystemQuotaExceeded)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::FileTooLarge)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::ResourceBusy)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::ExecutableFileBusy)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::Deadlock)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::CrossesDevices)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::TooManyLinks)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::InvalidFilename)), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(Error::from(ErrorKind::ArgumentListTooLong)), ExitCode::IoErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::Interrupted)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Error::from(ErrorKind::Unsupported)), ExitCode::Protocol ); assert_eq!( ExitCode::from(Error::from(ErrorKind::UnexpectedEof)), ExitCode::Software ); assert_eq!( ExitCode::from(Error::from(ErrorKind::OutOfMemory)), ExitCode::OsErr ); assert_eq!( ExitCode::from(Error::from(ErrorKind::Other)), ExitCode::IoErr ); } #[cfg(feature = "std")] #[test] #[allow(clippy::cognitive_complexity, clippy::too_many_lines)] fn from_io_error_kind_to_exit_code() { use std::io; assert_eq!(ExitCode::from(io::ErrorKind::NotFound), ExitCode::NoInput); assert_eq!( ExitCode::from(io::ErrorKind::PermissionDenied), ExitCode::NoPerm ); assert_eq!( ExitCode::from(io::ErrorKind::ConnectionRefused), ExitCode::OsErr ); assert_eq!( ExitCode::from(io::ErrorKind::ConnectionReset), ExitCode::TempFail ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::HostUnreachable), ExitCode::NoHost ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::NetworkUnreachable), ExitCode::NoHost ); assert_eq!( ExitCode::from(io::ErrorKind::ConnectionAborted), ExitCode::TempFail ); assert_eq!( ExitCode::from(io::ErrorKind::NotConnected), ExitCode::TempFail ); assert_eq!( ExitCode::from(io::ErrorKind::AddrInUse), ExitCode::Unavailable ); assert_eq!( ExitCode::from(io::ErrorKind::AddrNotAvailable), ExitCode::Unavailable ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::NetworkDown), ExitCode::Unavailable ); assert_eq!( ExitCode::from(io::ErrorKind::BrokenPipe), ExitCode::TempFail ); assert_eq!( ExitCode::from(io::ErrorKind::AlreadyExists), ExitCode::CantCreat ); assert_eq!( ExitCode::from(io::ErrorKind::WouldBlock), ExitCode::Protocol ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::NotADirectory), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::IsADirectory), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::DirectoryNotEmpty), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::ReadOnlyFilesystem), ExitCode::CantCreat ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::FilesystemLoop), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::StaleNetworkFileHandle), ExitCode::IoErr ); assert_eq!( ExitCode::from(io::ErrorKind::InvalidInput), ExitCode::DataErr ); assert_eq!( ExitCode::from(io::ErrorKind::InvalidData), ExitCode::DataErr ); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::TimedOut), ExitCode::TempFail); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::WriteZero), ExitCode::Software); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::StorageFull), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::NotSeekable), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::FilesystemQuotaExceeded), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::FileTooLarge), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::ResourceBusy), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::ExecutableFileBusy), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::Deadlock), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::CrossesDevices), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!(ExitCode::from(io::ErrorKind::TooManyLinks), ExitCode::IoErr); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::InvalidFilename), ExitCode::IoErr ); #[cfg(feature = "extended_io_error")] assert_eq!( ExitCode::from(io::ErrorKind::ArgumentListTooLong), ExitCode::IoErr ); assert_eq!( ExitCode::from(io::ErrorKind::Interrupted), ExitCode::TempFail ); assert_eq!( ExitCode::from(io::ErrorKind::Unsupported), ExitCode::Protocol ); assert_eq!( ExitCode::from(io::ErrorKind::UnexpectedEof), ExitCode::Software ); assert_eq!(ExitCode::from(io::ErrorKind::OutOfMemory), ExitCode::OsErr); assert_eq!(ExitCode::from(io::ErrorKind::Other), ExitCode::IoErr); } #[cfg(feature = "std")] #[cfg(any(unix, windows))] #[test] #[allow(clippy::cognitive_complexity)] fn try_from_process_exit_status_to_exit_code() { assert_eq!( ExitCode::try_from(get_exit_status(0)).unwrap(), ExitCode::Ok ); assert_eq!( ExitCode::try_from(get_exit_status(64)).unwrap(), ExitCode::Usage ); assert_eq!( ExitCode::try_from(get_exit_status(65)).unwrap(), ExitCode::DataErr ); assert_eq!( ExitCode::try_from(get_exit_status(66)).unwrap(), ExitCode::NoInput ); assert_eq!( ExitCode::try_from(get_exit_status(67)).unwrap(), ExitCode::NoUser ); assert_eq!( ExitCode::try_from(get_exit_status(68)).unwrap(), ExitCode::NoHost ); assert_eq!( ExitCode::try_from(get_exit_status(69)).unwrap(), ExitCode::Unavailable ); assert_eq!( ExitCode::try_from(get_exit_status(70)).unwrap(), ExitCode::Software ); assert_eq!( ExitCode::try_from(get_exit_status(71)).unwrap(), ExitCode::OsErr ); assert_eq!( ExitCode::try_from(get_exit_status(72)).unwrap(), ExitCode::OsFile ); assert_eq!( ExitCode::try_from(get_exit_status(73)).unwrap(), ExitCode::CantCreat ); assert_eq!( ExitCode::try_from(get_exit_status(74)).unwrap(), ExitCode::IoErr ); assert_eq!( ExitCode::try_from(get_exit_status(75)).unwrap(), ExitCode::TempFail ); assert_eq!( ExitCode::try_from(get_exit_status(76)).unwrap(), ExitCode::Protocol ); assert_eq!( ExitCode::try_from(get_exit_status(77)).unwrap(), ExitCode::NoPerm ); assert_eq!( ExitCode::try_from(get_exit_status(78)).unwrap(), ExitCode::Config ); } #[cfg(feature = "std")] #[cfg(any(unix, windows))] #[test] fn try_from_process_exit_status_to_exit_code_when_out_of_range() { use crate::error::TryFromExitStatusError; assert_eq!( ExitCode::try_from(get_exit_status(1)).unwrap_err(), TryFromExitStatusError::new(Some(1)) ); assert_eq!( ExitCode::try_from(get_exit_status(63)).unwrap_err(), TryFromExitStatusError::new(Some(63)) ); assert_eq!( ExitCode::try_from(get_exit_status(79)).unwrap_err(), TryFromExitStatusError::new(Some(79)) ); } #[cfg(all(feature = "std", unix))] #[test] fn try_from_process_exit_status_to_exit_code_when_terminated_by_signal() { use crate::error::TryFromExitStatusError; fn get_exit_status() -> std::process::ExitStatus { use std::process::{Command, Stdio}; let mut child = Command::new("sh") .arg("-c") .arg("read a") .stdin(Stdio::piped()) .spawn() .unwrap(); child.kill().unwrap(); child.wait().unwrap() } assert_eq!( ExitCode::try_from(get_exit_status()).unwrap_err(), TryFromExitStatusError::new(None) ); } } sysexits-0.8.1/src/exit_code/result.rs000064400000000000000000000134071046102023000161500ustar 00000000000000// SPDX-FileCopyrightText: 2023 Kevin Matthes // SPDX-FileCopyrightText: 2023 Shun Sakai // // SPDX-License-Identifier: Apache-2.0 OR MIT //! Error handling with the [`Result`] type based on [`ExitCode`]. use super::ExitCode; /// A [`Result`](core::result::Result) type based on [`ExitCode`]. /// /// In case of an error, an appropriate variant of [`ExitCode`] can describe the /// exact cause in further detail. pub type Result = core::result::Result; impl From> for ExitCode { /// Converts a [`Result`] into an `ExitCode`. /// /// This method returns [`ExitCode::Ok`] if the result is [`Ok`], otherwise /// returns the appropriate variant of `ExitCode` contained in the [`Err`] /// variant. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::from(Ok::<(), ExitCode>(())), ExitCode::Ok); /// assert_eq!(ExitCode::from(Ok::(42)), ExitCode::Ok); /// /// assert_eq!( /// ExitCode::from(Err::<(), ExitCode>(ExitCode::Usage)), /// ExitCode::Usage /// ); /// assert_eq!( /// ExitCode::from(Err::(ExitCode::Usage)), /// ExitCode::Usage /// ); /// ``` fn from(result: Result) -> Self { result.map_or_else(|code| code, |_| Self::Ok) } } #[cfg(test)] mod tests { use super::*; #[test] fn result_type() { assert_eq!( core::any::type_name::>(), core::any::type_name::>() ); assert_eq!( core::any::type_name::>(), core::any::type_name::>() ); } #[test] #[allow(clippy::cognitive_complexity, clippy::too_many_lines)] fn from_result_type_to_exit_code() { assert_eq!(ExitCode::from(Ok::<(), ExitCode>(())), ExitCode::Ok); assert_eq!(ExitCode::from(Ok::(42)), ExitCode::Ok); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::Usage)), ExitCode::Usage ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::DataErr)), ExitCode::DataErr ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::NoInput)), ExitCode::NoInput ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::NoUser)), ExitCode::NoUser ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::NoHost)), ExitCode::NoHost ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::Unavailable)), ExitCode::Unavailable ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::Software)), ExitCode::Software ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::OsErr)), ExitCode::OsErr ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::OsFile)), ExitCode::OsFile ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::CantCreat)), ExitCode::CantCreat ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::IoErr)), ExitCode::IoErr ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::TempFail)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::Protocol)), ExitCode::Protocol ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::NoPerm)), ExitCode::NoPerm ); assert_eq!( ExitCode::from(Err::<(), ExitCode>(ExitCode::Config)), ExitCode::Config ); assert_eq!( ExitCode::from(Err::(ExitCode::Usage)), ExitCode::Usage ); assert_eq!( ExitCode::from(Err::(ExitCode::DataErr)), ExitCode::DataErr ); assert_eq!( ExitCode::from(Err::(ExitCode::NoInput)), ExitCode::NoInput ); assert_eq!( ExitCode::from(Err::(ExitCode::NoUser)), ExitCode::NoUser ); assert_eq!( ExitCode::from(Err::(ExitCode::NoHost)), ExitCode::NoHost ); assert_eq!( ExitCode::from(Err::(ExitCode::Unavailable)), ExitCode::Unavailable ); assert_eq!( ExitCode::from(Err::(ExitCode::Software)), ExitCode::Software ); assert_eq!( ExitCode::from(Err::(ExitCode::OsErr)), ExitCode::OsErr ); assert_eq!( ExitCode::from(Err::(ExitCode::OsFile)), ExitCode::OsFile ); assert_eq!( ExitCode::from(Err::(ExitCode::CantCreat)), ExitCode::CantCreat ); assert_eq!( ExitCode::from(Err::(ExitCode::IoErr)), ExitCode::IoErr ); assert_eq!( ExitCode::from(Err::(ExitCode::TempFail)), ExitCode::TempFail ); assert_eq!( ExitCode::from(Err::(ExitCode::Protocol)), ExitCode::Protocol ); assert_eq!( ExitCode::from(Err::(ExitCode::NoPerm)), ExitCode::NoPerm ); assert_eq!( ExitCode::from(Err::(ExitCode::Config)), ExitCode::Config ); } } sysexits-0.8.1/src/exit_code.rs000064400000000000000000000725611046102023000146400ustar 00000000000000// SPDX-FileCopyrightText: 2022 Kevin Matthes // SPDX-FileCopyrightText: 2022 Shun Sakai // // SPDX-License-Identifier: Apache-2.0 OR MIT //! The system exit code constants as defined by [``]. //! //! [``]: https://man.openbsd.org/sysexits mod convert; pub mod result; use core::fmt; /// `ExitCode` is a type that represents the system exit code constants as /// defined by [``]. /// /// [``]: https://man.openbsd.org/sysexits #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ExitCode { /// The successful exit. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Ok as u8, 0); /// ``` Ok, /// The command was used incorrectly, e.g., with the wrong number of /// arguments, a bad flag, bad syntax in a parameter, or whatever. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Usage as u8, 64); /// ``` Usage = 64, /// The input data was incorrect in some way. This should only be used for /// user's data and not system files. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::DataErr as u8, 65); /// ``` DataErr, /// An input file (not a system file) did not exist or was not readable. /// This could also include errors like "No message" to a mailer (if it /// cared to catch it). /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::NoInput as u8, 66); /// ``` NoInput, /// The user specified did not exist. This might be used for mail addresses /// or remote logins. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::NoUser as u8, 67); /// ``` NoUser, /// The host specified did not exist. This is used in mail addresses or /// network requests. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::NoHost as u8, 68); /// ``` NoHost, /// A service is unavailable. This can occur if a support program or file /// does not exist. This can also be used as a catch-all message when /// something you wanted to do doesn't work, but you don't know why. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Unavailable as u8, 69); /// ``` Unavailable, /// An internal software error has been detected. This should be limited to /// non-operating system related errors if possible. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Software as u8, 70); /// ``` Software, /// An operating system error has been detected. This is intended to be used /// for such things as "cannot fork", or "cannot create pipe". It includes /// things like [`getuid(2)`] returning a user that does not exist in the /// passwd file. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::OsErr as u8, 71); /// ``` /// /// [`getuid(2)`]: https://man.openbsd.org/getuid.2 OsErr, /// Some system file (e.g., `/etc/passwd`, `/var/run/utmp`) does not exist, /// cannot be opened, or has some sort of error (e.g., syntax error). /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::OsFile as u8, 72); /// ``` OsFile, /// A (user specified) output file cannot be created. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::CantCreat as u8, 73); /// ``` CantCreat, /// An error occurred while doing I/O on some file. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::IoErr as u8, 74); /// ``` IoErr, /// Temporary failure, indicating something that is not really an error. For /// example that a mailer could not create a connection, and the request /// should be reattempted later. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::TempFail as u8, 75); /// ``` TempFail, /// The remote system returned something that was "not possible" during a /// protocol exchange. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Protocol as u8, 76); /// ``` Protocol, /// You did not have sufficient permission to perform the operation. This is /// not intended for file system problems, which should use /// [`NoInput`](Self::NoInput) or [`CantCreat`](Self::CantCreat), but rather /// for higher level permissions. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::NoPerm as u8, 77); /// ``` NoPerm, /// Something was found in an unconfigured or misconfigured state. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Config as u8, 78); /// ``` Config, } impl ExitCode { /// Returns [`true`] if this system exit code represents successful /// termination. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Ok.is_success(), true); /// assert_eq!(ExitCode::Usage.is_success(), false); /// ``` #[must_use] #[inline] pub const fn is_success(&self) -> bool { matches!(self, Self::Ok) } /// Returns [`true`] if this system exit code represents unsuccessful /// termination. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(ExitCode::Ok.is_failure(), false); /// assert_eq!(ExitCode::Usage.is_failure(), true); /// ``` #[must_use] #[inline] pub const fn is_failure(&self) -> bool { !self.is_success() } /// Terminates the current process with the exit code defined by `ExitCode`. /// /// Equivalent to [`std::process::exit`] with a restricted exit code. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// fn main() { /// ExitCode::Ok.exit(); /// } /// ``` #[cfg(feature = "std")] pub fn exit(self) -> ! { std::process::exit(self.into()) } } impl fmt::Display for ExitCode { /// Shows the integer representation of this `ExitCode`. /// /// # Examples /// /// ``` /// # use sysexits::ExitCode; /// # /// assert_eq!(format!("{}", ExitCode::Ok), "0"); /// assert_eq!(format!("{}", ExitCode::Usage), "64"); /// ``` #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { u8::from(*self).fmt(f) } } #[cfg(feature = "std")] impl std::error::Error for ExitCode {} #[cfg(feature = "std")] impl std::process::Termination for ExitCode { #[inline] fn report(self) -> std::process::ExitCode { u8::from(self).into() } } #[cfg(test)] mod tests { use super::*; #[test] fn clone_exit_code() { assert_eq!(ExitCode::Ok.clone(), ExitCode::Ok); } #[test] fn copy_exit_code() { let a = ExitCode::Ok; let b = a; assert_eq!(a, b); } #[test] fn debug_exit_code() { assert_eq!(format!("{:?}", ExitCode::Ok), "Ok"); assert_eq!(format!("{:?}", ExitCode::Usage), "Usage"); assert_eq!(format!("{:?}", ExitCode::DataErr), "DataErr"); assert_eq!(format!("{:?}", ExitCode::NoInput), "NoInput"); assert_eq!(format!("{:?}", ExitCode::NoUser), "NoUser"); assert_eq!(format!("{:?}", ExitCode::NoHost), "NoHost"); assert_eq!(format!("{:?}", ExitCode::Unavailable), "Unavailable"); assert_eq!(format!("{:?}", ExitCode::Software), "Software"); assert_eq!(format!("{:?}", ExitCode::OsErr), "OsErr"); assert_eq!(format!("{:?}", ExitCode::OsFile), "OsFile"); assert_eq!(format!("{:?}", ExitCode::CantCreat), "CantCreat"); assert_eq!(format!("{:?}", ExitCode::IoErr), "IoErr"); assert_eq!(format!("{:?}", ExitCode::TempFail), "TempFail"); assert_eq!(format!("{:?}", ExitCode::Protocol), "Protocol"); assert_eq!(format!("{:?}", ExitCode::NoPerm), "NoPerm"); assert_eq!(format!("{:?}", ExitCode::Config), "Config"); } #[test] #[allow(clippy::cognitive_complexity, clippy::too_many_lines)] fn exit_code_equality() { assert_eq!(ExitCode::Ok, ExitCode::Ok); assert_ne!(ExitCode::Ok, ExitCode::Usage); assert_ne!(ExitCode::Ok, ExitCode::DataErr); assert_ne!(ExitCode::Ok, ExitCode::NoInput); assert_ne!(ExitCode::Ok, ExitCode::NoUser); assert_ne!(ExitCode::Ok, ExitCode::NoHost); assert_ne!(ExitCode::Ok, ExitCode::Unavailable); assert_ne!(ExitCode::Ok, ExitCode::Software); assert_ne!(ExitCode::Ok, ExitCode::OsErr); assert_ne!(ExitCode::Ok, ExitCode::OsFile); assert_ne!(ExitCode::Ok, ExitCode::CantCreat); assert_ne!(ExitCode::Ok, ExitCode::IoErr); assert_ne!(ExitCode::Ok, ExitCode::TempFail); assert_ne!(ExitCode::Ok, ExitCode::Protocol); assert_ne!(ExitCode::Ok, ExitCode::NoPerm); assert_ne!(ExitCode::Ok, ExitCode::Config); assert_ne!(ExitCode::Usage, ExitCode::Ok); assert_eq!(ExitCode::Usage, ExitCode::Usage); assert_ne!(ExitCode::Usage, ExitCode::DataErr); assert_ne!(ExitCode::Usage, ExitCode::NoInput); assert_ne!(ExitCode::Usage, ExitCode::NoUser); assert_ne!(ExitCode::Usage, ExitCode::NoHost); assert_ne!(ExitCode::Usage, ExitCode::Unavailable); assert_ne!(ExitCode::Usage, ExitCode::Software); assert_ne!(ExitCode::Usage, ExitCode::OsErr); assert_ne!(ExitCode::Usage, ExitCode::OsFile); assert_ne!(ExitCode::Usage, ExitCode::CantCreat); assert_ne!(ExitCode::Usage, ExitCode::IoErr); assert_ne!(ExitCode::Usage, ExitCode::TempFail); assert_ne!(ExitCode::Usage, ExitCode::Protocol); assert_ne!(ExitCode::Usage, ExitCode::NoPerm); assert_ne!(ExitCode::Usage, ExitCode::Config); assert_ne!(ExitCode::DataErr, ExitCode::Ok); assert_ne!(ExitCode::DataErr, ExitCode::Usage); assert_eq!(ExitCode::DataErr, ExitCode::DataErr); assert_ne!(ExitCode::DataErr, ExitCode::NoInput); assert_ne!(ExitCode::DataErr, ExitCode::NoUser); assert_ne!(ExitCode::DataErr, ExitCode::NoHost); assert_ne!(ExitCode::DataErr, ExitCode::Unavailable); assert_ne!(ExitCode::DataErr, ExitCode::Software); assert_ne!(ExitCode::DataErr, ExitCode::OsErr); assert_ne!(ExitCode::DataErr, ExitCode::OsFile); assert_ne!(ExitCode::DataErr, ExitCode::CantCreat); assert_ne!(ExitCode::DataErr, ExitCode::IoErr); assert_ne!(ExitCode::DataErr, ExitCode::TempFail); assert_ne!(ExitCode::DataErr, ExitCode::Protocol); assert_ne!(ExitCode::DataErr, ExitCode::NoPerm); assert_ne!(ExitCode::DataErr, ExitCode::Config); assert_ne!(ExitCode::NoInput, ExitCode::Ok); assert_ne!(ExitCode::NoInput, ExitCode::Usage); assert_ne!(ExitCode::NoInput, ExitCode::DataErr); assert_eq!(ExitCode::NoInput, ExitCode::NoInput); assert_ne!(ExitCode::NoInput, ExitCode::NoUser); assert_ne!(ExitCode::NoInput, ExitCode::NoHost); assert_ne!(ExitCode::NoInput, ExitCode::Unavailable); assert_ne!(ExitCode::NoInput, ExitCode::Software); assert_ne!(ExitCode::NoInput, ExitCode::OsErr); assert_ne!(ExitCode::NoInput, ExitCode::OsFile); assert_ne!(ExitCode::NoInput, ExitCode::CantCreat); assert_ne!(ExitCode::NoInput, ExitCode::IoErr); assert_ne!(ExitCode::NoInput, ExitCode::TempFail); assert_ne!(ExitCode::NoInput, ExitCode::Protocol); assert_ne!(ExitCode::NoInput, ExitCode::NoPerm); assert_ne!(ExitCode::NoInput, ExitCode::Config); assert_ne!(ExitCode::NoUser, ExitCode::Ok); assert_ne!(ExitCode::NoUser, ExitCode::Usage); assert_ne!(ExitCode::NoUser, ExitCode::DataErr); assert_ne!(ExitCode::NoUser, ExitCode::NoInput); assert_eq!(ExitCode::NoUser, ExitCode::NoUser); assert_ne!(ExitCode::NoUser, ExitCode::NoHost); assert_ne!(ExitCode::NoUser, ExitCode::Unavailable); assert_ne!(ExitCode::NoUser, ExitCode::Software); assert_ne!(ExitCode::NoUser, ExitCode::OsErr); assert_ne!(ExitCode::NoUser, ExitCode::OsFile); assert_ne!(ExitCode::NoUser, ExitCode::CantCreat); assert_ne!(ExitCode::NoUser, ExitCode::IoErr); assert_ne!(ExitCode::NoUser, ExitCode::TempFail); assert_ne!(ExitCode::NoUser, ExitCode::Protocol); assert_ne!(ExitCode::NoUser, ExitCode::NoPerm); assert_ne!(ExitCode::NoUser, ExitCode::Config); assert_ne!(ExitCode::NoHost, ExitCode::Ok); assert_ne!(ExitCode::NoHost, ExitCode::Usage); assert_ne!(ExitCode::NoHost, ExitCode::DataErr); assert_ne!(ExitCode::NoHost, ExitCode::NoInput); assert_ne!(ExitCode::NoHost, ExitCode::NoUser); assert_eq!(ExitCode::NoHost, ExitCode::NoHost); assert_ne!(ExitCode::NoHost, ExitCode::Unavailable); assert_ne!(ExitCode::NoHost, ExitCode::Software); assert_ne!(ExitCode::NoHost, ExitCode::OsErr); assert_ne!(ExitCode::NoHost, ExitCode::OsFile); assert_ne!(ExitCode::NoHost, ExitCode::CantCreat); assert_ne!(ExitCode::NoHost, ExitCode::IoErr); assert_ne!(ExitCode::NoHost, ExitCode::TempFail); assert_ne!(ExitCode::NoHost, ExitCode::Protocol); assert_ne!(ExitCode::NoHost, ExitCode::NoPerm); assert_ne!(ExitCode::NoHost, ExitCode::Config); assert_ne!(ExitCode::Unavailable, ExitCode::Ok); assert_ne!(ExitCode::Unavailable, ExitCode::Usage); assert_ne!(ExitCode::Unavailable, ExitCode::DataErr); assert_ne!(ExitCode::Unavailable, ExitCode::NoInput); assert_ne!(ExitCode::Unavailable, ExitCode::NoUser); assert_ne!(ExitCode::Unavailable, ExitCode::NoHost); assert_eq!(ExitCode::Unavailable, ExitCode::Unavailable); assert_ne!(ExitCode::Unavailable, ExitCode::Software); assert_ne!(ExitCode::Unavailable, ExitCode::OsErr); assert_ne!(ExitCode::Unavailable, ExitCode::OsFile); assert_ne!(ExitCode::Unavailable, ExitCode::CantCreat); assert_ne!(ExitCode::Unavailable, ExitCode::IoErr); assert_ne!(ExitCode::Unavailable, ExitCode::TempFail); assert_ne!(ExitCode::Unavailable, ExitCode::Protocol); assert_ne!(ExitCode::Unavailable, ExitCode::NoPerm); assert_ne!(ExitCode::Unavailable, ExitCode::Config); assert_ne!(ExitCode::Software, ExitCode::Ok); assert_ne!(ExitCode::Software, ExitCode::Usage); assert_ne!(ExitCode::Software, ExitCode::DataErr); assert_ne!(ExitCode::Software, ExitCode::NoInput); assert_ne!(ExitCode::Software, ExitCode::NoUser); assert_ne!(ExitCode::Software, ExitCode::NoHost); assert_ne!(ExitCode::Software, ExitCode::Unavailable); assert_eq!(ExitCode::Software, ExitCode::Software); assert_ne!(ExitCode::Software, ExitCode::OsErr); assert_ne!(ExitCode::Software, ExitCode::OsFile); assert_ne!(ExitCode::Software, ExitCode::CantCreat); assert_ne!(ExitCode::Software, ExitCode::IoErr); assert_ne!(ExitCode::Software, ExitCode::TempFail); assert_ne!(ExitCode::Software, ExitCode::Protocol); assert_ne!(ExitCode::Software, ExitCode::NoPerm); assert_ne!(ExitCode::Software, ExitCode::Config); assert_ne!(ExitCode::OsErr, ExitCode::Ok); assert_ne!(ExitCode::OsErr, ExitCode::Usage); assert_ne!(ExitCode::OsErr, ExitCode::DataErr); assert_ne!(ExitCode::OsErr, ExitCode::NoInput); assert_ne!(ExitCode::OsErr, ExitCode::NoUser); assert_ne!(ExitCode::OsErr, ExitCode::NoHost); assert_ne!(ExitCode::OsErr, ExitCode::Unavailable); assert_ne!(ExitCode::OsErr, ExitCode::Software); assert_eq!(ExitCode::OsErr, ExitCode::OsErr); assert_ne!(ExitCode::OsErr, ExitCode::OsFile); assert_ne!(ExitCode::OsErr, ExitCode::CantCreat); assert_ne!(ExitCode::OsErr, ExitCode::IoErr); assert_ne!(ExitCode::OsErr, ExitCode::TempFail); assert_ne!(ExitCode::OsErr, ExitCode::Protocol); assert_ne!(ExitCode::OsErr, ExitCode::NoPerm); assert_ne!(ExitCode::OsErr, ExitCode::Config); assert_ne!(ExitCode::OsFile, ExitCode::Ok); assert_ne!(ExitCode::OsFile, ExitCode::Usage); assert_ne!(ExitCode::OsFile, ExitCode::DataErr); assert_ne!(ExitCode::OsFile, ExitCode::NoInput); assert_ne!(ExitCode::OsFile, ExitCode::NoUser); assert_ne!(ExitCode::OsFile, ExitCode::NoHost); assert_ne!(ExitCode::OsFile, ExitCode::Unavailable); assert_ne!(ExitCode::OsFile, ExitCode::Software); assert_ne!(ExitCode::OsFile, ExitCode::OsErr); assert_eq!(ExitCode::OsFile, ExitCode::OsFile); assert_ne!(ExitCode::OsFile, ExitCode::CantCreat); assert_ne!(ExitCode::OsFile, ExitCode::IoErr); assert_ne!(ExitCode::OsFile, ExitCode::TempFail); assert_ne!(ExitCode::OsFile, ExitCode::Protocol); assert_ne!(ExitCode::OsFile, ExitCode::NoPerm); assert_ne!(ExitCode::OsFile, ExitCode::Config); assert_ne!(ExitCode::CantCreat, ExitCode::Ok); assert_ne!(ExitCode::CantCreat, ExitCode::Usage); assert_ne!(ExitCode::CantCreat, ExitCode::DataErr); assert_ne!(ExitCode::CantCreat, ExitCode::NoInput); assert_ne!(ExitCode::CantCreat, ExitCode::NoUser); assert_ne!(ExitCode::CantCreat, ExitCode::NoHost); assert_ne!(ExitCode::CantCreat, ExitCode::Unavailable); assert_ne!(ExitCode::CantCreat, ExitCode::Software); assert_ne!(ExitCode::CantCreat, ExitCode::OsErr); assert_ne!(ExitCode::CantCreat, ExitCode::OsFile); assert_eq!(ExitCode::CantCreat, ExitCode::CantCreat); assert_ne!(ExitCode::CantCreat, ExitCode::IoErr); assert_ne!(ExitCode::CantCreat, ExitCode::TempFail); assert_ne!(ExitCode::CantCreat, ExitCode::Protocol); assert_ne!(ExitCode::CantCreat, ExitCode::NoPerm); assert_ne!(ExitCode::CantCreat, ExitCode::Config); assert_ne!(ExitCode::IoErr, ExitCode::Ok); assert_ne!(ExitCode::IoErr, ExitCode::Usage); assert_ne!(ExitCode::IoErr, ExitCode::DataErr); assert_ne!(ExitCode::IoErr, ExitCode::NoInput); assert_ne!(ExitCode::IoErr, ExitCode::NoUser); assert_ne!(ExitCode::IoErr, ExitCode::NoHost); assert_ne!(ExitCode::IoErr, ExitCode::Unavailable); assert_ne!(ExitCode::IoErr, ExitCode::Software); assert_ne!(ExitCode::IoErr, ExitCode::OsErr); assert_ne!(ExitCode::IoErr, ExitCode::OsFile); assert_ne!(ExitCode::IoErr, ExitCode::CantCreat); assert_eq!(ExitCode::IoErr, ExitCode::IoErr); assert_ne!(ExitCode::IoErr, ExitCode::TempFail); assert_ne!(ExitCode::IoErr, ExitCode::Protocol); assert_ne!(ExitCode::IoErr, ExitCode::NoPerm); assert_ne!(ExitCode::IoErr, ExitCode::Config); assert_ne!(ExitCode::TempFail, ExitCode::Ok); assert_ne!(ExitCode::TempFail, ExitCode::Usage); assert_ne!(ExitCode::TempFail, ExitCode::DataErr); assert_ne!(ExitCode::TempFail, ExitCode::NoInput); assert_ne!(ExitCode::TempFail, ExitCode::NoUser); assert_ne!(ExitCode::TempFail, ExitCode::NoHost); assert_ne!(ExitCode::TempFail, ExitCode::Unavailable); assert_ne!(ExitCode::TempFail, ExitCode::Software); assert_ne!(ExitCode::TempFail, ExitCode::OsErr); assert_ne!(ExitCode::TempFail, ExitCode::OsFile); assert_ne!(ExitCode::TempFail, ExitCode::CantCreat); assert_ne!(ExitCode::TempFail, ExitCode::IoErr); assert_eq!(ExitCode::TempFail, ExitCode::TempFail); assert_ne!(ExitCode::TempFail, ExitCode::Protocol); assert_ne!(ExitCode::TempFail, ExitCode::NoPerm); assert_ne!(ExitCode::TempFail, ExitCode::Config); assert_ne!(ExitCode::Protocol, ExitCode::Ok); assert_ne!(ExitCode::Protocol, ExitCode::Usage); assert_ne!(ExitCode::Protocol, ExitCode::DataErr); assert_ne!(ExitCode::Protocol, ExitCode::NoInput); assert_ne!(ExitCode::Protocol, ExitCode::NoUser); assert_ne!(ExitCode::Protocol, ExitCode::NoHost); assert_ne!(ExitCode::Protocol, ExitCode::Unavailable); assert_ne!(ExitCode::Protocol, ExitCode::Software); assert_ne!(ExitCode::Protocol, ExitCode::OsErr); assert_ne!(ExitCode::Protocol, ExitCode::OsFile); assert_ne!(ExitCode::Protocol, ExitCode::CantCreat); assert_ne!(ExitCode::Protocol, ExitCode::IoErr); assert_ne!(ExitCode::Protocol, ExitCode::TempFail); assert_eq!(ExitCode::Protocol, ExitCode::Protocol); assert_ne!(ExitCode::Protocol, ExitCode::NoPerm); assert_ne!(ExitCode::Protocol, ExitCode::Config); assert_ne!(ExitCode::NoPerm, ExitCode::Ok); assert_ne!(ExitCode::NoPerm, ExitCode::Usage); assert_ne!(ExitCode::NoPerm, ExitCode::DataErr); assert_ne!(ExitCode::NoPerm, ExitCode::NoInput); assert_ne!(ExitCode::NoPerm, ExitCode::NoUser); assert_ne!(ExitCode::NoPerm, ExitCode::NoHost); assert_ne!(ExitCode::NoPerm, ExitCode::Unavailable); assert_ne!(ExitCode::NoPerm, ExitCode::Software); assert_ne!(ExitCode::NoPerm, ExitCode::OsErr); assert_ne!(ExitCode::NoPerm, ExitCode::OsFile); assert_ne!(ExitCode::NoPerm, ExitCode::CantCreat); assert_ne!(ExitCode::NoPerm, ExitCode::IoErr); assert_ne!(ExitCode::NoPerm, ExitCode::TempFail); assert_ne!(ExitCode::NoPerm, ExitCode::Protocol); assert_eq!(ExitCode::NoPerm, ExitCode::NoPerm); assert_ne!(ExitCode::NoPerm, ExitCode::Config); assert_ne!(ExitCode::Config, ExitCode::Ok); assert_ne!(ExitCode::Config, ExitCode::Usage); assert_ne!(ExitCode::Config, ExitCode::DataErr); assert_ne!(ExitCode::Config, ExitCode::NoInput); assert_ne!(ExitCode::Config, ExitCode::NoUser); assert_ne!(ExitCode::Config, ExitCode::NoHost); assert_ne!(ExitCode::Config, ExitCode::Unavailable); assert_ne!(ExitCode::Config, ExitCode::Software); assert_ne!(ExitCode::Config, ExitCode::OsErr); assert_ne!(ExitCode::Config, ExitCode::OsFile); assert_ne!(ExitCode::Config, ExitCode::CantCreat); assert_ne!(ExitCode::Config, ExitCode::IoErr); assert_ne!(ExitCode::Config, ExitCode::TempFail); assert_ne!(ExitCode::Config, ExitCode::Protocol); assert_ne!(ExitCode::Config, ExitCode::NoPerm); assert_eq!(ExitCode::Config, ExitCode::Config); } #[test] fn display_exit_code() { assert_eq!(format!("{}", ExitCode::Ok), format!("{}", 0)); assert_eq!(format!("{}", ExitCode::Usage), format!("{}", 64)); assert_eq!(format!("{}", ExitCode::DataErr), format!("{}", 65)); assert_eq!(format!("{}", ExitCode::NoInput), format!("{}", 66)); assert_eq!(format!("{}", ExitCode::NoUser), format!("{}", 67)); assert_eq!(format!("{}", ExitCode::NoHost), format!("{}", 68)); assert_eq!(format!("{}", ExitCode::Unavailable), format!("{}", 69)); assert_eq!(format!("{}", ExitCode::Software), format!("{}", 70)); assert_eq!(format!("{}", ExitCode::OsErr), format!("{}", 71)); assert_eq!(format!("{}", ExitCode::OsFile), format!("{}", 72)); assert_eq!(format!("{}", ExitCode::CantCreat), format!("{}", 73)); assert_eq!(format!("{}", ExitCode::IoErr), format!("{}", 74)); assert_eq!(format!("{}", ExitCode::TempFail), format!("{}", 75)); assert_eq!(format!("{}", ExitCode::Protocol), format!("{}", 76)); assert_eq!(format!("{}", ExitCode::NoPerm), format!("{}", 77)); assert_eq!(format!("{}", ExitCode::Config), format!("{}", 78)); } #[test] fn is_success_for_successful_termination() { assert!(ExitCode::Ok.is_success()); } #[test] fn is_success_for_unsuccessful_termination() { assert!(!ExitCode::Usage.is_success()); assert!(!ExitCode::DataErr.is_success()); assert!(!ExitCode::NoInput.is_success()); assert!(!ExitCode::NoUser.is_success()); assert!(!ExitCode::NoHost.is_success()); assert!(!ExitCode::Unavailable.is_success()); assert!(!ExitCode::Software.is_success()); assert!(!ExitCode::OsErr.is_success()); assert!(!ExitCode::OsFile.is_success()); assert!(!ExitCode::CantCreat.is_success()); assert!(!ExitCode::IoErr.is_success()); assert!(!ExitCode::TempFail.is_success()); assert!(!ExitCode::Protocol.is_success()); assert!(!ExitCode::NoPerm.is_success()); assert!(!ExitCode::Config.is_success()); } #[test] fn is_failure_for_successful_termination() { assert!(!ExitCode::Ok.is_failure()); } #[test] fn is_failure_for_unsuccessful_termination() { assert!(ExitCode::Usage.is_failure()); assert!(ExitCode::DataErr.is_failure()); assert!(ExitCode::NoInput.is_failure()); assert!(ExitCode::NoUser.is_failure()); assert!(ExitCode::NoHost.is_failure()); assert!(ExitCode::Unavailable.is_failure()); assert!(ExitCode::Software.is_failure()); assert!(ExitCode::OsErr.is_failure()); assert!(ExitCode::OsFile.is_failure()); assert!(ExitCode::CantCreat.is_failure()); assert!(ExitCode::IoErr.is_failure()); assert!(ExitCode::TempFail.is_failure()); assert!(ExitCode::Protocol.is_failure()); assert!(ExitCode::NoPerm.is_failure()); assert!(ExitCode::Config.is_failure()); } #[cfg(feature = "std")] #[test] fn source_exit_code() { use std::error::Error; assert!(ExitCode::Ok.source().is_none()); assert!(ExitCode::Usage.source().is_none()); assert!(ExitCode::DataErr.source().is_none()); assert!(ExitCode::NoInput.source().is_none()); assert!(ExitCode::NoUser.source().is_none()); assert!(ExitCode::NoHost.source().is_none()); assert!(ExitCode::Unavailable.source().is_none()); assert!(ExitCode::Software.source().is_none()); assert!(ExitCode::OsErr.source().is_none()); assert!(ExitCode::OsFile.source().is_none()); assert!(ExitCode::CantCreat.source().is_none()); assert!(ExitCode::IoErr.source().is_none()); assert!(ExitCode::TempFail.source().is_none()); assert!(ExitCode::Protocol.source().is_none()); assert!(ExitCode::NoPerm.source().is_none()); assert!(ExitCode::Config.source().is_none()); } #[cfg(feature = "std")] #[test] fn report_exit_code() { use std::process::Termination; assert_eq!( format!("{:?}", ExitCode::Ok.report()), format!("{:?}", std::process::ExitCode::from(0)) ); assert_eq!( format!("{:?}", ExitCode::Usage.report()), format!("{:?}", std::process::ExitCode::from(64)) ); assert_eq!( format!("{:?}", ExitCode::DataErr.report()), format!("{:?}", std::process::ExitCode::from(65)) ); assert_eq!( format!("{:?}", ExitCode::NoInput.report()), format!("{:?}", std::process::ExitCode::from(66)) ); assert_eq!( format!("{:?}", ExitCode::NoUser.report()), format!("{:?}", std::process::ExitCode::from(67)) ); assert_eq!( format!("{:?}", ExitCode::NoHost.report()), format!("{:?}", std::process::ExitCode::from(68)) ); assert_eq!( format!("{:?}", ExitCode::Unavailable.report()), format!("{:?}", std::process::ExitCode::from(69)) ); assert_eq!( format!("{:?}", ExitCode::Software.report()), format!("{:?}", std::process::ExitCode::from(70)) ); assert_eq!( format!("{:?}", ExitCode::OsErr.report()), format!("{:?}", std::process::ExitCode::from(71)) ); assert_eq!( format!("{:?}", ExitCode::OsFile.report()), format!("{:?}", std::process::ExitCode::from(72)) ); assert_eq!( format!("{:?}", ExitCode::CantCreat.report()), format!("{:?}", std::process::ExitCode::from(73)) ); assert_eq!( format!("{:?}", ExitCode::IoErr.report()), format!("{:?}", std::process::ExitCode::from(74)) ); assert_eq!( format!("{:?}", ExitCode::TempFail.report()), format!("{:?}", std::process::ExitCode::from(75)) ); assert_eq!( format!("{:?}", ExitCode::Protocol.report()), format!("{:?}", std::process::ExitCode::from(76)) ); assert_eq!( format!("{:?}", ExitCode::NoPerm.report()), format!("{:?}", std::process::ExitCode::from(77)) ); assert_eq!( format!("{:?}", ExitCode::Config.report()), format!("{:?}", std::process::ExitCode::from(78)) ); } } sysexits-0.8.1/src/lib.rs000064400000000000000000000032011046102023000134240ustar 00000000000000// SPDX-FileCopyrightText: 2022 Shun Sakai // SPDX-FileCopyrightText: 2023 Kevin Matthes // SPDX-FileCopyrightText: 2023 zSchoen // // SPDX-License-Identifier: Apache-2.0 OR MIT //! The `sysexits` crate provides the system exit code constants as defined by //! [``]. //! //! The [`ExitCode`] type implements the //! [`Termination`](std::process::Termination) trait, so this can be returned //! from the `main` function. //! //! # Examples //! //! ``` //! # #[cfg(feature = "std")] //! use std::str; //! //! # #[cfg(feature = "std")] //! use sysexits::ExitCode; //! //! # #[cfg(feature = "std")] //! fn main() -> ExitCode { //! let bytes = [0xf0, 0x9f, 0x92, 0x96]; //! match str::from_utf8(&bytes) { //! Ok(string) => { //! println!("{string}"); //! ExitCode::Ok //! } //! Err(err) => { //! eprintln!("{err}"); //! ExitCode::DataErr //! } //! } //! } //! # //! # #[cfg(not(feature = "std"))] //! # fn main() {} //! ``` //! //! [``]: https://man.openbsd.org/sysexits #![cfg_attr(feature = "extended_io_error", feature(io_error_more))] #![doc(html_root_url = "https://docs.rs/sysexits/0.8.1/")] #![no_std] #![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))] // Lint levels of rustc. #![forbid(unsafe_code)] #![deny(missing_debug_implementations, missing_docs)] #![warn(rust_2018_idioms)] // Lint levels of Clippy. #![warn(clippy::cargo, clippy::nursery, clippy::pedantic)] #[cfg(test)] #[macro_use] extern crate alloc; #[cfg(feature = "std")] extern crate std; pub mod error; mod exit_code; pub use crate::exit_code::{result::Result, ExitCode};