├── .gitignore ├── Cargo.toml ├── README.md ├── LICENSE ├── src └── main.rs └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | kv.db 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kv" 3 | version = "0.1.0" 4 | authors = ["Brian Ketelsen "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | structopt = "0.3" 11 | serde = "1.0.102" 12 | serde_json = "1.0" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Welcome to kv 👋

2 |

3 | 4 | License: MIT 5 | 6 | 7 | Twitter: bketelsen 8 | 9 |

10 | 11 | > A command line key-value store built as a learning exercise for Rust. 12 | 13 | ## Author 14 | 15 | 👤 **Brian Ketelsen** 16 | 17 | * Website: http://www.brianketelsen.com 18 | * Twitter: [@bketelsen](https://twitter.com/bketelsen) 19 | * Github: [@bketelsen](https://github.com/bketelsen) 20 | * Youtube: [@bketelsen](https://www.youtube.com/user/bketelsen) 21 | 22 | ## Show your support 23 | 24 | Give a ⭐️ if this project helped you! 25 | 26 | *** 27 | _This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Brian Ketelsen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // Add these into our namespace 2 | use serde_json::Value; 3 | use std::collections::HashMap; 4 | use std::str::FromStr; 5 | use structopt::StructOpt; 6 | 7 | /// A basic example 8 | #[derive(StructOpt, Debug)] 9 | #[structopt(name = "basic")] 10 | struct Opt { 11 | #[structopt()] 12 | subcommand: Subcommand, 13 | #[structopt(short, long)] 14 | key: String, 15 | #[structopt(short, long)] 16 | value: String, 17 | } 18 | 19 | #[derive(Debug)] 20 | enum Subcommand { 21 | Set, 22 | Get, 23 | } 24 | 25 | impl FromStr for Subcommand { 26 | type Err = String; 27 | fn from_str(s: &str) -> Result { 28 | match s { 29 | "set" => Ok(Subcommand::Set), 30 | "get" => Ok(Subcommand::Get), 31 | _ => Err("Subcommand must be either 'get' or 'set'".to_string()), 32 | } 33 | } 34 | } 35 | // kv [subcommand=set|get] [key] {value} 36 | // https://github.com/bketelsen/kv # repo for this 37 | fn main() { 38 | let opt = Opt::from_args(); 39 | println!("{:#?}", opt); 40 | 41 | match opt.subcommand { 42 | Subcommand::Get => unimplemented!(), 43 | Subcommand::Set => set(opt.key, opt.value).unwrap(), 44 | }; 45 | } 46 | 47 | fn set(key: String, value: String) -> std::io::Result<()> { 48 | let mut map = load_keys()?; 49 | map.insert(key, value); 50 | write_keys(map)?; 51 | Ok(()) 52 | } 53 | 54 | fn write_keys(map: HashMap) -> std::io::Result<()> { 55 | let jstr = serde_json::to_string(&map)?; 56 | std::fs::write("kv.db", jstr.as_bytes())?; 57 | 58 | Ok(()) 59 | } 60 | 61 | fn load_keys() -> std::io::Result> { 62 | let contents = match std::fs::read_to_string("kv.db") { 63 | Ok(s) => s, 64 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()), 65 | Err(e) => return Err(e), 66 | }; 67 | 68 | let json: Value = serde_json::from_str(&contents)?; 69 | match json { 70 | Value::Object(map) => { 71 | let mut db = HashMap::new(); 72 | for (k, value) in map { 73 | match value { 74 | Value::String(string) => db.insert(k, string), 75 | _ => panic!("Bad Map: CORRUPT DATABASE!!!"), 76 | }; 77 | } 78 | Ok(db) 79 | } 80 | _ => panic!("Not a Map: CORRUPT DATABASE!!!"), 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "ansi_term" 5 | version = "0.11.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | dependencies = [ 8 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 9 | ] 10 | 11 | [[package]] 12 | name = "atty" 13 | version = "0.2.13" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | dependencies = [ 16 | "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", 17 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 18 | ] 19 | 20 | [[package]] 21 | name = "bitflags" 22 | version = "1.2.1" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | 25 | [[package]] 26 | name = "clap" 27 | version = "2.33.0" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | dependencies = [ 30 | "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 31 | "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", 32 | "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 33 | "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 34 | "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 35 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 36 | "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", 37 | ] 38 | 39 | [[package]] 40 | name = "heck" 41 | version = "0.3.1" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | dependencies = [ 44 | "unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 45 | ] 46 | 47 | [[package]] 48 | name = "itoa" 49 | version = "0.4.4" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | 52 | [[package]] 53 | name = "kv" 54 | version = "0.1.0" 55 | dependencies = [ 56 | "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)", 57 | "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", 58 | "structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 59 | ] 60 | 61 | [[package]] 62 | name = "libc" 63 | version = "0.2.65" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | 66 | [[package]] 67 | name = "proc-macro-error" 68 | version = "0.2.6" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | dependencies = [ 71 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 72 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 73 | "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 74 | ] 75 | 76 | [[package]] 77 | name = "proc-macro2" 78 | version = "1.0.6" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | dependencies = [ 81 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 82 | ] 83 | 84 | [[package]] 85 | name = "quote" 86 | version = "1.0.2" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | dependencies = [ 89 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 90 | ] 91 | 92 | [[package]] 93 | name = "ryu" 94 | version = "1.0.2" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | 97 | [[package]] 98 | name = "serde" 99 | version = "1.0.102" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | 102 | [[package]] 103 | name = "serde_json" 104 | version = "1.0.41" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | dependencies = [ 107 | "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 109 | "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)", 110 | ] 111 | 112 | [[package]] 113 | name = "strsim" 114 | version = "0.8.0" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | 117 | [[package]] 118 | name = "structopt" 119 | version = "0.3.4" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | dependencies = [ 122 | "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", 123 | "structopt-derive 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 124 | ] 125 | 126 | [[package]] 127 | name = "structopt-derive" 128 | version = "0.3.4" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | dependencies = [ 131 | "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "proc-macro-error 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", 133 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 134 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 135 | "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 136 | ] 137 | 138 | [[package]] 139 | name = "syn" 140 | version = "1.0.8" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | dependencies = [ 143 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 144 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 145 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 146 | ] 147 | 148 | [[package]] 149 | name = "textwrap" 150 | version = "0.11.0" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | dependencies = [ 153 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 154 | ] 155 | 156 | [[package]] 157 | name = "unicode-segmentation" 158 | version = "1.6.0" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | 161 | [[package]] 162 | name = "unicode-width" 163 | version = "0.1.6" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | 166 | [[package]] 167 | name = "unicode-xid" 168 | version = "0.2.0" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | 171 | [[package]] 172 | name = "vec_map" 173 | version = "0.8.1" 174 | source = "registry+https://github.com/rust-lang/crates.io-index" 175 | 176 | [[package]] 177 | name = "winapi" 178 | version = "0.3.8" 179 | source = "registry+https://github.com/rust-lang/crates.io-index" 180 | dependencies = [ 181 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 182 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 183 | ] 184 | 185 | [[package]] 186 | name = "winapi-i686-pc-windows-gnu" 187 | version = "0.4.0" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | 190 | [[package]] 191 | name = "winapi-x86_64-pc-windows-gnu" 192 | version = "0.4.0" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | 195 | [metadata] 196 | "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 197 | "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" 198 | "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 199 | "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" 200 | "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" 201 | "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" 202 | "checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" 203 | "checksum proc-macro-error 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "aeccfe4d5d8ea175d5f0e4a2ad0637e0f4121d63bd99d356fb1f39ab2e7c6097" 204 | "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" 205 | "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" 206 | "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" 207 | "checksum serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4b39bd9b0b087684013a792c59e3e07a46a01d2322518d8a1104641a0b1be0" 208 | "checksum serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "2f72eb2a68a7dc3f9a691bfda9305a1c017a6215e5a4545c258500d2099a37c2" 209 | "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 210 | "checksum structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c167b61c7d4c126927f5346a4327ce20abf8a186b8041bbeb1ce49e5db49587b" 211 | "checksum structopt-derive 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "519621841414165d2ad0d4c92be8f41844203f2b67e245f9345a5a12d40c69d7" 212 | "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92" 213 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 214 | "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" 215 | "checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" 216 | "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" 217 | "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" 218 | "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" 219 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 220 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 221 | --------------------------------------------------------------------------------