瀏覽代碼

Use Display trait for printing errors

...instead of using the `description` method.
Johannes Hofmann 7 年之前
父節點
當前提交
a6c0ea8561
共有 4 個檔案被更改,包括 12 行新增16 行删除
  1. 4
    5
      src/config.rs
  2. 1
    2
      src/main.rs
  3. 4
    5
      src/program.rs
  4. 3
    4
      src/tile_loader.rs

+ 4
- 5
src/config.rs 查看文件

@@ -1,4 +1,3 @@
1
-use std::error::Error;
2 1
 use std::fs::File;
3 2
 use std::io::{Read, Write};
4 3
 use std::path::{Path, PathBuf};
@@ -45,7 +44,7 @@ impl Config {
45 44
     /// not exist.
46 45
     fn default_tile_cache_dir() -> Result<PathBuf, String> {
47 46
         let xdg_dirs = xdg::BaseDirectories::with_prefix("deltamap")
48
-            .map_err(|e| e.description().to_string())?;
47
+            .map_err(|e| format!("{}", e))?;
49 48
 
50 49
         match xdg_dirs.find_cache_file("tiles") {
51 50
             Some(dir) => Ok(dir),
@@ -154,15 +153,15 @@ impl Config {
154 153
                 )
155 154
             },
156 155
             Ok(_) => Err("TOML file has invalid structure. Expected a Table as the top-level element.".to_string()),
157
-            Err(e) => Err(e.description().to_string()),
156
+            Err(e) => Err(format!("{}", e)),
158 157
         }
159 158
     }
160 159
 
161 160
     pub fn from_toml_file<P: AsRef<Path>>(path: P) -> Result<Config, String> {
162
-        let mut file = File::open(path).map_err(|e| e.description().to_string())?;
161
+        let mut file = File::open(path).map_err(|e| format!("{}", e))?;
163 162
 
164 163
         let mut content = String::new();
165
-        file.read_to_string(&mut content).map_err(|e| e.description().to_string())?;
164
+        file.read_to_string(&mut content).map_err(|e| format!("{}", e))?;
166 165
 
167 166
         Config::from_toml_str(&content)
168 167
     }

+ 1
- 2
src/main.rs 查看文件

@@ -30,7 +30,6 @@ use clap::Arg;
30 30
 use coord::ScreenCoord;
31 31
 use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
32 32
 use map_view_gl::MapViewGl;
33
-use std::error::Error;
34 33
 use std::time::{Duration, Instant};
35 34
 use tile_source::TileSource;
36 35
 
@@ -195,7 +194,7 @@ fn main() {
195 194
             .validator(|s| {
196 195
                 s.parse::<f64>()
197 196
                     .map(|_| ())
198
-                    .map_err(|e| e.description().to_string())
197
+                    .map_err(|e| format!("{}", e))
199 198
             })
200 199
             .help("Set target frames per second (default is 60). \
201 200
                 This should equal the refresh rate of the display.")

+ 4
- 5
src/program.rs 查看文件

@@ -1,6 +1,5 @@
1 1
 use ::context;
2 2
 use context::Context;
3
-use std::error::Error;
4 3
 use std::ffi::CStr;
5 4
 use std::fs::File;
6 5
 use std::io::BufReader;
@@ -29,21 +28,21 @@ impl<'a> Program<'a> {
29 28
     pub fn from_paths<P: AsRef<Path>>(cx: &'a Context, vert_path: P, frag_path: P) -> Result<Program<'a>, String> {
30 29
         let vert_src = {
31 30
             let file = File::open(&vert_path)
32
-                .map_err(|e| e.description().to_string())?;
31
+                .map_err(|e| format!("{}", e))?;
33 32
             let mut reader = BufReader::new(file);
34 33
             let mut buf: Vec<u8> = vec![];
35 34
             reader.read_to_end(&mut buf)
36
-                .map_err(|e| e.description().to_string())?;
35
+                .map_err(|e| format!("{}", e))?;
37 36
             buf
38 37
         };
39 38
 
40 39
         let frag_src = {
41 40
             let file = File::open(&frag_path)
42
-                .map_err(|e| e.description().to_string())?;
41
+                .map_err(|e| format!("{}", e))?;
43 42
             let mut reader = BufReader::new(file);
44 43
             let mut buf: Vec<u8> = vec![];
45 44
             reader.read_to_end(&mut buf)
46
-                .map_err(|e| e.description().to_string())?;
45
+                .map_err(|e| format!("{}", e))?;
47 46
             buf
48 47
         };
49 48
 

+ 3
- 4
src/tile_loader.rs 查看文件

@@ -5,7 +5,6 @@ use reqwest::Client;
5 5
 use std::cmp::Ordering;
6 6
 use std::cmp;
7 7
 use std::collections::hash_set::HashSet;
8
-use std::error::Error;
9 8
 use std::fs::File;
10 9
 use std::io::Write;
11 10
 use std::path::{Path, PathBuf};
@@ -140,7 +139,7 @@ impl TileLoader {
140 139
                                         }
141 140
                                         if let Err(e) = remote_request_tx.send(RemoteLoaderMessage::PopQueue) {
142 141
                                             //TODO what now? restart worker?
143
-                                            error!("remote worker terminated, {}", e.description());
142
+                                            error!("remote worker terminated, {}", e);
144 143
                                         }
145 144
                                     }
146 145
                                 } else if result_tx.send((request.tile, None)).is_err() {
@@ -193,7 +192,7 @@ impl TileLoader {
193 192
 
194 193
                                     if request.write_to_file {
195 194
                                         if let Err(e) = Self::write_to_file(&request.path, &buf) {
196
-                                            warn!("could not write file {}, {}", request.path.display(), e.description());
195
+                                            warn!("could not write file {}, {}", request.path.display(), e);
197 196
                                         }
198 197
                                     }
199 198
 
@@ -282,7 +281,7 @@ impl TileLoader {
282 281
                                     if write_to_file {
283 282
                                         let path = source.local_tile_path(tile);
284 283
                                         if let Err(e) = Self::write_to_file(&path, &buf) {
285
-                                            warn!("could not write file {}, {}", &path.display(), e.description());
284
+                                            warn!("could not write file {}, {}", &path.display(), e);
286 285
                                         }
287 286
                                     }
288 287
                                     debug!("sync ok from network {:?}", tile);