Bläddra i källkod

config: Find a default tile cache directory

Johannes Hofmann 8 år sedan
förälder
incheckning
c1aab5f87a
2 ändrade filer med 42 tillägg och 13 borttagningar
  1. 2
    4
      default_config.toml
  2. 40
    9
      src/config.rs

+ 2
- 4
default_config.toml Visa fil

@@ -1,11 +1,9 @@
1
-tile_cache_dir = "/tmp"
2
-
3
-[sources.OSM]
1
+[tile_sources.OSM]
4 2
 max_zoom = 19
5 3
 url_template = "http://a.tile.openstreetmap.org/{z}/{x}/{y}.png"
6 4
 extension = "png"
7 5
 
8
-[sources.esri]
6
+[tile_sources.esri]
9 7
 max_zoom = 19
10 8
 url_template = "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
11 9
 extension = "jpg"

+ 40
- 9
src/config.rs Visa fil

@@ -9,8 +9,9 @@ use xdg;
9 9
 static DEFAULT_CONFIG: &'static str = include_str!("../default_config.toml");
10 10
 
11 11
 
12
+#[derive(Debug)]
12 13
 pub struct Config {
13
-    tile_cache_dir: String,
14
+    tile_cache_dir: PathBuf,
14 15
     sources: Vec<(String, TileSource)>,
15 16
 }
16 17
 
@@ -22,6 +23,7 @@ impl Config {
22 23
 
23 24
                 Config::from_toml_file(config_path)
24 25
             } else {
26
+                // try to write a default config file
25 27
                 if let Ok(path) = xdg_dirs.place_config_file("config.toml") {
26 28
                     if let Ok(mut file) = File::create(&path) {
27 29
                         println!("write default config {:?}", &path);
@@ -36,18 +38,37 @@ impl Config {
36 38
         }
37 39
     }
38 40
 
41
+    /// Returns a tile cache directory path at a standard XDG cache location. The returned path may
42
+    /// not exist.
43
+    fn default_tile_cache_dir() -> Result<PathBuf, String> {
44
+        let xdg_dirs = xdg::BaseDirectories::with_prefix("deltamap")
45
+            .map_err(|e| e.description().to_string())?;
46
+
47
+        match xdg_dirs.find_cache_file("tiles") {
48
+            Some(dir) => Ok(dir),
49
+            None => Ok(xdg_dirs.get_cache_home().join("tiles")),
50
+        }
51
+    }
52
+
39 53
     pub fn from_toml_str(toml_str: &str) -> Result<Config, String> {
40 54
         match toml_str.parse::<Value>() {
41 55
             Ok(Value::Table(ref table)) => {
42
-                let tile_cache_dir = table.get("tile_cache_dir")
43
-                    .ok_or_else(|| "missing \"tile_cache_dir\" entry".to_string())?
44
-                    .as_str()
45
-                    .ok_or_else(|| "tile_cache_dir has to be a string".to_string())?;
56
+                let tile_cache_dir = {
57
+                    match table.get("tile_cache_dir") {
58
+                        Some(dir) => {
59
+                            PathBuf::from(
60
+                                dir.as_str()
61
+                                   .ok_or_else(|| "tile_cache_dir has to be a string".to_string())?
62
+                            )
63
+                        },
64
+                        None => Config::default_tile_cache_dir()?,
65
+                    }
66
+                };
46 67
 
47
-                let sources_table = table.get("sources")
48
-                    .ok_or_else(|| "missing \"sources\" table".to_string())?
68
+                let sources_table = table.get("tile_sources")
69
+                    .ok_or_else(|| "missing \"tile_sources\" table".to_string())?
49 70
                     .as_table()
50
-                    .ok_or_else(|| "\"sources\" has to be a table".to_string())?;
71
+                    .ok_or_else(|| "\"tile_sources\" has to be a table".to_string())?;
51 72
 
52 73
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
53 74
 
@@ -95,7 +116,7 @@ impl Config {
95 116
 
96 117
                 Ok(
97 118
                     Config {
98
-                        tile_cache_dir: tile_cache_dir.to_string(),
119
+                        tile_cache_dir: tile_cache_dir,
99 120
                         sources: sources_vec,
100 121
                     }
101 122
                 )
@@ -118,3 +139,13 @@ impl Config {
118 139
         &self.sources
119 140
     }
120 141
 }
142
+
143
+#[cfg(test)]
144
+mod tests {
145
+    use config::*;
146
+
147
+    #[test]
148
+    fn default_config() {
149
+        assert!(Config::from_toml_str(DEFAULT_CONFIG).is_ok())
150
+    }
151
+}