A simple map viewer

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use std::collections::BTreeMap;
  2. use std::fs::File;
  3. use std::io::Read;
  4. use std::path::{Path, PathBuf};
  5. use tile_source::TileSource;
  6. use toml;
  7. #[derive(Deserialize, Clone, Debug)]
  8. pub struct Config {
  9. tile_cache_dir: String,
  10. sources: BTreeMap<String, Source>,
  11. }
  12. #[derive(Deserialize, Clone, Debug)]
  13. struct Source {
  14. max_zoom: u32,
  15. url_template: String,
  16. extension: String,
  17. }
  18. impl Config {
  19. pub fn from_toml<P: AsRef<Path>>(path: P) -> Option<Config> {
  20. let mut file = match File::open(path) {
  21. Ok(file) => file,
  22. Err(_) => return None,
  23. };
  24. let mut content = String::new();
  25. if file.read_to_string(&mut content).is_err() {
  26. return None;
  27. }
  28. toml::from_str(&content).ok()
  29. }
  30. pub fn tile_sources(&self) -> Vec<(String, TileSource)> {
  31. let mut vec = Vec::with_capacity(self.sources.len());
  32. for (id, (name, source)) in self.sources.iter().enumerate() {
  33. let mut path = PathBuf::from(&self.tile_cache_dir);
  34. //TODO escape name (no slashes or dots)
  35. path.push(name);
  36. vec.push((
  37. name.clone(),
  38. TileSource::new(
  39. id as u32,
  40. source.url_template.clone(),
  41. path,
  42. source.extension.clone(),
  43. source.max_zoom,
  44. ),
  45. ));
  46. }
  47. vec
  48. }
  49. }