Browse Source

Create config system

master
Dylan Baker 5 years ago
parent
commit
12e4d78c47
4 changed files with 44 additions and 0 deletions
  1. 16
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 3
    0
      src/config.rs
  4. 24
    0
      src/main.rs

+ 16
- 0
Cargo.lock View File

194
  "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
194
  "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
195
 ]
195
 ]
196
 
196
 
197
+[[package]]
198
+name = "serde"
199
+version = "1.0.80"
200
+source = "registry+https://github.com/rust-lang/crates.io-index"
201
+
197
 [[package]]
202
 [[package]]
198
 name = "strsim"
203
 name = "strsim"
199
 version = "0.7.0"
204
 version = "0.7.0"
252
  "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
257
  "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
253
  "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
258
  "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
254
  "regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
259
  "regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
260
+ "toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
255
  "uuid 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
261
  "uuid 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
256
 ]
262
 ]
257
 
263
 
264
+[[package]]
265
+name = "toml"
266
+version = "0.4.8"
267
+source = "registry+https://github.com/rust-lang/crates.io-index"
268
+dependencies = [
269
+ "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
270
+]
271
+
258
 [[package]]
272
 [[package]]
259
 name = "twoway"
273
 name = "twoway"
260
 version = "0.1.8"
274
 version = "0.1.8"
356
 "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
370
 "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
357
 "checksum regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467"
371
 "checksum regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467"
358
 "checksum regex-syntax 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc557aac2b708fe84121caf261346cc2eed71978024337e42eb46b8a252ac6e"
372
 "checksum regex-syntax 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc557aac2b708fe84121caf261346cc2eed71978024337e42eb46b8a252ac6e"
373
+"checksum serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "15c141fc7027dd265a47c090bf864cf62b42c4d228bbcf4e51a0c9e2b0d3f7ef"
359
 "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
374
 "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
360
 "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
375
 "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
361
 "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
376
 "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
362
 "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
377
 "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
363
 "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
378
 "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
364
 "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
379
 "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
380
+"checksum toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4a2ecc31b0351ea18b3fe11274b8db6e4d82bce861bbb22e6dbed40417902c65"
365
 "checksum twoway 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1"
381
 "checksum twoway 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1"
366
 "checksum typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c6c06a92aef38bb4dc5b0df00d68496fc31307c5344c867bb61678c6e1671ec5"
382
 "checksum typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c6c06a92aef38bb4dc5b0df00d68496fc31307c5344c867bb61678c6e1671ec5"
367
 "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
383
 "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"

+ 1
- 0
Cargo.toml View File

9
 fs_extra = "1.1.0"
9
 fs_extra = "1.1.0"
10
 lazy_static = "1.2.0"
10
 lazy_static = "1.2.0"
11
 regex = "1"
11
 regex = "1"
12
+toml = "0.4.8"
12
 uuid = { version = "0.7", features = ["v4"] }
13
 uuid = { version = "0.7", features = ["v4"] }

+ 3
- 0
src/config.rs View File

1
+pub struct Config {
2
+    pub site_name: String,
3
+}

+ 24
- 0
src/main.rs View File

4
 #[macro_use]
4
 #[macro_use]
5
 extern crate lazy_static;
5
 extern crate lazy_static;
6
 extern crate regex;
6
 extern crate regex;
7
+extern crate toml;
7
 extern crate uuid;
8
 extern crate uuid;
8
 
9
 
9
 use std::{env, fs};
10
 use std::{env, fs};
10
 
11
 
11
 use clap::{App, Arg};
12
 use clap::{App, Arg};
12
 use fs_extra::dir;
13
 use fs_extra::dir;
14
+use toml::Value;
13
 
15
 
16
+use config::Config;
14
 use post::{parse_post, read_posts_dir};
17
 use post::{parse_post, read_posts_dir};
15
 use write::{write_post, write_post_listing};
18
 use write::{write_post, write_post_listing};
16
 
19
 
20
+mod config;
17
 mod post;
21
 mod post;
18
 mod render;
22
 mod render;
19
 mod write;
23
 mod write;
20
 
24
 
21
 fn build() {
25
 fn build() {
22
     let cwd = env::current_dir().expect("Couldn't read current directory");
26
     let cwd = env::current_dir().expect("Couldn't read current directory");
27
+    let config = match fs::read_to_string(cwd.join("tlon.toml")) {
28
+        Ok(contents) => match contents.parse::<Value>() {
29
+            Ok(config) => Config {
30
+                site_name: String::from(config["site_name"].as_str().unwrap()),
31
+            },
32
+            Err(_) => panic!("Invalid tlon.toml"),
33
+        },
34
+        Err(_) => {
35
+            panic!("Can't find tlon.toml");
36
+        }
37
+    };
23
 
38
 
24
     match fs::read_dir(cwd.join("public")) {
39
     match fs::read_dir(cwd.join("public")) {
25
         Ok(_) => {
40
         Ok(_) => {
72
     let project_path = cwd.join(name);
87
     let project_path = cwd.join(name);
73
 
88
 
74
     fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
89
     fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
90
+    fs::write(
91
+        project_path.join("tlon.toml"),
92
+        format!("site_name = \"{}\"", &name),
93
+    ).expect("Could not create tlon.toml");
75
 
94
 
76
     for dir in &["posts", "public", "templates", "css", "js"] {
95
     for dir in &["posts", "public", "templates", "css", "js"] {
77
         fs::create_dir(&project_path.join(&dir))
96
         fs::create_dir(&project_path.join(&dir))
161
             "",
180
             "",
162
             fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
181
             fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
163
         );
182
         );
183
+        assert_eq!(
184
+            format!("site_name = \"{}\"", &uuid),
185
+            fs::read_to_string(&project_dir.join("tlon.toml")).unwrap()
186
+        );
164
 
187
 
165
         fs::remove_dir_all(project_dir).unwrap();
188
         fs::remove_dir_all(project_dir).unwrap();
166
     }
189
     }
207
             project_dir.join("posts").join("first-post.md"),
230
             project_dir.join("posts").join("first-post.md"),
208
             "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
231
             "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
209
         ).unwrap();
232
         ).unwrap();
233
+        fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
210
 
234
 
211
         build();
235
         build();
212
 
236
 

Loading…
Cancel
Save