From 80589fb9c08e3c15b31db51c7a40767ff6fbf33d Mon Sep 17 00:00:00 2001
From: Hristo Venev <hristo@venev.name>
Date: Tue, 4 Feb 2020 22:58:37 +0100
Subject: Move source names inside the source sections.

This means that the order of the sources is preserved.
---
 src/config.rs          | 5 +++--
 src/main.rs            | 6 ++++--
 src/manager/builder.rs | 4 ++--
 src/manager/mod.rs     | 8 +++-----
 src/manager/updater.rs | 8 ++++----
 5 files changed, 16 insertions(+), 15 deletions(-)

(limited to 'src')

diff --git a/src/config.rs b/src/config.rs
index 7b3aa4b..bbe6366 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -10,6 +10,7 @@ use std::path::PathBuf;
 #[derive(serde_derive::Serialize, serde_derive::Deserialize, Clone, PartialEq, Eq, Debug)]
 #[serde(deny_unknown_fields)]
 pub struct Source {
+    pub name: String,
     pub url: String,
     pub psk: Option<Secret>,
     pub ipv4: Ipv4Set,
@@ -81,7 +82,7 @@ pub struct Config {
     pub runtime_directory: Option<PathBuf>,
     pub global: GlobalConfig,
     pub updater: UpdaterConfig,
-    pub sources: HashMap<String, Source>,
+    pub sources: Vec<Source>,
 }
 
 #[derive(serde_derive::Serialize, serde_derive::Deserialize)]
@@ -101,7 +102,7 @@ struct ConfigRepr {
     refresh_sec: u32,
 
     #[serde(default, rename = "source")]
-    sources: HashMap<String, Source>,
+    sources: Vec<Source>,
 }
 
 impl From<Config> for ConfigRepr {
diff --git a/src/main.rs b/src/main.rs
index 462da48..2ab02af 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -109,13 +109,15 @@ fn cli_config(mut args: impl Iterator<Item = OsString>) -> Option<config::Config
         if key == "source" {
             let name = args.next()?.into_string().ok()?;
             let url = args.next()?.into_string().ok()?;
-            cur = State::Source(cfg.sources.entry(name).or_insert(config::Source {
+            cfg.sources.push(config::Source {
+                name,
                 url,
                 psk: None,
                 ipv4: model::Ipv4Set::new(),
                 ipv6: model::Ipv6Set::new(),
                 required: false,
-            }));
+            });
+            cur = State::Source(cfg.sources.last_mut().unwrap());
             continue;
         }
         if key == "peer" {
diff --git a/src/manager/builder.rs b/src/manager/builder.rs
index 9c2ad15..e302f7e 100644
--- a/src/manager/builder.rs
+++ b/src/manager/builder.rs
@@ -18,7 +18,7 @@ pub struct Error {
 impl Error {
     fn new(err: &'static str, src: &Source, p: &proto::Peer, important: bool) -> Self {
         Self {
-            src: src.name.clone(),
+            src: src.config.name.clone(),
             peer: p.public_key,
             important,
             err,
@@ -170,7 +170,7 @@ fn peer_contact<'a>(
 
     if let Some(pc) = gc.peers.get(&p.public_key) {
         if let Some(ref want_src) = &pc.source {
-            if *want_src != src.name {
+            if *want_src != src.config.name {
                 return Err(Error::new("peer source not allowed", src, p, true));
             }
         }
diff --git a/src/manager/mod.rs b/src/manager/mod.rs
index b7fb8c2..d4799a7 100644
--- a/src/manager/mod.rs
+++ b/src/manager/mod.rs
@@ -9,7 +9,6 @@ use std::path::PathBuf;
 use std::time::{Duration, Instant, SystemTime};
 
 struct Source {
-    name: String,
     config: config::Source,
     data: proto::Source,
     next_update: Instant,
@@ -50,8 +49,8 @@ impl Manager {
 
         let _ = m.current_load();
 
-        for (name, cfg) in c.sources {
-            m.add_source(name, cfg)?;
+        for cfg in c.sources {
+            m.add_source(cfg)?;
         }
 
         Ok(m)
@@ -92,9 +91,8 @@ impl Manager {
         }
     }
 
-    fn add_source(&mut self, name: String, config: config::Source) -> io::Result<()> {
+    fn add_source(&mut self, config: config::Source) -> io::Result<()> {
         let mut s = Source {
-            name,
             config,
             data: proto::Source::empty(),
             next_update: Instant::now(),
diff --git a/src/manager/updater.rs b/src/manager/updater.rs
index 0245e95..db24d6e 100644
--- a/src/manager/updater.rs
+++ b/src/manager/updater.rs
@@ -20,7 +20,7 @@ impl Updater {
 
     fn cache_path(&self, s: &Source) -> Option<PathBuf> {
         let mut p = self.config.cache_directory.as_ref()?.clone();
-        p.push(&s.name);
+        p.push(&s.config.name);
         Some(p)
     }
 
@@ -34,7 +34,7 @@ impl Updater {
         match fileutil::update(&path, &data) {
             Ok(()) => {}
             Err(e) => {
-                eprintln!("<4>Failed to cache [{}]: {}", &src.name, e);
+                eprintln!("<4>Failed to cache [{}]: {}", &src.config.name, e);
             }
         }
     }
@@ -51,7 +51,7 @@ impl Updater {
                 return false;
             }
             Err(e) => {
-                eprintln!("<3>Failed to read [{}] from cache: {}", &src.name, e);
+                eprintln!("<3>Failed to read [{}] from cache: {}", &src.config.name, e);
                 return false;
             }
         };
@@ -60,7 +60,7 @@ impl Updater {
         src.data = match serde::Deserialize::deserialize(&mut de) {
             Ok(r) => r,
             Err(e) => {
-                eprintln!("<3>Failed to load [{}] from cache: {}", &src.name, e);
+                eprintln!("<3>Failed to load [{}] from cache: {}", &src.config.name, e);
                 return false;
             }
         };
-- 
cgit