This is a rust attribute-like proc macro which reduces the amount of code required to call shell commands and parse the results.
It allows you to wrap a script in any language with strongly typed functions. The function's arguments are set as env variables and the result of the script is parsed either as a value or as an iterator.
```rust use shellfn::shell; use std::error::Error;
fn list_modified(dir: &str) -> Result
```rust use shellfn::shell; use std::error::Error;
fn prettyjson(json: &str, indent: u8, sortkeys: bool) -> Result
input = os.environ['JSON'] indent = int(os.environ['INDENT']) sortkeys = os.environ['SORTKEYS'] == 'true' obj = json.loads(input)
print(json.dumps(obj, indent=indent, sortkeys=sortkeys)) "# } ```
You can use the #[shell]
attribute on functions that have:
- a body containing only one expression - a string literal representing the script to execute
- types that implement the .to_string()
method
- return a value that is either void
, T
, Result<T, E>
, impl Iterator<Item=T>
, Result<impl Iterator<Item=T>>
or Result<impl Iterator<Item=Result<T, E>>>
with constrains:
T: FromStr,
<T as FromStr>::Err: StdError,
E: From<shellfn::Error<<T as FromStr>::Err>>,
The #[shell]
attribute does the following:
std::process::Command
Most of the steps can be adjusted:
- the default command is bash -c
. You can change it using the cmd
parameter:
```rust
- by default, the script is added as the last argument. You can change it using the special variable `PROGRAM` in the `cmd` parameter:
rust
- if the return type is not wrapping some part of the result in `Result`, you may decide to suppress panics by adding the `no_panic` flag:
rust
```
Following return types are currently recognized:
| return type | flags | on parse fail | on error exit code | on spawn fail | notes |
|-----------------------------------------------|----------|---------------|--------------------|---------------|-------|
| | | - | panic | panic | |
| | nopanic | - | nothing | nothing | |
| T | | panic | panic | panic | 2 |
| T | nopanic | panic | panic | panic | 1,2 |
| Result
Glossary:
| action | meaning | |---------------|--------------------------------------------------------------------------| | panic | panics (.expect or panic!) | | nothing | consumes and ignores error (let _ = ...) | | error | returns error | | ignore errors | yields all successfuly parsed items, ignores parsing failures (flat_map) | | empty iter | returns empty iterator | | item error | when parsing fails, yields Err | | ignored | ignores exit code, behaves in the same way for exit code 0 and != 0 |
Notes:
no_panic
attribute makes no differenceAll contributions and comments are more than welcome! Don't be afraid to open an issue or PR whenever you find a bug or have an idea to improve this crate.
MIT License
Copyright (c) 2017 Marcin Sas-SzymaĆski
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.