├── .github ├── dependabot.yml └── workflows │ ├── devskim-analysis.yml │ ├── format.yml │ ├── linux-ci.yml │ ├── mac-ci.yml │ └── windows-ci.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Cargo.toml ├── LICENSE ├── OSSMETADATA ├── README.md ├── ROADMAP.md ├── SECURITY.md ├── compiled_voila ├── Cargo.toml └── src │ └── main.rs ├── rustfmt.toml └── voila ├── Cargo.toml ├── benches └── benchmark.rs ├── build.rs ├── rust-toolchain └── src ├── ast ├── call.rs ├── cycle.rs ├── expr.rs ├── lookup.rs ├── mod.rs ├── script.rs ├── string.rs └── target.rs ├── bytecode.rs ├── cli.rs ├── compiler.rs ├── error.rs ├── interpreter ├── cache.rs ├── error.rs ├── hash.rs └── mod.rs ├── lexer.rs ├── lib.rs ├── macros.rs ├── main.rs ├── parser.rs ├── runtime.rs └── safety.rs /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "cargo" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "daily" 11 | 12 | -------------------------------------------------------------------------------- /.github/workflows/devskim-analysis.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | name: DevSkim 7 | 8 | on: 9 | push: 10 | branches: [ main ] 11 | pull_request: 12 | branches: [ main ] 13 | schedule: 14 | - cron: '35 17 * * 4' 15 | 16 | jobs: 17 | lint: 18 | name: DevSkim 19 | runs-on: ubuntu-20.04 20 | permissions: 21 | actions: read 22 | contents: read 23 | security-events: write 24 | steps: 25 | - name: Checkout code 26 | uses: actions/checkout@v2 27 | 28 | - name: Run DevSkim scanner 29 | uses: microsoft/DevSkim-Action@v1 30 | 31 | - name: Upload DevSkim scan results to GitHub Security tab 32 | uses: github/codeql-action/upload-sarif@v1 33 | with: 34 | sarif_file: devskim-results.sarif 35 | -------------------------------------------------------------------------------- /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | name: format 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | 7 | jobs: 8 | format: 9 | runs-on: ubuntu-20.04 10 | 11 | steps: 12 | - name: Checkout repository 13 | uses: actions/checkout@v2 14 | 15 | - name: Install Rust 16 | uses: actions-rs/toolchain@v1 17 | with: 18 | override: true 19 | profile: minimal 20 | toolchain: nightly 21 | components: rustfmt 22 | 23 | - name: Cache Rust 24 | uses: Swatinem/rust-cache@v1 25 | 26 | - name: Format code with rustfmt 27 | uses: mbrobbel/rustfmt-check@master 28 | with: 29 | token: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /.github/workflows/linux-ci.yml: -------------------------------------------------------------------------------- 1 | name: Linux build 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | name: Rust Linux ${{ matrix.rust }} 12 | runs-on: ubuntu-latest 13 | strategy: 14 | matrix: 15 | rust: [nightly] 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v2 20 | 21 | - name: Install minimal ${{ matrix.rust }} rust 22 | uses: actions-rs/toolchain@v1 23 | with: 24 | override: true 25 | profile: minimal 26 | toolchain: ${{ matrix.rust }} 27 | 28 | - name: Cache Rust 29 | uses: Swatinem/rust-cache@v1 30 | 31 | - name: Check local package 32 | run: cargo check --bin voila 33 | 34 | - name: Run tests 35 | run: cargo test --bin voila 36 | -------------------------------------------------------------------------------- /.github/workflows/mac-ci.yml: -------------------------------------------------------------------------------- 1 | name: macOS build 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | name: Rust macOS ${{ matrix.rust }} 12 | runs-on: macos-10.15 13 | strategy: 14 | matrix: 15 | rust: [nightly] 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v2 20 | 21 | - name: Install minimal ${{ matrix.rust }} rust 22 | uses: actions-rs/toolchain@v1 23 | with: 24 | override: true 25 | profile: minimal 26 | toolchain: ${{ matrix.rust }} 27 | 28 | - name: Cache Rust 29 | uses: Swatinem/rust-cache@v1 30 | 31 | - name: Check local package 32 | run: cargo check --bin voila 33 | 34 | - name: Run tests 35 | run: cargo test --bin voila 36 | -------------------------------------------------------------------------------- /.github/workflows/windows-ci.yml: -------------------------------------------------------------------------------- 1 | name: Windows build 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | name: Rust Windows ${{ matrix.rust }} 12 | runs-on: windows-2019 13 | strategy: 14 | matrix: 15 | rust: [nightly] 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v2 20 | 21 | - name: Install minimal ${{ matrix.rust }} rust 22 | uses: actions-rs/toolchain@v1 23 | with: 24 | override: true 25 | profile: minimal 26 | toolchain: ${{ matrix.rust }} 27 | 28 | - name: Cache Rust 29 | uses: Swatinem/rust-cache@v1 30 | 31 | - name: Check local package 32 | run: cargo check --bin voila 33 | 34 | - name: Run tests 35 | run: cargo test --bin voila 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | test/ 3 | Cargo.lock 4 | code.tar.gz 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 2 | # Contributor Covenant Code of Conduct 3 | 4 | ## Our Pledge 5 | 6 | We as members, contributors, and leaders pledge to make participation in our 7 | community a harassment-free experience for everyone, regardless of age, body 8 | size, visible or invisible disability, ethnicity, sex characteristics, gender 9 | identity and expression, level of experience, education, socio-economic status, 10 | nationality, personal appearance, race, caste, color, religion, or sexual identity 11 | and orientation. 12 | 13 | We pledge to act and interact in ways that contribute to an open, welcoming, 14 | diverse, inclusive, and healthy community. 15 | 16 | ## Our Standards 17 | 18 | Examples of behavior that contributes to a positive environment for our 19 | community include: 20 | 21 | * Demonstrating empathy and kindness toward other people 22 | * Being respectful of differing opinions, viewpoints, and experiences 23 | * Giving and gracefully accepting constructive feedback 24 | * Accepting responsibility and apologizing to those affected by our mistakes, 25 | and learning from the experience 26 | * Focusing on what is best not just for us as individuals, but for the 27 | overall community 28 | 29 | Examples of unacceptable behavior include: 30 | 31 | * The use of sexualized language or imagery, and sexual attention or 32 | advances of any kind 33 | * Trolling, insulting or derogatory comments, and personal or political attacks 34 | * Public or private harassment 35 | * Publishing others' private information, such as a physical or email 36 | address, without their explicit permission 37 | * Other conduct which could reasonably be considered inappropriate in a 38 | professional setting 39 | 40 | ## Enforcement Responsibilities 41 | 42 | Community leaders are responsible for clarifying and enforcing our standards of 43 | acceptable behavior and will take appropriate and fair corrective action in 44 | response to any behavior that they deem inappropriate, threatening, offensive, 45 | or harmful. 46 | 47 | Community leaders have the right and responsibility to remove, edit, or reject 48 | comments, commits, code, wiki edits, issues, and other contributions that are 49 | not aligned to this Code of Conduct, and will communicate reasons for moderation 50 | decisions when appropriate. 51 | 52 | ## Scope 53 | 54 | This Code of Conduct applies within all community spaces, and also applies when 55 | an individual is officially representing the community in public spaces. 56 | Examples of representing our community include using an official e-mail address, 57 | posting via an official social media account, or acting as an appointed 58 | representative at an online or offline event. 59 | 60 | ## Enforcement 61 | 62 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 63 | reported to the community leaders responsible for enforcement at 64 | [INSERT CONTACT METHOD]. 65 | All complaints will be reviewed and investigated promptly and fairly. 66 | 67 | All community leaders are obligated to respect the privacy and security of the 68 | reporter of any incident. 69 | 70 | ## Enforcement Guidelines 71 | 72 | Community leaders will follow these Community Impact Guidelines in determining 73 | the consequences for any action they deem in violation of this Code of Conduct: 74 | 75 | ### 1. Correction 76 | 77 | **Community Impact**: Use of inappropriate language or other behavior deemed 78 | unprofessional or unwelcome in the community. 79 | 80 | **Consequence**: A private, written warning from community leaders, providing 81 | clarity around the nature of the violation and an explanation of why the 82 | behavior was inappropriate. A public apology may be requested. 83 | 84 | ### 2. Warning 85 | 86 | **Community Impact**: A violation through a single incident or series 87 | of actions. 88 | 89 | **Consequence**: A warning with consequences for continued behavior. No 90 | interaction with the people involved, including unsolicited interaction with 91 | those enforcing the Code of Conduct, for a specified period of time. This 92 | includes avoiding interactions in community spaces as well as external channels 93 | like social media. Violating these terms may lead to a temporary or 94 | permanent ban. 95 | 96 | ### 3. Temporary Ban 97 | 98 | **Community Impact**: A serious violation of community standards, including 99 | sustained inappropriate behavior. 100 | 101 | **Consequence**: A temporary ban from any sort of interaction or public 102 | communication with the community for a specified period of time. No public or 103 | private interaction with the people involved, including unsolicited interaction 104 | with those enforcing the Code of Conduct, is allowed during this period. 105 | Violating these terms may lead to a permanent ban. 106 | 107 | ### 4. Permanent Ban 108 | 109 | **Community Impact**: Demonstrating a pattern of violation of community 110 | standards, including sustained inappropriate behavior, harassment of an 111 | individual, or aggression toward or disparagement of classes of individuals. 112 | 113 | **Consequence**: A permanent ban from any sort of public interaction within 114 | the community. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 2.1, available at 120 | [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. 121 | 122 | Community Impact Guidelines were inspired by 123 | [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. 124 | 125 | For answers to common questions about this code of conduct, see the FAQ at 126 | [https://www.contributor-covenant.org/faq][FAQ]. Translations are available 127 | at [https://www.contributor-covenant.org/translations][translations]. 128 | 129 | [homepage]: https://www.contributor-covenant.org 130 | [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html 131 | [Mozilla CoC]: https://github.com/mozilla/diversity 132 | [FAQ]: https://www.contributor-covenant.org/faq 133 | [translations]: https://www.contributor-covenant.org/translations 134 | 135 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | First of all, thanks for considering contributing to Voila! 4 | 5 | If you haven't already, come to our [Discord](https://discord.gg/RhTpYGbnXU)! There is where we discuss the future of the project. 6 | 7 | Here are some important resources: 8 | 9 | - [Code of conduct](https://github.com/Alonely0/voila/blob/main/CODE_OF_CONDUCT.md) 10 | - [Roadmap](https://github.com/Alonely0/voila/blob/main/ROADMAP.md) 11 | 12 | ## Submitting changes 13 | 14 | For submitting a change to Voila source code and contributing to it, please send a [GitHub Pull Request to Voila](https://github.com/Alonely0/voila/pull/new/master) with a clear list of what you've done (read more about [pull requests](https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests)). 15 | 16 | Always write a clear log message for your commits. One-line messages are fine for small changes, but bigger changes should look like this: 17 | 18 | $ git commit -m "A brief summary of the commit 19 | > 20 | > A paragraph describing what changed and its impact." 21 | 22 | ## Coding conventions 23 | 24 | Start reading our code and you'll get the hang of it. We optimize for readability: 25 | 26 | - We indent using 4 spaces 27 | - Comment the code for explaining its purpose and what does, but without writing, obvious, unnecessary and redundant comments. 28 | - We ALWAYS put spaces after list items and method parameters (`[1, 2, 3]`, not `[1,2,3]`), around operators (`x += 1`, not `x+=1`), around arrows (`...) -> ...`, not `...)->...`), before curly brackets (`) {`, not `){`), etc. This is just a convention, as when upload code to the repo it gets formatted automatically. This is open source software. Consider the people who will read your code, and make it look nice for them. It's sort of like driving a car: Perhaps you love doing donuts when you're alone, but with passengers the goal is to make the ride as smooth as possible. 29 | 30 | --- 31 | 32 | Thanks, 33 | Guillem L. (alias *alonely0*), creator of Voila. 34 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | cargo-features = ["strip"] 2 | 3 | [workspace] 4 | members = [ 5 | "voila", 6 | "compiled_voila", 7 | ] 8 | 9 | [profile.release] 10 | lto = true 11 | strip = true -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Guillem Jara 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OSSMETADATA: -------------------------------------------------------------------------------- 1 | osslifecycle=active 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [![Voila](https://i.ibb.co/R2T5Tvb/voila.png)](https://shields.io/) [![forthebadge](https://forthebadge.com/images/badges/made-with-rust.svg)](https://forthebadge.com) [![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com) 2 | 3 | ![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg) 4 | [![GitHub license](https://img.shields.io/github/license/Alonely0/voila.svg)](https://github.com/Alonely0/voila/blob/master/LICENSE) 5 | ![Lines of code](https://img.shields.io/tokei/lines/github/Alonely0/Voila) 6 | ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/Alonely0/Voila?label=latest%20release) 7 | ![GitHub commits since latest release (by date)](https://img.shields.io/github/commits-since/Alonely0/Voila/latest/main) 8 | ![GitHub Release Date](https://img.shields.io/github/release-date/Alonely0/Voila?label=last%20release%20date) 9 | ![GitHub last commit](https://img.shields.io/github/last-commit/Alonely0/Voila) 10 | ![GitHub contributors](https://img.shields.io/github/contributors/Alonely0/Voila) 11 | ![OSS Lifecycle](https://img.shields.io/osslifecycle/Alonely0/Voila) 12 | ![Crates.io](https://img.shields.io/crates/d/voila?label=downloads%40%2A) 13 | ![Crates.io (latest)](https://img.shields.io/crates/dv/voila) 14 | [![Linux build](https://github.com/Alonely0/Voila/actions/workflows/linux-ci.yml/badge.svg)](https://github.com/Alonely0/Voila/actions/workflows/linux-ci.yml) 15 | [![macOS build](https://github.com/Alonely0/Voila/actions/workflows/mac-ci.yml/badge.svg)](https://github.com/Alonely0/Voila/actions/workflows/mac-ci.yml) 16 | [![Windows build](https://github.com/Alonely0/Voila/actions/workflows/windows-ci.yml/badge.svg)](https://github.com/Alonely0/Voila/actions/workflows/windows-ci.yml) 17 | 18 | Voila is a DSL (domain-specific language) for interacting in a fast, reliable, versatile, safe & multithreaded way with files & directories. It is based on a CLI tool, although you can write your Voila code and do something like this `voila DIRECTORY "$(cat operations.vla)"`. Voila is mainly tested in Linux, so should work better in \*nix (Linux, \*BSD, macOS, etc) than in Windows-based operating systems, but shouldn't be any problems on them. Voila is completely cross-platform. 19 | 20 | Voila scripts are interpreted, but optionally you can [compile it](https://github.com/Alonely0/Voila/wiki/Compiling-a-Voila-script) like you'd do with other programming languages. 21 | 22 | # Documentation 23 | You can find the docs in the [wiki](https://github.com/Alonely0/Voila/wiki/Documentation). 24 | 25 | # Installation 26 | 27 | See [#Installation](https://github.com/Alonely0/Voila/wiki/Installation) in the docs. 28 | 29 | # Submitting 30 | 31 | * Errors: file an error issue using [this link](https://github.com/Alonely0/voila/issues/new?assignees=Alonely0&labels=bug&template=bug_report.md&title=). Remember to check if that issue is [already registered](https://github.com/Alonely0/voila/labels/bug)! 32 | * Feature requests: file a f-request issue using [this link](https://github.com/Alonely0/voila/issues/new?assignees=Alonely0&labels=enhancement&template=feature_request.md&title=). Remember to check if that f-request was [already submitted](https://github.com/Alonely0/voila/labels/enhancement)! 33 | * Doubt: file a doubt issue using [this link](https://github.com/Alonely0/voila/issues/new?assignees=Alonely0&labels=question&template=doubt.md&title=). Remember to check if that doubt [was already resolved](https://github.com/Alonely0/voila/labels/question)! 34 | * Wanna chat with me? You can talk with me on [Discord](https://discord.com), add me as friend (`NOT-Guillem#8042`) and we'll be able to start chatting! 35 | -------------------------------------------------------------------------------- /ROADMAP.md: -------------------------------------------------------------------------------- 1 | * verify works on all platforms 2 | * move to a BNF-like language spec 3 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Security updates are pushed directly to the latest version, just because I can't modify whatever already published on crates.io, following github's conventions, here is a table: 6 | 7 | | Version | Supported | 8 | | ------------ | ------------------ | 9 | | Latest | :white_check_mark: | 10 | | Not latest | :x: | 11 | 12 | ## Reporting a Vulnerability 13 | Open up an issue with the error label, and I'll fix that ASAP. 14 | -------------------------------------------------------------------------------- /compiled_voila/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "compiled_voila" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | voila = {path = "../voila"} 8 | -------------------------------------------------------------------------------- /compiled_voila/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // the use of the `env!()` macro instead of 3 | // its `option_env!()` counterpart is on purpose, 4 | // so if the data needed to run is not provided, a compile-time 5 | // error will be thrown instead of a runtime one. 6 | if let Err(ref e) = voila::exec( 7 | str_to_vec_u8(env!("v_code")).into(), // into() automatically deserializes the data 8 | std::path::PathBuf::from(env!("v_path")), 9 | env!("v_recursive").parse().unwrap(), 10 | ) { 11 | eprintln!("{}", e); 12 | std::process::exit(1); 13 | } 14 | } 15 | 16 | fn str_to_vec_u8(mut str: &str) -> Vec { 17 | str = str.strip_prefix('[').unwrap().strip_suffix(']').unwrap(); 18 | str.split(", ").map(|n| n.parse::().unwrap()).collect() 19 | } 20 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | edition = "2021" 2 | match_block_trailing_comma = true 3 | -------------------------------------------------------------------------------- /voila/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "voila" 3 | description = "A tool for doing complex operations to files and directories." 4 | authors = ["Guillem Jara <4lon3ly0@tutanota.com>"] 5 | license = "MIT" 6 | readme = "../README.md" 7 | documentation = "https://github.com/Alonely0/voila/wiki" 8 | homepage = "https://github.com/Alonely0/voila/" 9 | repository = "https://github.com/Alonely0/voila/" 10 | version = "3.4.1" 11 | edition = "2021" 12 | exclude = [ 13 | "ROADMAP.md", 14 | "CODE_OF_CONDUCT.md", 15 | "CONTRIBUTING.md", 16 | "target", 17 | ".*", 18 | ] 19 | categories = ["command-line-utilities", "compilers"] 20 | keywords = [ 21 | "files", 22 | "domain-specific-lang", 23 | "command-line-utils", 24 | "compilers", 25 | "cli", 26 | ] 27 | 28 | [dependencies] 29 | md5 = "0.7.0" 30 | sha2 = "0.9.5" 31 | sha-1 = "0.9.7" 32 | walkdir = "2.3.2" 33 | regex = "1.5.4" 34 | structopt = "0.3.23" 35 | logos = "0.12.0" 36 | async-trait = "0.1.51" 37 | rayon = "1.5.1" 38 | ring = "0.16.20" 39 | enum_dispatch = "0.3.7" 40 | threadpool = "1.8.1" 41 | num_cpus = "1.13.0" 42 | fs_extra = "1.2.0" 43 | byte-unit = "4.0.12" 44 | async-stream = "0.3.2" 45 | chrono = "0.4.19" 46 | flate2 = "1.0.21" 47 | tar = "0.4.37" 48 | ansi_term = "0.12.1" 49 | path-absolutize = "3.0.11" 50 | serde = "1.0.130" 51 | serde_derive = "1.0.130" 52 | bincode = "1.3.3" 53 | 54 | [build-dependencies] 55 | tar = "0.4.37" 56 | flate2 = "1.0.21" 57 | 58 | [dev-dependencies] 59 | criterion = "0.3.5" 60 | 61 | [lib] 62 | name = "voila" 63 | path = "src/lib.rs" 64 | 65 | [[bench]] 66 | name = "benchmark" 67 | harness = false 68 | -------------------------------------------------------------------------------- /voila/benches/benchmark.rs: -------------------------------------------------------------------------------- 1 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; 2 | use voila::run; 3 | 4 | fn criterion_benchmark(c: &mut Criterion) { 5 | c.bench_function("Voila, [benchmark name here]", |b| { 6 | b.iter(|| { 7 | run( 8 | black_box("@name == @name { print(@name.file); print(@a, @parent) }".to_string()), 9 | black_box(std::path::PathBuf::from(env!("HOME"))), 10 | black_box(true), 11 | ) 12 | }) 13 | }); 14 | } 15 | 16 | criterion_group!(benches, criterion_benchmark); 17 | criterion_main!(benches); 18 | -------------------------------------------------------------------------------- /voila/build.rs: -------------------------------------------------------------------------------- 1 | use flate2::write::GzEncoder; 2 | use flate2::Compression; 3 | use std::env::current_dir; 4 | use std::fs::File; 5 | 6 | const COMPILE_ASSETS: [&str; 4] = ["voila", "compiled_voila", "Cargo.toml", "Cargo.lock"]; 7 | 8 | fn main() { 9 | let src = ¤t_dir().expect("Can't open current directory"); 10 | let src = src.parent().unwrap(); 11 | let dest = File::create(format!("{}/code.tar.gz", src.to_str().unwrap())) 12 | .expect("Can't write to current directory"); 13 | println!("{}/code.tar.gz", src.to_str().unwrap()); 14 | let encoder = GzEncoder::new(dest, Compression::default()); 15 | let mut tar = tar::Builder::new(encoder); 16 | for a in COMPILE_ASSETS { 17 | let p = src.join(a); 18 | if p.is_dir() { 19 | tar.append_dir_all(a, p) 20 | .expect("Can't read current directory"); 21 | } else { 22 | tar.append_path_with_name(p, a) 23 | .expect("Can't read current directory"); 24 | } 25 | } 26 | tar.finish().unwrap(); 27 | } 28 | -------------------------------------------------------------------------------- /voila/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly 2 | -------------------------------------------------------------------------------- /voila/src/ast/call.rs: -------------------------------------------------------------------------------- 1 | use super::parser::{ContextLevel, Parse, ParseErrorKind, ParseRes, Parser, WantedSpec}; 2 | use super::HasSpan; 3 | use super::Str; 4 | use super::Token; 5 | use serde_derive::{Deserialize, Serialize}; 6 | use std::fmt; 7 | use std::io; 8 | 9 | use std::ops::Range; 10 | 11 | /// Represents a call in the script like 12 | /// `shell` or `delete`. All functions receive the arguments in 13 | /// interpolated strings, which will have their variables resolved 14 | /// before execution. 15 | /// 16 | /// # List of non-destructive functions 17 | /// These functions are safe to use without any worries that 18 | /// any data will be eliminated: 19 | /// - `print` 20 | /// - `mkdir` 21 | /// `shell` won't be in the list, since you can do absolutely anything 22 | /// when we give you a shell. The shell is an escape hatch to enable the 23 | /// integration of Voila to the rest of the system. 24 | #[derive(Serialize, Deserialize, Debug)] 25 | pub struct Call<'source> { 26 | pub function_kind: Function, 27 | pub arguments: Vec>, 28 | pub safe: bool, 29 | span: Range, 30 | } 31 | 32 | /// The function that is [`Call`]ed. 33 | /// 34 | /// # Panic 35 | /// The interpreter will panic when the function has not 36 | /// enough arguments to execute. 37 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 38 | pub enum Function { 39 | /// Create a directory with its parents recursively. 40 | /// This function is not destructive, it will error 41 | /// if a file with the name of the directory exists already. 42 | /// 43 | /// # Call format 44 | /// `mkdir` receives at least one argument: the path to create. 45 | /// You can put more directories to create, but make sure to separate them 46 | /// by commas! 47 | Mkdir { safe: bool }, 48 | /// Print something to standard output 49 | /// 50 | /// # Call format 51 | /// `print` receives a variadic number of arguments which it prints 52 | /// separated by spaces (similar to python's print function without parameters), and a newline 53 | /// after. 54 | Print { safe: bool }, 55 | /// Execute a command in a shell 56 | /// 57 | /// # Call format 58 | /// `shell` needs at least one argument. When called, it gets all the arguments, joins all by spaces 59 | /// and feeds that to `sh -c` in the case of linux and `powershell` on windows. No input is given to it, so things like `sudo` 60 | /// won't work unless you start voila with privileges 61 | /// 62 | /// # Safety 63 | /// This function may modify the outer system! 64 | Shell { safe: bool }, 65 | /// Delete the given files/directories 66 | /// 67 | /// # Call format 68 | /// `delete` receives at least one argument: the file/directory to delete. 69 | /// You can put more things to remove, but make sure to separate them by commas! 70 | /// Directories are deleted recursively! 71 | /// 72 | /// # Safety 73 | /// `delete` will modify the outer system! Make sure that you're not doing 74 | /// accesses to the file in the argument on the same cycle, otherwise you will 75 | /// get undefined behavior. 76 | Delete { safe: bool }, 77 | /// Moves or renames a file, with a similar behavior to the `mv` command. 78 | /// 79 | /// # Call format 80 | /// `move` receives two arguments: the source file/directory and the target destination 81 | /// 82 | /// # Safety 83 | /// `move` is a destructive call, so please make sure that you're not using it with the same file in the same cycle. Refer to [`Function::Delete`] for details 84 | Move { safe: bool }, 85 | /// Copy a file or a directory. Directories are copied recursively. 86 | /// 87 | /// # Call format 88 | /// `copy` receives two arguments: the source file/directory and the target destination 89 | /// 90 | /// # Safety 91 | /// `copy` might overwrite files in the system, so use it carefully! Avoid using it in the same 92 | /// cycle unless you can prove it's safe to do so. 93 | Copy { safe: bool }, 94 | /// Gzip a file or a directory. Directories are gzipped recursively. 95 | /// 96 | // NOTE: please rename this to `gzip` and `gunzip` like the binutils 97 | /// # Call format 98 | /// `gzc` receives two arguments: the source file/directory to compress and the file to save it 99 | /// into. Note that the destination name is not manipulated in any way (nothing is added or 100 | /// removed to it) 101 | /// 102 | /// # Safety 103 | /// Since `gzc` has an output file. it may overwrite another that's in the system. 104 | GzipCompress { safe: bool }, 105 | /// Gunzip a file into a file/directory. 106 | /// 107 | /// # Call format 108 | /// `gzd` receives two arguments: the gzipped file, and the destination to decompress into. 109 | /// The destination, if not specified, is the directory in which the gzipped file is, **not the 110 | /// one that voila is executing in** 111 | /// 112 | /// # Safety 113 | /// since `gzd` has an output directory, it may overwrite a lot af files! Use with care. 114 | GzipDecompress { safe: bool }, 115 | /// Create a file, with optional contents 116 | /// 117 | /// # Call format 118 | /// `create` receives the file to create and an optional second argument with the contents 119 | /// 120 | /// # Safety 121 | /// `create` will modify the file system! 122 | Create { safe: bool }, 123 | /// Execute an executable as a child process, ignoring the cycle.await 124 | /// 125 | /// # Call format 126 | /// `child` receives either a binary or a path pointing to a executable. All other arguments 127 | /// are passed as arguments 128 | /// 129 | /// # Safety 130 | /// As safe as the executable is. Like in the shell function, the safety checker will treat 131 | /// arguments as access, modify and created as has 0 information about what the executable will do 132 | Child { safe: bool }, 133 | } 134 | 135 | impl Function { 136 | pub const fn minimum_arg_count(&self) -> u8 { 137 | match self { 138 | Self::Copy { safe: _ } 139 | | Self::Move { safe: _ } 140 | | Self::GzipCompress { safe: _ } 141 | | Self::GzipDecompress { safe: _ } => 2, 142 | Self::Delete { safe: _ } 143 | | Self::Shell { safe: _ } 144 | | Self::Mkdir { safe: _ } 145 | | Self::Create { safe: _ } 146 | | Self::Child { safe: _ } => 1, 147 | Self::Print { safe: _ } => 0, 148 | } 149 | } 150 | fn from_name(source: &str, safe: bool) -> Option { 151 | Some(match source.trim() { 152 | "copy" => Self::Copy { safe }, 153 | "move" => Self::Move { safe }, 154 | "gzc" => Self::GzipCompress { safe }, 155 | "gzd" => Self::GzipDecompress { safe }, 156 | "delete" => Self::Delete { safe }, 157 | "shell" => Self::Shell { safe }, 158 | "mkdir" => Self::Mkdir { safe }, 159 | "print" => Self::Print { safe }, 160 | "create" => Self::Create { safe }, 161 | "child" => Self::Child { safe }, 162 | _ => return None, 163 | }) 164 | } 165 | fn is_safe(&self) -> bool { 166 | match *self { 167 | Function::Mkdir { safe } 168 | | Function::Print { safe } 169 | | Function::Shell { safe } 170 | | Function::Delete { safe } 171 | | Function::Copy { safe } 172 | | Function::Move { safe } 173 | | Function::GzipCompress { safe } 174 | | Function::GzipDecompress { safe } 175 | | Function::Create { safe } 176 | | Function::Child { safe } => safe, 177 | } 178 | } 179 | } 180 | 181 | impl fmt::Display for Function { 182 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 183 | f.write_str(match self { 184 | Self::Copy { safe: _ } => "copy", 185 | Self::Move { safe: _ } => "move", 186 | Self::GzipCompress { safe: _ } => "gzc", 187 | Self::GzipDecompress { safe: _ } => "gzd", 188 | Self::Delete { safe: _ } => "delete", 189 | Self::Shell { safe: _ } => "shell", 190 | Self::Mkdir { safe: _ } => "mkdir", 191 | Self::Print { safe: _ } => "print", 192 | Self::Create { safe: _ } => "create", 193 | Self::Child { safe: _ } => "child", 194 | }) 195 | } 196 | } 197 | 198 | impl Parse<'_> for Function { 199 | fn parse(parser: &mut Parser) -> ParseRes { 200 | // this doesn't accept the token (unless unsafe is found) because 201 | // it is accepted by the calling parser (`Call`), so it can use 202 | // the identifier start as a more accurate start of the function span. 203 | let mut safe = true; 204 | let mut src = parser.expect_token( 205 | Token::Identifier, 206 | Some("as the name of the function or unsafe statement"), 207 | )?; 208 | if src == "unsafe" { 209 | safe = false; 210 | parser.accept_current(); 211 | src = parser.expect_token(Token::Identifier, Some("as the name of the function"))? 212 | } 213 | Self::from_name(src, safe).ok_or_else(|| parser.error(ParseErrorKind::UnknownFunction)) 214 | } 215 | } 216 | 217 | impl<'source> Call<'source> { 218 | pub fn offset(&self) -> usize { 219 | self.span.start 220 | } 221 | } 222 | 223 | impl HasSpan for Call<'_> { 224 | fn span(&self) -> &Range { 225 | &self.span 226 | } 227 | } 228 | 229 | impl<'source> Parse<'source> for Call<'source> { 230 | fn parse(parser: &mut Parser<'source>) -> ParseRes { 231 | parser.with_context(ContextLevel::Call, |parser| { 232 | let function_kind = parser.parse()?; 233 | let start = parser.current_token_span().start; 234 | parser.accept_current(); 235 | parser.expect_token( 236 | Token::OpenParen, 237 | Some("to begin the function call arguments"), 238 | )?; 239 | parser.accept_current(); 240 | let mut arguments = Vec::new(); 241 | 242 | loop { 243 | if parser.expect_any_token(Some( 244 | WantedSpec::explicit_multiple(vec![ 245 | Token::Variable, 246 | Token::Identifier, 247 | Token::CloseParen, 248 | ]) 249 | .with_explanation("end of argument list or argument to the function"), 250 | ))? == Token::CloseParen 251 | { 252 | break; 253 | } 254 | arguments.push(parser.parse()?); 255 | if parser.expect_any_token(Some( 256 | WantedSpec::explicit_multiple(vec![Token::CloseParen, Token::Identifier]) 257 | .with_explanation("end of argument list or comma to continue it"), 258 | ))? != Token::Comma 259 | { 260 | break; 261 | } 262 | parser.accept_current() 263 | } 264 | parser.expect_token(Token::CloseParen, Some("to end the argument list"))?; 265 | let end = parser.current_token_span().end; 266 | parser.accept_current(); 267 | Ok(Self { 268 | function_kind, 269 | arguments, 270 | safe: function_kind.is_safe(), 271 | span: start..end, 272 | }) 273 | }) 274 | } 275 | } 276 | use crate::interpreter::{Cache, ErrorKind, ExprResult}; 277 | use path_absolutize::*; 278 | use std::path::PathBuf; 279 | use std::sync::{Arc, Mutex}; 280 | 281 | pub fn run_call(call: &Call, cache: Arc>) -> Result<(), ErrorKind> { 282 | use crate::interpreter::ArgCountMismatched; 283 | 284 | // note: already considered streaming the arguments instead 285 | // of collecting all of them, but the number of arguments is very low (1 or 2), 286 | // so there is no real performance hit if we evaluate all of them now. 287 | let mut args: Vec = call 288 | .arguments 289 | .iter() 290 | // note: grabbing the cache lock on each argument separately to prevent locking 291 | // the cache too much time, e.g in a cycle like 292 | // `print(@sum=sha256 @sum=sha512 @sum=md5) print(@path)`, 293 | // if the first `print` grabs the cache first, it will only prevent the second `print` from 294 | // executing while it's calculating the SHA256 sum, then the second print will be executed 295 | .map(|arg| cache.lock().unwrap().resolve(arg)) 296 | .map(|x| x.map(ExprResult::cast_to_string)) 297 | .collect::>()?; 298 | // drop the guard now since we're finished 299 | drop(cache); 300 | 301 | ArgCountMismatched::check(call.function_kind, args.len())?; 302 | 303 | // todo: error contexts in interpreter errors... 304 | match call.function_kind { 305 | Function::Print { safe: _ } => print(args), 306 | Function::Create { safe: _ } => create(&args[0], args.get(1).map(String::as_str)), 307 | Function::Mkdir { safe: _ } => mkdir(args), 308 | Function::Delete { safe: _ } => delete(args), 309 | Function::Copy { safe: _ } => { 310 | copy_file_or_dir(args[0].as_str().into(), args[1].as_str().into()) 311 | }, 312 | Function::Move { safe: _ } => move_file(&args[0], &args[1]), 313 | Function::GzipCompress { safe: _ } => gzc(&args[0], &args[1]), 314 | Function::GzipDecompress { safe: _ } => gzd(&args[0], &args[1]), 315 | Function::Shell { safe: _ } => shell(args), 316 | Function::Child { safe: _ } => child(&args.remove(0), args), 317 | } 318 | .map_err(Into::into) 319 | } 320 | 321 | fn print(args: Vec) -> Result<(), io::Error> { 322 | let args = args 323 | .into_iter() 324 | .enumerate() 325 | .fold(String::new(), |acc, (i, next)| { 326 | acc + if i > 0 { " " } else { "" } + &next 327 | }); 328 | let stdout = io::stdout(); 329 | // lock stdout since we're executing in multithread 330 | let mut stdout = stdout.lock(); 331 | use io::Write; 332 | 333 | stdout.write_all(args.as_bytes())?; 334 | stdout.write_all(b"\n")?; 335 | stdout.flush() 336 | } 337 | 338 | fn create(dest: &str, content: Option<&str>) -> Result<(), io::Error> { 339 | use std::fs; 340 | fs::write(dest, content.unwrap_or("")) 341 | } 342 | 343 | fn mkdir(dirs: Vec) -> Result<(), io::Error> { 344 | use std::fs; 345 | dirs.into_iter().try_for_each(fs::create_dir_all) 346 | } 347 | 348 | fn delete(files: Vec) -> Result<(), io::Error> { 349 | files.into_iter().try_for_each(|x| delete_file_or_dir(&x)) 350 | } 351 | 352 | fn delete_file_or_dir(target: &str) -> Result<(), io::Error> { 353 | use std::fs; 354 | let mut t = PathBuf::from(target); 355 | let metadata = match fs::metadata(target) { 356 | Ok(meta) => meta, 357 | Err(_) => return Ok(()), 358 | }; 359 | 360 | if t.is_relative() { 361 | t = t.absolutize()?.into(); 362 | } 363 | 364 | if metadata.is_dir() { 365 | fs::remove_dir_all(t) 366 | } else { 367 | fs::remove_file(t) 368 | } 369 | } 370 | 371 | fn copy_file_or_dir(mut source: PathBuf, mut dest: PathBuf) -> Result<(), io::Error> { 372 | use std::fs; 373 | 374 | if source.is_relative() { 375 | source = source.absolutize()?.into(); 376 | } 377 | if dest.is_relative() { 378 | dest = dest.absolutize()?.into(); 379 | } 380 | 381 | if dest.exists() && dest.is_dir() { 382 | dest = dest.join( 383 | source 384 | .file_name() 385 | .and_then(std::ffi::OsStr::to_str) 386 | .unwrap(), 387 | ); 388 | } 389 | 390 | if source.is_dir() { 391 | fs::create_dir_all(dest)?; 392 | } else { 393 | fs::copy(source, dest)?; 394 | } 395 | Ok(()) 396 | } 397 | 398 | fn move_file(source: &str, dest: &str) -> Result<(), io::Error> { 399 | copy_file_or_dir(source.into(), dest.into())?; 400 | delete_file_or_dir(source) 401 | } 402 | 403 | fn gzc(source: &str, dest: &str) -> Result<(), io::Error> { 404 | use flate2::write::GzEncoder; 405 | use flate2::Compression; 406 | use std::fs; 407 | 408 | let dest = fs::File::create(dest)?; 409 | let encoder = GzEncoder::new(dest, Compression::default()); 410 | let mut tar = tar::Builder::new(encoder); 411 | let source = PathBuf::from(source); 412 | if source.is_dir() { 413 | tar.append_dir_all(source.clone(), source)?; 414 | } else { 415 | tar.append_path(source)?; 416 | } 417 | tar.finish() 418 | } 419 | 420 | fn gzd(source: &str, dest: &str) -> Result<(), io::Error> { 421 | use flate2::read::GzDecoder; 422 | use std::fs::File; 423 | use tar::Archive; 424 | 425 | let mut archive = Archive::new(GzDecoder::new(File::open(source)?)); 426 | archive.unpack(dest) 427 | } 428 | 429 | use std::process::Command; 430 | 431 | fn shell(commands: Vec) -> Result<(), io::Error> { 432 | commands.into_iter().try_for_each(|cmd| { 433 | let complete_command: Result<_, std::io::Error> = { 434 | #[cfg(windows)] 435 | { 436 | let mut initial = Command::new("powershell"); 437 | initial.arg("-Command"); 438 | Ok(initial) 439 | } 440 | 441 | #[cfg(unix)] 442 | { 443 | let mut initial = Command::new("sh"); 444 | initial.arg("-c"); 445 | Ok(initial) 446 | } 447 | 448 | #[cfg(not(any(unix, windows)))] 449 | Err(std::io::Error::new( 450 | std::io::ErrorKind::Other, 451 | "Voila's shell is only supported on Windows & Unix-like systems", 452 | )) 453 | }; 454 | let mut complete_command = complete_command?; 455 | 456 | complete_command.arg(cmd); 457 | // question is: will this thread join with rayon threadpool? 458 | // TODO: refactor this to use the thread pool. 459 | complete_command.spawn()?.wait()?; 460 | 461 | Ok(()) 462 | }) 463 | } 464 | 465 | fn child(executable: &str, arguments: Vec) -> Result<(), io::Error> { 466 | Command::new(executable) 467 | .args(arguments) 468 | .spawn() 469 | .map(|_| ()) 470 | } 471 | -------------------------------------------------------------------------------- /voila/src/ast/cycle.rs: -------------------------------------------------------------------------------- 1 | use super::Call; 2 | use super::HasSpan; 3 | use serde_derive::{Deserialize, Serialize}; 4 | use std::ops::Range; 5 | 6 | /// A cycle is a group of calls that are run asynchronously. 7 | /// Every cycle must finish completely before the next cycle 8 | /// starts executing. 9 | /// 10 | /// # Safety 11 | /// Note that inside the same cycle, all the functions will be executed 12 | /// in their own thread. This means that destructive functions might cause 13 | /// data races in the system, so use them carefully! 14 | /// 15 | /// # Example 16 | /// ```voila 17 | /// @size=gb > 1 { 18 | /// print(@name is too big. Deleting it) 19 | /// delete(@path) 20 | /// ; 21 | /// print(@name has been deleted) 22 | /// } 23 | /// ``` 24 | /// 25 | /// In this example, first `print` will be executid while the file is being deleted, 26 | /// and the second `pritn` will be executed when the file has been deleted successfully. 27 | #[derive(Serialize, Deserialize, Debug)] 28 | pub struct Cycle<'source> { 29 | pub calls: Vec>, 30 | span: Range, 31 | } 32 | 33 | impl HasSpan for Cycle<'_> { 34 | fn span(&self) -> &Range { 35 | &self.span 36 | } 37 | } 38 | 39 | use super::parser::*; 40 | use super::Token; 41 | 42 | impl<'source> Parse<'source> for Cycle<'source> { 43 | fn parse(parser: &mut Parser<'source>) -> ParseRes { 44 | parser.with_context(ContextLevel::Cycle, |parser| { 45 | let start = parser.offset(); 46 | let mut calls = Vec::new(); 47 | loop { 48 | match parser.expect_one_of_tokens( 49 | &[Token::CloseBrace, Token::Identifier, Token::Semicolon], 50 | Some("safe/unsafe function or end of cycle/target"), 51 | )? { 52 | Token::CloseBrace => break, 53 | Token::Semicolon => { 54 | parser.accept_current(); 55 | break; 56 | }, 57 | Token::Identifier => calls.push(parser.parse()?), 58 | _ => unreachable!(), 59 | } 60 | } 61 | let end = parser.offset(); 62 | Ok(Self { 63 | calls, 64 | span: start..end, 65 | }) 66 | }) 67 | } 68 | } 69 | 70 | use crate::interpreter; 71 | use std::sync::{mpsc, Arc, Mutex}; 72 | 73 | pub fn run_cycle( 74 | cycle: &Cycle, 75 | cache: Arc>, 76 | pool: &rayon::ThreadPool, 77 | tx: mpsc::Sender, 78 | ) { 79 | pool.scope(move |s| { 80 | for call in &cycle.calls { 81 | let cache = cache.clone(); 82 | let tx = tx.clone(); 83 | s.spawn(move |_| { 84 | if let Err(e) = super::run_call(call, cache) { 85 | tx.send(e).unwrap(); 86 | } 87 | }) 88 | } 89 | }); 90 | } 91 | -------------------------------------------------------------------------------- /voila/src/ast/expr.rs: -------------------------------------------------------------------------------- 1 | use super::HasSpan; 2 | use super::Str; 3 | use super::Token; 4 | use serde_derive::{Deserialize, Serialize}; 5 | use std::ops::Range; 6 | 7 | // TODO: update `Expr` docs when static analyzer is brought into life 8 | 9 | /// The conditional to filter when a block will be executed. 10 | /// 11 | /// They are composed by [values](Value) and [operators](Operator). The expression ends up in a `bool`, 12 | /// which determines whether the block will be eecuted or not. 13 | /// 14 | /// 15 | /// # Panics: Coherence 16 | /// Voila doesn't have yet any way to check that the comparisons make sense before going and 17 | /// executing them, so it will panic whenever it finds a one that is ill-constructed. 18 | /// 19 | /// Because of this, the type rules are quite relaxed. But be careful with pattern matches and 20 | /// relational comparisons, because there is no other reasonable way to relax those rules without 21 | /// breaking the consistency of the operator. 22 | /// 23 | /// # Examples 24 | /// ```voila 25 | /// @size=mb > 1.23 && @txt { ... } 26 | /// ``` 27 | /// ```voila 28 | /// @sha256sum ~= #.*e0.*# { ... } 29 | /// ``` 30 | #[derive(Serialize, Deserialize, Debug)] 31 | pub enum Expr<'source> { 32 | Value(Str<'source>), 33 | Binary { 34 | operator: Operator, 35 | lhs: Box>, 36 | rhs: Box>, 37 | span: Range, 38 | }, 39 | } 40 | 41 | impl HasSpan for Expr<'_> { 42 | fn span(&self) -> &Range { 43 | match self { 44 | Self::Value(val) => val.span(), 45 | Self::Binary { span, .. } => span, 46 | } 47 | } 48 | } 49 | 50 | /// The operators than help build [Expr]s 51 | /// # Supported operators 52 | /// The currently supported operators are: 53 | /// - Equality operators: `==` and `!=` 54 | /// Currently the comparison is string-based, although that might change it the future. 55 | /// - Relative operators: `>=`, `<=`, `>` and `<` 56 | /// These comparisons are number-based on both sides. The numbers can be integers 57 | /// or decimal numbers, which will be cut to a precision of 2 digits. 58 | /// - Pattern match operators: `~=` and `!~` 59 | /// The left hand side will always be converted to a string, and the right hand side 60 | /// must be a valid regex. 61 | /// - Logic operators: `&&` and `||` 62 | /// Both sides must result in a bool. Anything that is not a bool (regex, string, variable) will 63 | /// become true for the moment. There are plans to forbid this in the future with a static 64 | /// analyzer. 65 | /// 66 | #[derive(Serialize, Deserialize, Debug)] 67 | pub enum Operator { 68 | /// `!=`: True if the two sides are strictly not equal. 69 | NEquals, 70 | /// `==`: True if the two sides are strictly equal. 71 | Equals, 72 | /// `~=`: True if the string matches the regex 73 | Matches, 74 | /// `!~`: True if the string doesn't match the regex 75 | NMatches, 76 | /// `&&`: True if both sides are true. 77 | LogicAnd, 78 | /// `||`: True if either of the sides is true. 79 | LogicOr, 80 | /// `<` : True if the left hand side is strictly less than the right hand side 81 | LessThan, 82 | /// `<=`: True if the left hand side is less than, or equal to, the right hand side 83 | LessEqual, 84 | /// `>` : True if the left hand side is strictly greater than the right hand side 85 | GreaterThan, 86 | /// `>=`: True if the left hand side is greater than, or equal to, the right hand side 87 | GreaterEqual, 88 | } 89 | 90 | impl Operator { 91 | const fn precedence(&self) -> u8 { 92 | match self { 93 | Self::LogicOr => 0, 94 | Self::LogicAnd => 1, 95 | Self::NEquals 96 | | Self::NMatches 97 | | Self::Matches 98 | | Self::Equals 99 | | Self::LessEqual 100 | | Self::LessThan 101 | | Self::GreaterEqual 102 | | Self::GreaterThan => 2, 103 | } 104 | } 105 | fn from_token(tok: Token) -> Option { 106 | Some(match tok { 107 | Token::NEquals => Self::NEquals, 108 | Token::NMatch => Self::NMatches, 109 | Token::Equals => Self::Equals, 110 | Token::Match => Self::Matches, 111 | Token::LogicAnd => Self::LogicAnd, 112 | Token::LogicOr => Self::LogicOr, 113 | Token::LThan => Self::LessThan, 114 | Token::LEq => Self::LessEqual, 115 | Token::GThan => Self::GreaterThan, 116 | Token::GEq => Self::GreaterEqual, 117 | _ => return None, 118 | }) 119 | } 120 | } 121 | 122 | use super::parser::*; 123 | 124 | impl<'source> Parse<'source> for Expr<'source> { 125 | fn parse(parser: &mut Parser<'source>) -> ParseRes { 126 | parser.with_context(ContextLevel::Condition, |parser| { 127 | parser 128 | .parse() 129 | .map(Expr::Value) 130 | .and_then(|lhs| parse_expr(parser, lhs, 0)) 131 | }) 132 | } 133 | } 134 | 135 | fn parse_expr<'source>( 136 | parser: &mut Parser<'source>, 137 | mut lhs: Expr<'source>, 138 | min_precedence: u8, 139 | ) -> ParseRes> { 140 | while let Some(op) = parser 141 | .current_token()? 142 | .and_then(Operator::from_token) 143 | .filter(|x| x.precedence() >= min_precedence) 144 | { 145 | parser.accept_current(); 146 | let mut rhs = parser.parse().map(Expr::Value)?; 147 | while parser 148 | .current_token()? 149 | .and_then(Operator::from_token) 150 | .filter(|op| op.precedence() > op.precedence()) 151 | .is_some() 152 | { 153 | rhs = parse_expr(parser, rhs, min_precedence + 1)?; 154 | } 155 | let rhs_end = rhs.span().end; 156 | lhs = Expr::Binary { 157 | span: lhs.span().start..rhs_end, 158 | operator: op, 159 | lhs: Box::new(lhs), 160 | rhs: Box::new(rhs), 161 | }; 162 | } 163 | Ok(lhs) 164 | } 165 | 166 | use crate::interpreter::{Cache, ErrorKind, ExprResult, Resolve}; 167 | impl Resolve for Expr<'_> { 168 | fn resolve(&self, cache: &mut Cache) -> Result { 169 | match self { 170 | Self::Value(v) => cache.resolve(v), 171 | Self::Binary { 172 | operator, lhs, rhs, .. 173 | } => { 174 | let lhs = cache.resolve(lhs.as_ref())?; 175 | let rhs = cache.resolve(rhs.as_ref())?; 176 | Ok(ExprResult::from(match operator { 177 | Operator::Equals => lhs.cast_to_string() == rhs.cast_to_string(), 178 | Operator::GreaterEqual => { 179 | lhs.reinterpret().cast_to_number()? >= rhs.reinterpret().cast_to_number()? 180 | }, 181 | Operator::GreaterThan => { 182 | lhs.reinterpret().cast_to_number()? > rhs.reinterpret().cast_to_number()? 183 | }, 184 | Operator::LessEqual => { 185 | lhs.reinterpret().cast_to_number()? <= rhs.reinterpret().cast_to_number()? 186 | }, 187 | Operator::LessThan => { 188 | lhs.reinterpret().cast_to_number()? < rhs.reinterpret().cast_to_number()? 189 | }, 190 | Operator::NEquals => lhs.cast_to_string() != rhs.cast_to_string(), 191 | // regex will be always on the right...? 192 | // NOTE: now that I think about it, it should be on the left, right? 193 | // like matches . I'll keep it consistent with how 194 | // it's documented thaugh, and when we change the behavior we should 195 | // update the docs. TODO: Think about the places in the matches and 196 | // update the description on the README + operator enum before changing 197 | // the behavior code. 198 | Operator::Matches => rhs.cast_to_regex()?.is_match(&lhs.cast_to_string()), 199 | Operator::NMatches => rhs.cast_to_regex()?.is_match(&lhs.cast_to_string()), 200 | // note: using the single ones so a shortcut is not generated, 201 | // and the casts are made first. This won't be relevant when types 202 | // are validated prior to runtime. 203 | Operator::LogicAnd => lhs.cast_to_bool()? & rhs.cast_to_bool()?, 204 | Operator::LogicOr => lhs.cast_to_bool()? | rhs.cast_to_bool()?, 205 | })) 206 | }, 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /voila/src/ast/lookup.rs: -------------------------------------------------------------------------------- 1 | use super::parser::{Parse, ParseErrorKind, ParseRes, Parser}; 2 | use crate::interpreter::{Hash, Hasher}; 3 | use serde_derive::{Deserialize, Serialize}; 4 | 5 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] 6 | pub enum Lookup { 7 | /// The file name (basename) 8 | Name, 9 | /// The complete file path 10 | Path, 11 | /// Absolute path to the file's parent directory 12 | Parent, 13 | /// File owner ID (unix-only) 14 | #[cfg(unix)] 15 | OwnerID, 16 | /// Whether the file occupies than 1 byte 17 | Empty, 18 | /// Whether the file is read only (for the user that runs this process) 19 | Readonly, 20 | /// Whether the file follows the Executable & Linkable Format 21 | Elf, 22 | /// Whether the file is a valid text file 23 | Text, 24 | /// File content 25 | Content, 26 | /// Access to a specific line of the file 27 | Line(usize), 28 | /// Whether the file is hidden 29 | Hidden, 30 | /// The file size 31 | Size(SizeLabel), 32 | /// A computed sum of the file's contents 33 | Sum(SumKind), 34 | /// time of file creation 35 | Creation(TimeStamp), 36 | /// time of the last modification 37 | LastModification(TimeStamp), 38 | /// time of the last access to the file 39 | LastAccess(TimeStamp), 40 | } 41 | 42 | use Lookup::*; 43 | 44 | impl Lookup { 45 | const VAR_OPTIONS: &'static [&'static str] = &[ 46 | "name", 47 | "path", 48 | "parent", 49 | #[cfg(unix)] 50 | "ownerID", 51 | "empty", 52 | "readonly", 53 | "elf", 54 | "txt", 55 | "content", 56 | "lines", 57 | "hidden", 58 | "size", 59 | "sum", 60 | "creation", 61 | "lastChange", 62 | "lastAccess", 63 | ]; 64 | pub fn as_str<'source>(&self) -> &'source str { 65 | match self { 66 | Name => "name", 67 | Path => "path", 68 | Parent => "parent", 69 | #[cfg(unix)] 70 | OwnerID => "ownerID", 71 | Empty => "empty", 72 | Readonly => "readonly", 73 | Elf => "elf", 74 | Text => "txt", 75 | Content => "content", 76 | Line(_) => "lines", 77 | Hidden => "hidden", 78 | Size(_) => "size=", 79 | Sum(_) => "sum=", 80 | Creation(_) => "creation=", 81 | LastModification(_) => "lastChange", 82 | LastAccess(_) => "lastAccess", 83 | } 84 | } 85 | } 86 | 87 | impl std::fmt::Display for Lookup { 88 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 89 | write!(f, "{lookup}", lookup = self.as_str()) 90 | } 91 | } 92 | 93 | impl Parse<'_> for Lookup { 94 | // this parser assumes that the parser is already at `Token::Variable` 95 | fn parse(parser: &mut Parser) -> ParseRes { 96 | use crate::{no_spec, spec}; 97 | 98 | let full_var = parser.current_token_source().strip_prefix('@').unwrap(); 99 | let (var_name, var_spec): (&str, Option<&str>) = full_var 100 | .find('=') 101 | .map(|idx| { 102 | let (a, b) = full_var.split_at(idx); 103 | (a, Some(&b[1..])) 104 | }) 105 | .unwrap_or((full_var, None)); 106 | match var_name { 107 | "name" => no_spec!("name", Name, var_spec), 108 | "path" => no_spec!("path", Path, var_spec), 109 | "parent" => no_spec!("parent", Parent, var_spec), 110 | #[cfg(unix)] 111 | "ownerID" => no_spec!("ownerID", OwnerID, var_spec), 112 | "empty" => no_spec!("empty", Empty, var_spec), 113 | "readonly" => no_spec!("readonly", Readonly, var_spec), 114 | "elf" => no_spec!("elf", Elf, var_spec), 115 | "txt" => no_spec!("txt", Text, var_spec), 116 | "content" => no_spec!("content", Content, var_spec), 117 | "line" => spec!("line", usize, Line, var_spec), 118 | "hidden" => no_spec!("hidden", Hidden, var_spec), 119 | "size" => spec!("size", SizeLabel, Size, var_spec), 120 | "sum" => spec!("sum", SumKind, Sum, var_spec), 121 | "creation" => spec!("creation", TimeStamp, Creation, var_spec), 122 | "lastChange" => spec!("lastChange", TimeStamp, LastModification, var_spec), 123 | "lastAccess" => spec!("lastAccess", TimeStamp, LastAccess, var_spec), 124 | _ => Err(ParseErrorKind::UnknownVariable), 125 | } 126 | .map_err(|e| parser.error(e)) 127 | } 128 | } 129 | 130 | trait Specifier { 131 | const OPTS: [&'static str; O]; 132 | fn detect(source: &str) -> Option; 133 | } 134 | 135 | #[allow(clippy::enum_variant_names)] 136 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] 137 | pub enum SizeLabel { 138 | TeraBytes, 139 | GigaBytes, 140 | MegaBytes, 141 | KiloBytes, 142 | Bytes, 143 | } 144 | 145 | impl Specifier for SizeLabel { 146 | const OPTS: [&'static str; 5] = ["tb", "gb", "mb", "kb", "bs"]; 147 | fn detect(source: &str) -> Option { 148 | Some(match source { 149 | "tb" => Self::TeraBytes, 150 | "gb" => Self::GigaBytes, 151 | "mb" => Self::MegaBytes, 152 | "kb" => Self::KiloBytes, 153 | "bs" => Self::Bytes, 154 | _ => return None, 155 | }) 156 | } 157 | } 158 | 159 | impl std::fmt::Display for SizeLabel { 160 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 161 | f.write_str(match self { 162 | Self::TeraBytes => "tb", 163 | Self::GigaBytes => "gb", 164 | Self::MegaBytes => "mb", 165 | Self::KiloBytes => "kb", 166 | Self::Bytes => "bs", 167 | }) 168 | } 169 | } 170 | 171 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] 172 | pub enum SumKind { 173 | Md5, 174 | Sha1, 175 | Sha224, 176 | Sha256, 177 | Sha384, 178 | Sha512, 179 | } 180 | 181 | impl Specifier for SumKind { 182 | const OPTS: [&'static str; 6] = ["md5", "sha224", "sha256", "sha384", "sha512", "sha1"]; 183 | fn detect(source: &str) -> Option { 184 | Some(match source { 185 | "md5" => Self::Md5, 186 | "sha1" => Self::Sha1, 187 | "sha224" => Self::Sha224, 188 | "sha256" => Self::Sha256, 189 | "sha384" => Self::Sha384, 190 | "sha512" => Self::Sha512, 191 | _ => return None, 192 | }) 193 | } 194 | } 195 | 196 | impl std::fmt::Display for SumKind { 197 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 198 | f.write_str(match self { 199 | Self::Sha1 => "sha1", 200 | Self::Md5 => "md5", 201 | Self::Sha224 => "sha224", 202 | Self::Sha256 => "sha256", 203 | Self::Sha384 => "sha384", 204 | Self::Sha512 => "sha512", 205 | }) 206 | } 207 | } 208 | 209 | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] 210 | pub enum TimeStamp { 211 | /// Presented to the user as yyyy-mm-dd 212 | Date, 213 | /// Presented to the user as hh:mm:ss 214 | Hour, 215 | } 216 | 217 | impl Specifier for TimeStamp { 218 | const OPTS: [&'static str; 2] = ["date", "hour"]; 219 | fn detect(source: &str) -> Option { 220 | Some(match source { 221 | "date" => Self::Date, 222 | "hour" => Self::Hour, 223 | _ => return None, 224 | }) 225 | } 226 | } 227 | 228 | impl std::fmt::Display for TimeStamp { 229 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 230 | f.write_str(match self { 231 | Self::Date => "date", 232 | Self::Hour => "hour", 233 | }) 234 | } 235 | } 236 | 237 | impl Specifier for usize { 238 | const OPTS: [&'static str; 1] = ["any positive integer"]; 239 | fn detect(source: &str) -> Option { 240 | source.parse().ok() 241 | } 242 | } 243 | 244 | use crate::interpreter::{with_blocks, Cache, CachedResolve, ErrorKind, ExprResult, Resolve}; 245 | use std::time::SystemTime; 246 | impl CachedResolve for Lookup { 247 | fn cached_resolve(&self, cache: &mut Cache) -> Result { 248 | #[cfg(unix)] 249 | use std::os::unix::fs::MetadataExt; 250 | match self { 251 | Name => Ok(cache 252 | .get_path() 253 | .file_name() 254 | .and_then(std::ffi::OsStr::to_str) 255 | .unwrap() 256 | .into()), 257 | Path => cache 258 | .get_path() 259 | .canonicalize() 260 | .map_err(ErrorKind::from) 261 | .map(|path| path.to_str().unwrap().into()), 262 | // TODO: add error for not having parent 263 | Parent => cache 264 | .get_path() 265 | .parent() 266 | .unwrap() 267 | .canonicalize() 268 | .map_err(ErrorKind::from) 269 | .map(|path| path.to_str().unwrap().into()), 270 | 271 | #[cfg(unix)] 272 | OwnerID => cache 273 | .get_file_metadata() 274 | .map(|m| m.uid() as f64) 275 | .map(ExprResult::from), 276 | Empty => cache 277 | .get_file_metadata() 278 | .map(|m| m.len() <= 1) 279 | .map(ExprResult::from), 280 | Readonly => cache 281 | .get_file_metadata() 282 | .map(|m| m.permissions().readonly()) 283 | .map(ExprResult::from), 284 | Elf => { 285 | use std::io::Read; 286 | use std::io::Seek; 287 | let mut rb = [0u8; 4]; 288 | let br = cache.get_file_mut()?; 289 | // impl a partial reader that can seek wherever 290 | br.rewind().map_err(ErrorKind::from)?; 291 | let size = br.read(&mut rb).map_err(ErrorKind::from)?; 292 | Ok(size >= 4 && rb == [0x7f, b'E', b'L', b'F']).map(ExprResult::from) 293 | }, 294 | Text => { 295 | use std::io::Read; 296 | use std::io::Seek; 297 | use std::io::SeekFrom; 298 | 299 | let bufreader = cache.get_file_mut()?; 300 | bufreader.seek(SeekFrom::End(-1))?; 301 | let mut buf = [0u8; 1]; 302 | let last = if bufreader.read(&mut buf)? > 0 { 303 | Some(buf[0]) 304 | } else { 305 | None 306 | }; 307 | Ok(last == Some(b'\n') || last == Some(b'\r')).map(ExprResult::from) 308 | }, 309 | Content => { 310 | use std::ops::Deref; 311 | 312 | let mut reader = cache.get_file_mut()?; 313 | let mut buffer = Vec::new(); 314 | 315 | with_blocks(&mut reader, |block| buffer.extend_from_slice(block)).unwrap_or(()); 316 | 317 | Ok(String::from_utf8_lossy(&buffer).deref().into()) 318 | }, 319 | Line(l) => { 320 | use std::io::BufRead; 321 | 322 | let reader = cache.get_file_mut().unwrap(); 323 | let offset = vec![Ok(String::from(""))].into_iter(); 324 | 325 | Ok(offset 326 | .chain(reader.lines()) 327 | .nth(*l) 328 | .unwrap_or_else(|| Ok(String::new())) 329 | .unwrap_or_default() 330 | .into()) 331 | }, 332 | Hidden => { 333 | #[cfg(not(any(unix, windows)))] 334 | { 335 | println!("`hidden` variable is not natively supported in the current OS, falling back to unix implementation"); 336 | } 337 | #[cfg(not(windows))] 338 | { 339 | use std::ffi::OsStr; 340 | Ok(ExprResult::from( 341 | cache 342 | .get_path() 343 | .file_name() 344 | .unwrap_or_else(|| OsStr::new("")) 345 | .to_str() 346 | .unwrap_or("") 347 | .starts_with('.'), 348 | )) 349 | } 350 | #[cfg(windows)] 351 | { 352 | use std::os::windows::fs::MetadataExt; 353 | Ok(ExprResult::from( 354 | cache.get_file_metadata()?.file_attributes() & 0x2 > 0, 355 | )) // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants 356 | } 357 | }, 358 | Creation(ts) => { 359 | let created_time = cache.get_file_metadata()?.created()?; 360 | Ok(get_timestamp(created_time, ts)) 361 | }, 362 | LastModification(ts) => { 363 | let mod_time = cache.get_file_metadata()?.modified()?; 364 | Ok(get_timestamp(mod_time, ts)) 365 | }, 366 | LastAccess(ts) => { 367 | let last_access_time = cache.get_file_metadata()?.created()?; 368 | Ok(get_timestamp(last_access_time, ts)) 369 | }, 370 | // note: think about using Decimal (for the 2 decimal imposed precision): 371 | // https://crates.io/crates/rust-decimal 372 | Size(sz) => Ok(cache.get_file_metadata()?.len() as f64 373 | / match sz { 374 | SizeLabel::Bytes => 1.0, 375 | SizeLabel::KiloBytes => 1_000.0, 376 | SizeLabel::MegaBytes => 1_000_000.0, 377 | SizeLabel::GigaBytes => 1_000_000_000.0, 378 | SizeLabel::TeraBytes => 1_000_000_000_000.0, 379 | }) 380 | .map(ExprResult::from), 381 | Sum(sum) => { 382 | let hasher = Hasher::select_from_sum(*sum); 383 | hasher 384 | .hash_reader(cache.get_file_mut()?) 385 | .map(ExprResult::from) 386 | .map_err(ErrorKind::from) 387 | }, 388 | } 389 | } 390 | } 391 | impl Resolve for Lookup { 392 | fn resolve(&self, cache: &mut Cache) -> Result { 393 | cache.resolve_var(*self) 394 | } 395 | } 396 | 397 | use std::time::Duration; 398 | fn get_naive_datetime(duration: Duration) -> chrono::NaiveDateTime { 399 | let duration = chrono::Duration::from_std(duration).unwrap(); 400 | let unix = chrono::NaiveDateTime::from_timestamp(0, 0); 401 | unix + duration 402 | } 403 | 404 | fn get_timestamp(time: SystemTime, timestamp: &TimeStamp) -> ExprResult { 405 | let datetime = time 406 | .duration_since(SystemTime::UNIX_EPOCH) 407 | .map(get_naive_datetime) 408 | .unwrap(); 409 | match timestamp { 410 | TimeStamp::Date => ExprResult::Date(datetime.date()), 411 | TimeStamp::Hour => ExprResult::Time(datetime.time()), 412 | } 413 | } 414 | -------------------------------------------------------------------------------- /voila/src/ast/mod.rs: -------------------------------------------------------------------------------- 1 | use super::mod_use; 2 | use super::parser; 3 | use crate::lexer::Token; 4 | use std::ops::Range; 5 | 6 | // This trait will be useful later 7 | pub trait HasSpan { 8 | fn span(&self) -> &Range; 9 | } 10 | 11 | mod_use! { 12 | use script; 13 | use target; 14 | use expr; 15 | use cycle; 16 | use call; 17 | use lookup; 18 | use string; 19 | } 20 | 21 | pub fn parse_script(source: &str) -> parser::ParseRes