├── .gitignore ├── tests ├── 4k.jpg └── tests.rs ├── src ├── proc.rs ├── argp.rs └── main.rs ├── start.cmd ├── Cargo.toml ├── data ├── jsoparse.js └── commands.js └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /tests/4k.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/foto-image-transformer/master/tests/4k.jpg -------------------------------------------------------------------------------- /src/proc.rs: -------------------------------------------------------------------------------- 1 | // Main code 2 | 3 | pub fn execute(actions: Vec) { 4 | println!("{:?}", actions); 5 | } 6 | -------------------------------------------------------------------------------- /src/argp.rs: -------------------------------------------------------------------------------- 1 | // Arguments parser 2 | 3 | fn app(actions: Vec) { 4 | println!("{:?}", actions); 5 | } 6 | 7 | -------------------------------------------------------------------------------- /start.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | cargo watch -c -d 0 -x "test --no-fail-fast -q --message-format short --test tests -- tests --nocapture" -x "run --quiet" -q --use-shell cmd 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | # name = "foto-image-transformer" 3 | name = "foto" 4 | version = "0.1.0" 5 | edition = "2021" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | console = "0.15.0" 11 | serde_json = "1.0.74" 12 | regex = "1" -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | 3 | mod proc; 4 | 5 | // Process cli arguments 6 | fn main() { 7 | // proc::execute(std::env::args().skip(1).collect()); 8 | 9 | let regex = Regex::new(r"([0-9]{1,3})x([0-9]{1,3})").unwrap(); 10 | for cap in regex.captures("640x480").unwrap().iter().skip(1) { 11 | println!("{:?}", cap.unwrap().as_str()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /data/jsoparse.js: -------------------------------------------------------------------------------- 1 | const {readFileSync, accessSync, constants} = require("fs"); 2 | 3 | const path = process.argv[2]; 4 | 5 | if (!path) { 6 | console.log(""); 7 | } else { 8 | try { 9 | accessSync(path, constants.R_OK); 10 | const jsObjectAsTest = readFileSync(path).toString(); 11 | console.log(JSON.stringify(eval(jsObjectAsTest))); 12 | } catch (error) { 13 | console.log(""); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/tests.rs: -------------------------------------------------------------------------------- 1 | mod tests { 2 | use std::env; 3 | 4 | use json_minimal::Json; 5 | 6 | macro_rules! cast { 7 | ($target: expr, $pat: path) => {{ 8 | if let $pat(a) = $target { 9 | a 10 | } else { 11 | panic!("mismatch variant when cast to {}", stringify!($pat)); 12 | } 13 | }}; 14 | } 15 | 16 | fn parse_commands() -> Json { 17 | let out = std::process::Command::new("node") 18 | .arg("data/jsoparse.js") 19 | .arg("data/commands.js") 20 | .output() 21 | .expect("failed"); 22 | return Json::parse(&out.stdout).unwrap(); 23 | } 24 | 25 | #[test] 26 | fn test() { 27 | #[path = "../../src/proc.rs"] 28 | mod proc; 29 | 30 | let json = cast!(parse_commands(), Json::ARRAY); 31 | for a in json { 32 | match a.get("name").unwrap().unbox() { 33 | Json::JSON(vals) => { 34 | println!("{}", vals.len()); 35 | } 36 | Json::OBJECT { name, value } => match value { 37 | Json::STRING(val) => { 38 | println!("{}:{}", name, val); 39 | } 40 | }, 41 | _ => {} 42 | } 43 | } 44 | // proc::execute(vec![]); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /data/commands.js: -------------------------------------------------------------------------------- 1 | [ 2 | // temp:no halve clone png:100 640x480 3 | // 4 | // FLAG - standalone argument, does not have any parameters 5 | // COLON - accepts one or more paramerets (params) 6 | // MATCH - parameter is defined by a regular expression with arguments being capture groups 7 | { 8 | name: "temp", 9 | description: "Action group related to backup", 10 | structure: { 11 | type: "COLON", 12 | values: { 13 | no: { 14 | description: "Do not backup", 15 | // type: "string" // implicitly set to be 'string' 16 | }, 17 | yes: { 18 | // use later? 19 | description: "Do backup (default)", 20 | }, 21 | clear: { 22 | description: "Remove all backups defined in foto.bk", 23 | values: { 24 | // looks useless, but i have to leave it here 25 | // as an example of double parameter colon flag: 26 | // temp:clear:2 27 | "[0-9]+": { 28 | description: "Remove nth backup from foto.bk", 29 | }, 30 | }, 31 | }, 32 | save: { // expose this because why not? 33 | description: "Backup files (implicitly called before exiting when backup:yes)" 34 | } 35 | }, 36 | }, 37 | }, 38 | { 39 | name: "halve", 40 | description: "Halve the resolution", 41 | structure: { 42 | type: "FLAG", 43 | }, 44 | }, 45 | { 46 | name: "(png|jpg):([1-9][0-9]?0?)", 47 | description: "Set type:quality of the output file (0-100, higher is better)", 48 | structure: { 49 | type: "MATCH", 50 | types: ["u8"], 51 | } 52 | } 53 | ]; 54 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "console" 16 | version = "0.15.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "a28b32d32ca44b70c3e4acd7db1babf555fa026e385fb95f18028f88848b3c31" 19 | dependencies = [ 20 | "encode_unicode", 21 | "libc", 22 | "once_cell", 23 | "regex", 24 | "terminal_size", 25 | "unicode-width", 26 | "winapi", 27 | ] 28 | 29 | [[package]] 30 | name = "encode_unicode" 31 | version = "0.3.6" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" 34 | 35 | [[package]] 36 | name = "foto" 37 | version = "0.1.0" 38 | dependencies = [ 39 | "console", 40 | "regex", 41 | "serde_json", 42 | ] 43 | 44 | [[package]] 45 | name = "itoa" 46 | version = "1.0.1" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" 49 | 50 | [[package]] 51 | name = "libc" 52 | version = "0.2.112" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" 55 | 56 | [[package]] 57 | name = "memchr" 58 | version = "2.4.1" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 61 | 62 | [[package]] 63 | name = "once_cell" 64 | version = "1.9.0" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" 67 | 68 | [[package]] 69 | name = "regex" 70 | version = "1.5.4" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 73 | dependencies = [ 74 | "aho-corasick", 75 | "memchr", 76 | "regex-syntax", 77 | ] 78 | 79 | [[package]] 80 | name = "regex-syntax" 81 | version = "0.6.25" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 84 | 85 | [[package]] 86 | name = "ryu" 87 | version = "1.0.9" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" 90 | 91 | [[package]] 92 | name = "serde" 93 | version = "1.0.133" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" 96 | 97 | [[package]] 98 | name = "serde_json" 99 | version = "1.0.74" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | checksum = "ee2bb9cd061c5865d345bb02ca49fcef1391741b672b54a0bf7b679badec3142" 102 | dependencies = [ 103 | "itoa", 104 | "ryu", 105 | "serde", 106 | ] 107 | 108 | [[package]] 109 | name = "terminal_size" 110 | version = "0.1.17" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" 113 | dependencies = [ 114 | "libc", 115 | "winapi", 116 | ] 117 | 118 | [[package]] 119 | name = "unicode-width" 120 | version = "0.1.9" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" 123 | 124 | [[package]] 125 | name = "winapi" 126 | version = "0.3.9" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 129 | dependencies = [ 130 | "winapi-i686-pc-windows-gnu", 131 | "winapi-x86_64-pc-windows-gnu", 132 | ] 133 | 134 | [[package]] 135 | name = "winapi-i686-pc-windows-gnu" 136 | version = "0.4.0" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 139 | 140 | [[package]] 141 | name = "winapi-x86_64-pc-windows-gnu" 142 | version = "0.4.0" 143 | source = "registry+https://github.com/rust-lang/crates.io-index" 144 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 145 | --------------------------------------------------------------------------------