Browse Source

Clean up warnings

master
Dylan Baker 1 year ago
parent
commit
af9a781fde
5 changed files with 76 additions and 93 deletions
  1. 19
    19
      src/entry.rs
  2. 38
    56
      src/lib.rs
  3. 1
    1
      src/main.rs
  4. 6
    5
      src/rss.rs
  5. 12
    12
      tests/integration_test.rs

+ 19
- 19
src/entry.rs View File

@@ -7,7 +7,7 @@ use std::path;
7 7
 
8 8
 use config::Config;
9 9
 
10
-#[derive(Debug, PartialEq)]
10
+#[derive(Debug, PartialEq, Eq)]
11 11
 pub enum EntryKind {
12 12
     Page,
13 13
     Post,
@@ -46,7 +46,7 @@ impl Entry {
46 46
 pub fn read_entry_dir(cwd: &path::Path) -> Vec<fs::DirEntry> {
47 47
     match fs::read_dir(cwd) {
48 48
         Ok(entries) => entries.into_iter().map(|entry| entry.unwrap()).collect(),
49
-        Err(err) => panic!(err),
49
+        Err(err) => std::panic::panic_any(err),
50 50
     }
51 51
 }
52 52
 
@@ -68,7 +68,7 @@ pub fn parse_entry(kind: EntryKind, contents: &str, path: &path::Path) -> Entry
68 68
 
69 69
     if kind == EntryKind::Post {
70 70
         let captures = &RE_WITH_DATE
71
-            .captures(&contents)
71
+            .captures(contents)
72 72
             .expect("Couldn't parse post");
73 73
         let title = captures["title"].to_string();
74 74
         let body = captures["body"].to_string();
@@ -84,7 +84,7 @@ pub fn parse_entry(kind: EntryKind, contents: &str, path: &path::Path) -> Entry
84 84
         }
85 85
     } else {
86 86
         let captures = &RE_WITHOUT_DATE
87
-            .captures(&contents)
87
+            .captures(contents)
88 88
             .expect("Couldn't parse page");
89 89
         let title = captures["title"].to_string();
90 90
         let body = captures["body"].to_string();
@@ -114,15 +114,15 @@ pub fn write_entry(
114 114
         Ok(_) => {}
115 115
         Err(err) => match err.kind() {
116 116
             std::io::ErrorKind::AlreadyExists => {}
117
-            _ => panic!(err),
117
+            _ => std::panic::panic_any(err),
118 118
         },
119 119
     }
120 120
 
121
-    let template = layout.replace("{{ contents }}", &post_template);
121
+    let template = layout.replace("{{ contents }}", post_template);
122 122
 
123 123
     fs::write(
124 124
         &root_path.join("index.html"),
125
-        entry.render(&template, &config),
125
+        entry.render(&template, config),
126 126
     )
127 127
     .expect("Unable to write file");
128 128
 }
@@ -132,7 +132,7 @@ pub fn write_entry_listing(
132 132
     layout: &str,
133 133
     post_listing_template: &str,
134 134
     post_item_template: &str,
135
-    posts: &Vec<Entry>,
135
+    posts: &[Entry],
136 136
     config: &Config,
137 137
 ) {
138 138
     fs::write(
@@ -152,18 +152,18 @@ pub fn render_post_listing(
152 152
     layout: &str,
153 153
     post_listing_template: &str,
154 154
     post_item_template: &str,
155
-    posts: &Vec<Entry>,
155
+    posts: &[Entry],
156 156
     config: &Config,
157 157
 ) -> String {
158 158
     layout
159
-        .replace("{{ page_title }}", &format!("{}", config.site_name))
159
+        .replace("{{ page_title }}", &config.site_name.to_string())
160 160
         .replace(
161 161
             "{{ contents }}",
162 162
             &post_listing_template.replace(
163 163
                 "{{ post_listing }}",
164 164
                 &posts
165 165
                     .iter()
166
-                    .map(|post| post.render(&post_item_template, &config))
166
+                    .map(|post| post.render(post_item_template, config))
167 167
                     .collect::<Vec<String>>()
168 168
                     .join("\n"),
169 169
             ),
@@ -198,7 +198,7 @@ mod tests {
198 198
         let output = post.render(
199 199
             "<html><head><title>{{ page_title }}</title></head><body><article><h1>{{ title }}</h1><div>{{ body }}</div></article></body></html>", &config
200 200
         )
201
-        .replace("\n", "");
201
+        .replace('\n', "");
202 202
 
203 203
         assert_eq!(
204 204
             "<html><head><title>hello world | Test Site</title></head><body><article><h1>hello world</h1><div><p>lorem ipsum dolor sit amet</p></div></article></body></html>",
@@ -245,7 +245,7 @@ mod tests {
245 245
             &posts,
246 246
             &config,
247 247
         )
248
-        .replace("\n", "");
248
+        .replace('\n', "");
249 249
 
250 250
         assert_eq!(
251 251
             "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
@@ -278,7 +278,7 @@ mod tests {
278 278
             description: String::from("recent posts from testsite.com"),
279 279
         };
280 280
 
281
-        write_entry(&project_dir, &layout, &post_template, &post, &config);
281
+        write_entry(&project_dir, layout, post_template, &post, &config);
282 282
 
283 283
         let content = fs::read_to_string(
284 284
             project_dir
@@ -291,7 +291,7 @@ mod tests {
291 291
 
292 292
         assert_eq!(
293 293
             "<html><head><title>Hello world | Test Site</title></head><body><article><h1>Hello world</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body></html>",
294
-	    content.replace("\n", "")
294
+	    content.replace('\n', "")
295 295
 	);
296 296
 
297 297
         fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();
@@ -341,9 +341,9 @@ mod tests {
341 341
         };
342 342
         write_entry_listing(
343 343
             &cwd,
344
-            &layout,
345
-            &post_listing_template,
346
-            &post_item_template,
344
+            layout,
345
+            post_listing_template,
346
+            post_item_template,
347 347
             &posts,
348 348
             &config,
349 349
         );
@@ -355,7 +355,7 @@ mod tests {
355 355
              ></ul></body></html>",
356 356
             fs::read_to_string(&cwd.join("public").join("index.html"))
357 357
                 .unwrap()
358
-                .replace("\n", ""),
358
+                .replace('\n', ""),
359 359
         );
360 360
 
361 361
         fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();

+ 38
- 56
src/lib.rs View File

@@ -40,11 +40,8 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
40 40
         }
41 41
     };
42 42
 
43
-    match fs::read_dir(cwd.join("public")) {
44
-        Ok(_) => {
45
-            fs::remove_dir_all(cwd.join("public")).unwrap();
46
-        }
47
-        Err(_) => {}
43
+    if fs::read_dir(cwd.join("public")).is_ok() {
44
+        fs::remove_dir_all(cwd.join("public")).unwrap();
48 45
     }
49 46
 
50 47
     fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
@@ -85,18 +82,18 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
85 82
     posts.sort_by(|a, b| b.date.cmp(&a.date));
86 83
 
87 84
     for post in &posts {
88
-        write_entry(&cwd, &layout_template, &post_template, &post, &config);
85
+        write_entry(cwd, &layout_template, &post_template, post, &config);
89 86
     }
90 87
 
91 88
     for entry in page_paths.into_iter() {
92 89
         let path = entry.path();
93 90
         let contents = fs::read_to_string(&path).expect("Couldn't read page file");
94 91
         let page = parse_entry(EntryKind::Page, &contents, &path);
95
-        write_entry(&cwd, &layout_template, &page_template, &page, &config);
92
+        write_entry(cwd, &layout_template, &page_template, &page, &config);
96 93
     }
97 94
 
98 95
     write_entry_listing(
99
-        &cwd,
96
+        cwd,
100 97
         &layout_template,
101 98
         &post_listing_template,
102 99
         &post_item_template,
@@ -111,14 +108,15 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
111 108
     )
112 109
     .expect("Couldn't copy assets directory");
113 110
 
114
-    let rss = generate_rss(&config, &posts);
111
+    let rss = generate_rss(&config, posts);
115 112
     fs::write(cwd.join("public").join("rss.xml"), rss).expect("Couldn't write rss file");
116 113
 }
117 114
 
118 115
 pub fn new(name: &str, cwd: &path::Path) {
119 116
     let project_path = cwd.join(name);
120 117
 
121
-    fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
118
+    fs::create_dir(&project_path)
119
+        .unwrap_or_else(|_| panic!("Couldn't create directory '{}'", &name));
122 120
     fs::write(
123 121
         project_path.join("casaubon.toml"),
124 122
         format!(
@@ -138,7 +136,7 @@ pub fn new(name: &str, cwd: &path::Path) {
138 136
         "js",
139 137
     ] {
140 138
         fs::create_dir(&project_path.join(&dir))
141
-            .expect(&format!("Couldn't create {} directory", &dir));
139
+            .unwrap_or_else(|_| panic!("Couldn't create {} directory", &dir));
142 140
     }
143 141
 
144 142
     let default_layout_template = format!(
@@ -153,29 +151,25 @@ pub fn new(name: &str, cwd: &path::Path) {
153 151
 </html>\n",
154 152
         name, name
155 153
     );
156
-    let default_post_listing_template = format!(
157
-        "<div>
154
+    let default_post_listing_template = "<div>
158 155
   <h3>Posts</h3>
159
-  <ul>{{{{ post_listing }}}}</ul>
156
+  <ul>{{ post_listing }}</ul>
160 157
 </div>\n"
161
-    );
162
-    let default_post_template = format!(
163
-        "<article>
164
-  <h1>{{{{ title }}}}</h1>
165
-  <div>{{{{ body }}}}</div>
158
+        .to_string();
159
+    let default_post_template = "<article>
160
+  <h1>{{ title }}</h1>
161
+  <div>{{ body }}</div>
166 162
 </article>\n"
167
-    );
168
-    let default_page_template = format!(
169
-        "<article>
170
-  <h1>{{{{ title }}}}</h1>
171
-  <div>{{{{ body }}}}</div>
163
+        .to_string();
164
+    let default_page_template = "<article>
165
+  <h1>{{ title }}</h1>
166
+  <div>{{ body }}</div>
172 167
 </article>\n"
173
-    );
174
-    let default_post_listing_item_template = format!(
175
-        "<li>
176
-  {{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a>
168
+        .to_string();
169
+    let default_post_listing_item_template = "<li>
170
+  { date } <a href=\"/posts/{{ slug }}/\">{{ title }}</a>
177 171
 </li>\n"
178
-    );
172
+        .to_string();
179 173
 
180 174
     for (filename, contents) in &[
181 175
         ("layout", &default_layout_template),
@@ -190,11 +184,11 @@ pub fn new(name: &str, cwd: &path::Path) {
190 184
                 .join(format!("{}.html", filename)),
191 185
             &contents,
192 186
         )
193
-        .expect(&format!("Couldn't write templates/{}.html", filename));
187
+        .unwrap_or_else(|_| panic!("Couldn't write templates/{}.html", filename));
194 188
     }
195 189
 }
196 190
 
197
-fn should_rebuild(cwd: &path::Path, path: &path::PathBuf) -> bool {
191
+fn should_rebuild(cwd: &path::Path, path: &path::Path) -> bool {
198 192
     let path_string = path.to_str().unwrap().to_string();
199 193
     let change_is_from_public = path_string.contains(cwd.join("public").to_str().unwrap());
200 194
     let change_is_from_git = path_string.contains(cwd.join(".git").to_str().unwrap());
@@ -204,14 +198,14 @@ fn should_rebuild(cwd: &path::Path, path: &path::PathBuf) -> bool {
204 198
 
205 199
 pub fn watch(include_drafts: bool, cwd: &path::Path) -> notify::Result<()> {
206 200
     let (tx, rx) = channel();
207
-    let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
208
-    try!(watcher.watch(&cwd, RecursiveMode::Recursive));
201
+    let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
202
+    watcher.watch(&cwd, RecursiveMode::Recursive)?;
209 203
     println!("Watching {}", cwd.to_str().unwrap());
210 204
 
211 205
     let handle_event = |path: &path::PathBuf| {
212
-        if should_rebuild(&cwd, &path) {
206
+        if should_rebuild(cwd, path) {
213 207
             println!("Rebuilding");
214
-            build(include_drafts, &cwd);
208
+            build(include_drafts, cwd);
215 209
         }
216 210
     };
217 211
 
@@ -243,26 +237,14 @@ mod tests {
243 237
     #[test]
244 238
     fn test_should_rebuild() {
245 239
         let cwd = env::current_dir().unwrap();
246
-        assert_eq!(
247
-            false,
248
-            should_rebuild(&cwd, &cwd.join("public").join("index.html"))
249
-        );
250
-        assert_eq!(
251
-            false,
252
-            should_rebuild(&cwd, &cwd.join(".git").join("index.html"))
253
-        );
254
-        assert_eq!(
255
-            true,
256
-            should_rebuild(&cwd, &cwd.join("posts").join("test.md"))
257
-        );
258
-        assert_eq!(
259
-            true,
260
-            should_rebuild(&cwd, &cwd.join("drafts").join("test.md"))
261
-        );
262
-        assert_eq!(
263
-            true,
264
-            should_rebuild(&cwd, &cwd.join("css").join("style.css"))
265
-        );
266
-        assert_eq!(true, should_rebuild(&cwd, &cwd.join("js").join("index.js")));
240
+        assert!(!should_rebuild(
241
+            &cwd,
242
+            &cwd.join("public").join("index.html")
243
+        ));
244
+        assert!(!should_rebuild(&cwd, &cwd.join(".git").join("index.html")));
245
+        assert!(should_rebuild(&cwd, &cwd.join("posts").join("test.md")));
246
+        assert!(should_rebuild(&cwd, &cwd.join("drafts").join("test.md")));
247
+        assert!(should_rebuild(&cwd, &cwd.join("css").join("style.css")));
248
+        assert!(should_rebuild(&cwd, &cwd.join("js").join("index.js")));
267 249
     }
268 250
 }

+ 1
- 1
src/main.rs View File

@@ -37,7 +37,7 @@ fn main() {
37 37
         );
38 38
     } else if command == "new" {
39 39
         casaubon::new(
40
-            &matches.value_of("name").unwrap(),
40
+            matches.value_of("name").unwrap(),
41 41
             &env::current_dir().expect("Can't read current directory"),
42 42
         );
43 43
     } else if command == "watch" {

+ 6
- 5
src/rss.rs View File

@@ -5,7 +5,8 @@ use config::Config;
5 5
 use entry::{Entry, EntryKind};
6 6
 
7 7
 pub fn generate_post_rss(config: &Config, post: &Entry) -> String {
8
-    let date: DateTime<Utc> = DateTime::from_utc(post.date.unwrap().and_hms(0, 0, 0), Utc);
8
+    let raw_date = post.date.unwrap().and_hms_opt(0, 0, 0).unwrap();
9
+    let date: DateTime<Utc> = DateTime::from_utc(raw_date, Utc);
9 10
     let date_string = date.format("%a, %d %b %Y %H:%M:%S %z").to_string();
10 11
     let url = format!("{}/posts/{}/", config.url, post.slug);
11 12
     format!(
@@ -14,11 +15,11 @@ pub fn generate_post_rss(config: &Config, post: &Entry) -> String {
14 15
     )
15 16
 }
16 17
 
17
-pub fn generate_rss(config: &Config, posts: &Vec<Entry>) -> String {
18
+pub fn generate_rss(config: &Config, posts: Vec<Entry>) -> String {
18 19
     let items = posts
19
-        .into_iter()
20
+        .iter()
20 21
         .filter(|post| post.kind == EntryKind::Post)
21
-        .map(|post| generate_post_rss(&config, &post))
22
+        .map(|post| generate_post_rss(config, post))
22 23
         .collect::<Vec<String>>()
23 24
         .join("");
24 25
 
@@ -51,7 +52,7 @@ mod tests {
51 52
             kind: EntryKind::Post,
52 53
         }];
53 54
         assert_eq!(
54
-            generate_rss(&config, &posts),
55
+            generate_rss(&config, posts),
55 56
             "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\"><channel><atom:link href=\"https://www.loremipsum.com/rss.xml\" rel=\"self\" /><title>Lorem Ipsum</title><description>recent posts from loremipsum.com</description><link>https://www.loremipsum.com</link><item><title>Hello World</title><description>&lt;article&gt;&lt;p&gt;lorem ipsum dolor sit amet&lt;/p&gt;\n&lt;/article&gt;</description><guid>https://www.loremipsum.com/posts/hello-world/</guid><link>https://www.loremipsum.com/posts/hello-world/</link><pubDate>Tue, 01 Jan 2019 00:00:00 +0000</pubDate></item></channel></rss>"
56 57
         );
57 58
     }

+ 12
- 12
tests/integration_test.rs View File

@@ -87,7 +87,7 @@ fn test_build() {
87 87
                 .join("index.html")
88 88
         )
89 89
         .unwrap()
90
-        .replace("\n", ""),
90
+        .replace('\n', ""),
91 91
     );
92 92
 
93 93
     assert_eq!(
@@ -101,7 +101,7 @@ fn test_build() {
101 101
                 .join("index.html")
102 102
         )
103 103
         .unwrap()
104
-        .replace("\n", ""),
104
+        .replace('\n', ""),
105 105
     );
106 106
 
107 107
     assert_eq!(
@@ -182,7 +182,7 @@ fn test_build_drafts() {
182 182
 
183 183
     assert_eq!(
184 184
         "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/first-draft\">First draft</a></li></ul></body></html>",
185
-        fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace("\n", ""),
185
+        fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace('\n', ""),
186 186
     );
187 187
 
188 188
     assert_eq!(
@@ -194,7 +194,7 @@ fn test_build_drafts() {
194 194
                 .join("first-post")
195 195
                 .join("index.html")
196 196
         ).unwrap()
197
-        .replace("\n", ""),
197
+        .replace('\n', ""),
198 198
     );
199 199
 
200 200
     assert_eq!(
@@ -205,7 +205,7 @@ fn test_build_drafts() {
205 205
                 .join("first-page")
206 206
                 .join("index.html")
207 207
         ).unwrap()
208
-        .replace("\n", ""),
208
+        .replace('\n', ""),
209 209
     );
210 210
 
211 211
     assert_eq!(
@@ -217,7 +217,7 @@ fn test_build_drafts() {
217 217
                 .join("first-draft")
218 218
                 .join("index.html")
219 219
         ).unwrap()
220
-        .replace("\n", ""),
220
+        .replace('\n', ""),
221 221
     );
222 222
 }
223 223
 
@@ -227,7 +227,7 @@ fn test_new() {
227 227
     let temp_dir = tempfile::tempdir().unwrap();
228 228
     let project_dir = temp_dir.path().join(&uuid);
229 229
 
230
-    casaubon::new(&uuid, &temp_dir.path());
230
+    casaubon::new(&uuid, temp_dir.path());
231 231
 
232 232
     for dir in &["public", "pages", "posts", "templates"] {
233 233
         fs::read_dir(&project_dir.join(dir)).unwrap();
@@ -240,35 +240,35 @@ fn test_new() {
240 240
         ),
241 241
         fs::read_to_string(&project_dir.join("templates").join("layout.html"))
242 242
             .unwrap()
243
-            .replace("\n", "")
243
+            .replace('\n', "")
244 244
             .replace("  ", "")
245 245
     );
246 246
     assert_eq!(
247 247
         format!("<div><h3>Posts</h3><ul>{{{{ post_listing }}}}</ul></div>"),
248 248
         fs::read_to_string(&project_dir.join("templates").join("post_listing.html"))
249 249
             .unwrap()
250
-            .replace("\n", "")
250
+            .replace('\n', "")
251 251
             .replace("  ", "")
252 252
     );
253 253
     assert_eq!(
254 254
         format!("<li>{{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a></li>"),
255 255
         fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
256 256
             .unwrap()
257
-            .replace("\n", "")
257
+            .replace('\n', "")
258 258
             .replace("  ", "")
259 259
     );
260 260
     assert_eq!(
261 261
         format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
262 262
         fs::read_to_string(&project_dir.join("templates").join("post.html"))
263 263
             .unwrap()
264
-            .replace("\n", "")
264
+            .replace('\n', "")
265 265
             .replace("  ", "")
266 266
     );
267 267
     assert_eq!(
268 268
         format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
269 269
         fs::read_to_string(&project_dir.join("templates").join("page.html"))
270 270
             .unwrap()
271
-            .replace("\n", "")
271
+            .replace('\n', "")
272 272
             .replace("  ", "")
273 273
     );
274 274
     assert_eq!(

Loading…
Cancel
Save