]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/config/network.rs: use a simple String for comments
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 24 Apr 2020 05:46:08 +0000 (07:46 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 24 Apr 2020 05:46:08 +0000 (07:46 +0200)
src/api2/config/network.rs
src/api2/types.rs
src/config/network.rs
src/config/network/parser.rs

index d0b4e94246b3db89adaea879b69ef6c170cdda07..1931196e27a9f9b5b30e6b21f6026b3470440984 100644 (file)
@@ -126,12 +126,12 @@ pub enum DeletableProperty {
                 optional: true,
             },
             comments_v4: {
-                description: "Comments (inet)",
+                description: "Comments (inet, may span multiple lines)",
                 type: String,
                 optional: true,
             },
             comments_v6: {
-                description: "Comments (inet6)",
+                description: "Comments (inet5, may span multiple lines)",
                 type: String,
                 optional: true,
             },
@@ -221,8 +221,8 @@ pub fn update_interface(
                 DeletableProperty::gateway_v6 => { interface.gateway_v6 = None; },
                 DeletableProperty::method_v4 => { interface.method_v4 = None; },
                 DeletableProperty::method_v6 => { interface.method_v6 = None; },
-                DeletableProperty::comments_v4 => { interface.comments_v4 = Vec::new(); },
-                DeletableProperty::comments_v6 => { interface.comments_v6 = Vec::new(); },
+                DeletableProperty::comments_v4 => { interface.comments_v4 = None; },
+                DeletableProperty::comments_v6 => { interface.comments_v6 = None; },
                 DeletableProperty::mtu => { interface.mtu = None; },
                 DeletableProperty::auto => { interface.auto = false; },
                 DeletableProperty::bridge_ports => { interface.set_bridge_ports(Vec::new())?; }
@@ -266,13 +266,8 @@ pub fn update_interface(
         }
     }
 
-    if let Some(comments) = comments_v4 {
-        interface.comments_v4 = comments.lines().map(String::from).collect();
-    }
-
-    if let Some(comments) = comments_v6 {
-        interface.comments_v6 = comments.lines().map(String::from).collect();
-    }
+    if comments_v4.is_some() { interface.comments_v4 = comments_v4; }
+    if comments_v6.is_some() { interface.comments_v6 = comments_v6; }
 
     network::save_config(&config)?;
 
index 7595a54ce95863ae0ca40722b041b7e7e3259f58..90207861d8111ca4305de1fbbb6979d11c66cbb7 100644 (file)
@@ -587,20 +587,14 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
             },
         },
         comments_v4: {
-            description: "Comments (inet)",
-            type: Array,
-            items: {
-                description: "Comment line.",
-                type: String,
-            },
+            description: "Comments (inet, may span multiple lines)",
+            type: String,
+            optional: true,
         },
         comments_v6: {
-            description: "Comments (inet6)",
-            type: Array,
-            items: {
-                description: "Comment line.",
-                type: String,
-            },
+            description: "Comments (inet6, may span multiple lines)",
+            type: String,
+            optional: true,
         },
         bridge_ports: {
             schema: NETWORK_INTERFACE_LIST_SCHEMA,
@@ -645,10 +639,10 @@ pub struct Interface {
     #[serde(skip_serializing_if="Vec::is_empty")]
     pub options_v6: Vec<String>,
 
-    #[serde(skip_serializing_if="Vec::is_empty")]
-    pub comments_v4: Vec<String>,
-    #[serde(skip_serializing_if="Vec::is_empty")]
-    pub comments_v6: Vec<String>,
+    #[serde(skip_serializing_if="Option::is_none")]
+    pub comments_v4: Option<String>,
+    #[serde(skip_serializing_if="Option::is_none")]
+    pub comments_v6: Option<String>,
 
     #[serde(skip_serializing_if="Option::is_none")]
     /// Maximum Transmission Unit
index 12e13b8ce02addef07509ad519efbea249cb4d2c..5b263115078a201e04ca77716172166910aabd79 100644 (file)
@@ -32,8 +32,8 @@ impl Interface {
             gateway_v6: None,
             options_v4: Vec::new(),
             options_v6: Vec::new(),
-            comments_v4: Vec::new(),
-            comments_v6: Vec::new(),
+            comments_v4: None,
+            comments_v6: None,
             mtu: None,
             bridge_ports: None,
             bond_slaves: None,
@@ -166,8 +166,10 @@ impl Interface {
             writeln!(w, "    {}", option)?;
         }
 
-        for comment in &self.comments_v4 {
-            writeln!(w, "#4{}", comment)?;
+        if let Some(ref comments) = self.comments_v4 {
+            for comment in comments.lines() {
+                writeln!(w, "#{}", comment)?;
+            }
         }
 
         Ok(())
@@ -188,8 +190,10 @@ impl Interface {
             writeln!(w, "    {}", option)?;
         }
 
-        for comment in &self.comments_v6 {
-            writeln!(w, "#6{}", comment)?;
+        if let Some(ref comments) = self.comments_v6 {
+            for comment in comments.lines() {
+                writeln!(w, "#{}", comment)?;
+            }
         }
 
         Ok(())
@@ -220,8 +224,8 @@ impl Interface {
                 bond_slaves: _bond_slaves,
             } => {
                 method_v4 == method_v6
-                    && comments_v4.is_empty()
-                    && comments_v6.is_empty()
+                    && comments_v4.is_none()
+                    && comments_v6.is_none()
                     && options_v4.is_empty()
                     && options_v6.is_empty()
             }
index 9d1c7a113024fd9693063d7cc836c712f9d65c57..c63c34778559d750485a54c0d075e3c5041d5766 100644 (file)
@@ -182,9 +182,15 @@ impl <R: BufRead> NetworkParser<R> {
                 Token::Comment => {
                     let comment = self.eat(Token::Comment)?;
                     if !address_family_v4 && address_family_v6 {
-                        interface.comments_v6.push(comment);
+                        let mut comments = interface.comments_v6.take().unwrap_or(String::new());
+                        if !comments.is_empty() { comments.push('\n'); }
+                        comments.push_str(&comment);
+                        interface.comments_v6 = Some(comments);
                     } else {
-                        interface.comments_v4.push(comment);
+                        let mut comments = interface.comments_v4.take().unwrap_or(String::new());
+                        if !comments.is_empty() { comments.push('\n'); }
+                        comments.push_str(&comment);
+                        interface.comments_v4 = Some(comments);
                     }
                     self.eat(Token::Newline)?;
                     continue;