Browse Source

config: Find a default tile cache directory

Johannes Hofmann 8 years ago
parent
commit
c1aab5f87a
2 changed files with 42 additions and 13 deletions
  1. 2
    4
      default_config.toml
  2. 40
    9
      src/config.rs

+ 2
- 4
default_config.toml View File

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

+ 40
- 9
src/config.rs View File

9
 static DEFAULT_CONFIG: &'static str = include_str!("../default_config.toml");
9
 static DEFAULT_CONFIG: &'static str = include_str!("../default_config.toml");
10
 
10
 
11
 
11
 
12
+#[derive(Debug)]
12
 pub struct Config {
13
 pub struct Config {
13
-    tile_cache_dir: String,
14
+    tile_cache_dir: PathBuf,
14
     sources: Vec<(String, TileSource)>,
15
     sources: Vec<(String, TileSource)>,
15
 }
16
 }
16
 
17
 
22
 
23
 
23
                 Config::from_toml_file(config_path)
24
                 Config::from_toml_file(config_path)
24
             } else {
25
             } else {
26
+                // try to write a default config file
25
                 if let Ok(path) = xdg_dirs.place_config_file("config.toml") {
27
                 if let Ok(path) = xdg_dirs.place_config_file("config.toml") {
26
                     if let Ok(mut file) = File::create(&path) {
28
                     if let Ok(mut file) = File::create(&path) {
27
                         println!("write default config {:?}", &path);
29
                         println!("write default config {:?}", &path);
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
     pub fn from_toml_str(toml_str: &str) -> Result<Config, String> {
53
     pub fn from_toml_str(toml_str: &str) -> Result<Config, String> {
40
         match toml_str.parse::<Value>() {
54
         match toml_str.parse::<Value>() {
41
             Ok(Value::Table(ref table)) => {
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
                     .as_table()
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
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
73
                 let mut sources_vec: Vec<(String, TileSource)> = Vec::with_capacity(sources_table.len());
53
 
74
 
95
 
116
 
96
                 Ok(
117
                 Ok(
97
                     Config {
118
                     Config {
98
-                        tile_cache_dir: tile_cache_dir.to_string(),
119
+                        tile_cache_dir: tile_cache_dir,
99
                         sources: sources_vec,
120
                         sources: sources_vec,
100
                     }
121
                     }
101
                 )
122
                 )
118
         &self.sources
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
+}