Pārlūkot izejas kodu

Use directories crate instead of xdg

This should improve platform independence.
Johannes Hofmann 7 gadus atpakaļ
vecāks
revīzija
43e33e7072
4 mainītis faili ar 86 papildinājumiem un 55 dzēšanām
  1. 10
    7
      Cargo.lock
  2. 1
    1
      Cargo.toml
  3. 74
    46
      src/config.rs
  4. 1
    1
      src/main.rs

+ 10
- 7
Cargo.lock Parādīt failu

@@ -260,6 +260,7 @@ name = "deltamap"
260 260
 version = "0.1.0"
261 261
 dependencies = [
262 262
  "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
263
+ "directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
263 264
  "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
264 265
  "gl 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
265 266
  "gl_generator 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -272,7 +273,14 @@ dependencies = [
272 273
  "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
273 274
  "reqwest 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
274 275
  "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
275
- "xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
276
+]
277
+
278
+[[package]]
279
+name = "directories"
280
+version = "0.10.0"
281
+source = "registry+https://github.com/rust-lang/crates.io-index"
282
+dependencies = [
283
+ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
276 284
 ]
277 285
 
278 286
 [[package]]
@@ -1687,11 +1695,6 @@ dependencies = [
1687 1695
  "pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
1688 1696
 ]
1689 1697
 
1690
-[[package]]
1691
-name = "xdg"
1692
-version = "2.1.0"
1693
-source = "registry+https://github.com/rust-lang/crates.io-index"
1694
-
1695 1698
 [[package]]
1696 1699
 name = "xml-rs"
1697 1700
 version = "0.7.0"
@@ -1733,6 +1736,7 @@ dependencies = [
1733 1736
 "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
1734 1737
 "checksum crossbeam-utils 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d636a8b3bcc1b409d7ffd3facef8f21dcb4009626adbd0c5e6c4305c07253c7b"
1735 1738
 "checksum deflate 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)" = "32c8120d981901a9970a3a1c97cf8b630e0fa8c3ca31e75b6fd6fd5f9f427b31"
1739
+"checksum directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2561db021b6f1321d0f16b67ed28ce843ef4610dfaa432e3ffa2e8a3050ebf"
1736 1740
 "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a"
1737 1741
 "checksum downcast-rs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "18df8ce4470c189d18aa926022da57544f31e154631eb4cfe796aea97051fe6c"
1738 1742
 "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
@@ -1896,5 +1900,4 @@ dependencies = [
1896 1900
 "checksum winit 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "452e3331a6fb5b9d660efae533bf9ae022c528d36eb1e6278b37fc9319d58eff"
1897 1901
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
1898 1902
 "checksum x11-dl 2.17.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3235540540fde1ae074c8df49054166c0e070407f1c6e1ee17b8c87c2c7bcc7d"
1899
-"checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61"
1900 1903
 "checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"

+ 1
- 1
Cargo.toml Parādīt failu

@@ -23,7 +23,7 @@ osmpbf = "0.1"
23 23
 regex = "1.0"
24 24
 reqwest = "0.8"
25 25
 toml = "0.4"
26
-xdg = "2.1"
26
+directories = "0.10"
27 27
 
28 28
 [build-dependencies]
29 29
 gl_generator = "0.9"

+ 74
- 46
src/config.rs Parādīt failu

@@ -1,14 +1,19 @@
1 1
 use clap;
2
+use directories::ProjectDirs;
3
+use std::fmt::Debug;
2 4
 use std::fs::File;
3 5
 use std::io::{Read, Write};
4 6
 use std::path::{Path, PathBuf};
5 7
 use tile_source::TileSource;
6 8
 use toml::Value;
7
-use xdg;
8 9
 
9 10
 static DEFAULT_CONFIG: &'static str = "";
10 11
 static DEFAULT_TILE_SOURCES: &'static str = include_str!("../default_tile_sources.toml");
11 12
 
13
+lazy_static! {
14
+    static ref PROJ_DIRS: ProjectDirs = ProjectDirs::from("", "", "DeltaMap");
15
+}
16
+
12 17
 
13 18
 #[derive(Debug)]
14 19
 pub struct Config {
@@ -54,64 +59,87 @@ impl Config {
54 59
         }
55 60
     }
56 61
 
62
+    fn create_config_file<P: AsRef<Path> + Debug>(dir_path: P, file_path: P, contents: &[u8]) -> Result<(), String> {
63
+        if !dir_path.as_ref().is_dir() {
64
+            if let Err(err) = ::std::fs::create_dir_all(&dir_path) {
65
+                return Err(format!("failed to create config directory ({:?}): {}",
66
+                    dir_path,
67
+                    err
68
+                ));
69
+            }
70
+        }
71
+
72
+        let mut file = File::create(&file_path)
73
+            .map_err(|err| format!("failed to create config file {:?}: {}", &file_path, err))?;
74
+
75
+        file.write_all(contents)
76
+            .map_err(|err| format!(
77
+                "failed to write contents to config file {:?}: {}",
78
+                &file_path,
79
+                err
80
+            ))
81
+    }
82
+
57 83
     fn find_or_create() -> Result<Config, String> {
58
-        if let Ok(xdg_dirs) = xdg::BaseDirectories::with_prefix("deltamap") {
59
-            if let Some(config_path) = xdg_dirs.find_config_file("config.toml") {
60
-                info!("load config from path {:?}", config_path);
61
-
62
-                Config::from_toml_file(config_path)
63
-            } else {
64
-                // try to write a default config file
65
-                if let Ok(path) = xdg_dirs.place_config_file("config.toml") {
66
-                    if let Ok(mut file) = File::create(&path) {
67
-                        if file.write_all(DEFAULT_CONFIG.as_bytes()).is_ok() {
68
-                            info!("write default config to {:?}", &path);
69
-                        }
70
-                    }
71
-                }
84
+        let config_dir = PROJ_DIRS.config_dir();
85
+        let config_file = {
86
+            let mut path = PathBuf::from(config_dir);
87
+            path.push("config.toml");
88
+            path
89
+        };
72 90
 
73
-                Config::from_toml_str(DEFAULT_CONFIG)
74
-            }
91
+        if config_file.is_file() {
92
+            info!("load config from path {:?}", config_file);
93
+
94
+            Config::from_toml_file(config_file)
75 95
         } else {
76
-            info!("load default config");
96
+            // try to write a default config file
97
+
98
+            if let Err(err) = Config::create_config_file(
99
+                config_dir,
100
+                &config_file,
101
+                DEFAULT_CONFIG.as_bytes())
102
+            {
103
+                warn!("{}", err);
104
+            }
105
+
77 106
             Config::from_toml_str(DEFAULT_CONFIG)
78 107
         }
79 108
     }
80 109
 
81 110
     fn add_tile_sources_from_default_or_create(&mut self) -> Result<(), String> {
82
-        if let Ok(xdg_dirs) = xdg::BaseDirectories::with_prefix("deltamap") {
83
-            if let Some(sources_path) = xdg_dirs.find_config_file("tile_sources.toml") {
84
-                info!("load tile sources from path {:?}", sources_path);
85
-
86
-                self.add_tile_sources_from_file(sources_path)
87
-            } else {
88
-                // try to write a default tile sources file
89
-                if let Ok(path) = xdg_dirs.place_config_file("tile_sources.toml") {
90
-                    if let Ok(mut file) = File::create(&path) {
91
-                        if file.write_all(DEFAULT_TILE_SOURCES.as_bytes()).is_ok() {
92
-                            info!("write default tile sources to {:?}", &path);
93
-                        }
94
-                    }
95
-                }
111
+        let config_dir = PROJ_DIRS.config_dir();
112
+        let sources_file = {
113
+            let mut path = PathBuf::from(config_dir);
114
+            path.push("tile_sources.toml");
115
+            path
116
+        };
96 117
 
97
-                self.add_tile_sources_from_str(DEFAULT_TILE_SOURCES)
98
-            }
118
+        if sources_file.is_file() {
119
+            info!("load tile sources from path {:?}", sources_file);
120
+
121
+            self.add_tile_sources_from_file(sources_file)
99 122
         } else {
100
-            info!("load default config");
123
+            // try to write a default config file
124
+
125
+            if let Err(err) = Config::create_config_file(
126
+                config_dir,
127
+                &sources_file,
128
+                DEFAULT_TILE_SOURCES.as_bytes())
129
+            {
130
+                warn!("{}", err);
131
+            }
132
+
101 133
             self.add_tile_sources_from_str(DEFAULT_TILE_SOURCES)
102 134
         }
103 135
     }
104 136
 
105
-    /// Returns a tile cache directory path at a standard XDG cache location. The returned path may
106
-    /// not exist.
107
-    fn default_tile_cache_dir() -> Result<PathBuf, String> {
108
-        let xdg_dirs = xdg::BaseDirectories::with_prefix("deltamap")
109
-            .map_err(|e| format!("{}", e))?;
110
-
111
-        match xdg_dirs.find_cache_file("tiles") {
112
-            Some(dir) => Ok(dir),
113
-            None => Ok(xdg_dirs.get_cache_home().join("tiles")),
114
-        }
137
+    /// Returns a tile cache directory path at a standard location. The returned path may not
138
+    /// exist.
139
+    fn default_tile_cache_dir() -> PathBuf {
140
+        let mut path = PathBuf::from(PROJ_DIRS.cache_dir());
141
+        path.push("tiles");
142
+        path
115 143
     }
116 144
 
117 145
     fn from_toml_str(toml_str: &str) -> Result<Config, String> {
@@ -125,7 +153,7 @@ impl Config {
125 153
                                    .ok_or_else(|| "tile_cache_dir has to be a string".to_string())?
126 154
                             )
127 155
                         },
128
-                        None => Config::default_tile_cache_dir()?,
156
+                        None => Config::default_tile_cache_dir(),
129 157
                     }
130 158
                 };
131 159
 

+ 1
- 1
src/main.rs Parādīt failu

@@ -1,5 +1,6 @@
1 1
 #[macro_use]
2 2
 extern crate clap;
3
+extern crate directories;
3 4
 extern crate env_logger;
4 5
 extern crate glutin;
5 6
 extern crate image;
@@ -11,7 +12,6 @@ extern crate log;
11 12
 extern crate regex;
12 13
 extern crate reqwest;
13 14
 extern crate toml;
14
-extern crate xdg;
15 15
 
16 16
 pub mod args;
17 17
 #[macro_use]