Blame SOURCES/Enable_clib_yml_api.patch

0f3ee0
From ad2bfa136290e72cdfd4b7877b49b3fc07203f9c Mon Sep 17 00:00:00 2001
0f3ee0
From: Gris Ge <fge@redhat.com>
0f3ee0
Date: Tue, 21 Feb 2023 16:26:22 +0800
0f3ee0
Subject: [PATCH] clib: Introduce YAML support
0f3ee0
0f3ee0
Allowing both YAML and JSON input, the output format will matching input
0f3ee0
format.
0f3ee0
0f3ee0
For `nmstate_net_state_retrieve()`, user can use
0f3ee0
`NMSTATE_FLAG_YAML_OUTPUT` flag to instruct the output to be YAML
0f3ee0
format.
0f3ee0
0f3ee0
Signed-off-by: Gris Ge <fge@redhat.com>
0f3ee0
---
0f3ee0
 rust/src/clib/Cargo.toml                      |  1 +
0f3ee0
 rust/src/clib/apply.rs                        |  2 +-
0f3ee0
 rust/src/clib/gen_conf.rs                     | 52 +++++++-----
0f3ee0
 rust/src/clib/nmstate.h.in                    | 55 +++++++------
0f3ee0
 rust/src/clib/policy.rs                       | 57 ++++++++-----
0f3ee0
 rust/src/clib/query.rs                        | 49 ++++++++----
0f3ee0
 .../{nmpolicy_test.c => nmpolicy_json_test.c} |  5 ++
0f3ee0
 rust/src/clib/test/nmpolicy_yaml_test.c       | 80 +++++++++++++++++++
0f3ee0
 .../{nmstate_test.c => nmstate_json_test.c}   |  5 ++
0f3ee0
 rust/src/clib/test/nmstate_yaml_test.c        | 34 ++++++++
0f3ee0
 rust/src/lib/Cargo.toml                       |  3 +
0f3ee0
 rust/src/lib/net_state.rs                     | 14 +++-
0f3ee0
 12 files changed, 274 insertions(+), 83 deletions(-)
0f3ee0
 rename rust/src/clib/test/{nmpolicy_test.c => nmpolicy_json_test.c} (96%)
0f3ee0
 create mode 100644 rust/src/clib/test/nmpolicy_yaml_test.c
0f3ee0
 rename rust/src/clib/test/{nmstate_test.c => nmstate_json_test.c} (87%)
0f3ee0
 create mode 100644 rust/src/clib/test/nmstate_yaml_test.c
0f3ee0
0f3ee0
diff --git a/rust/src/clib/Cargo.toml b/rust/src/clib/Cargo.toml
0f3ee0
index 97e4128c..ed391b3a 100644
0f3ee0
--- a/rust/src/clib/Cargo.toml
0f3ee0
+++ b/rust/src/clib/Cargo.toml
0f3ee0
@@ -16,6 +16,7 @@ crate-type = ["cdylib", "staticlib"]
0f3ee0
 nmstate = { path = "../lib", default-features = false }
0f3ee0
 libc = "0.2.74"
0f3ee0
 serde_json = "1.0"
0f3ee0
+serde_yaml = "0.9"
0f3ee0
 log = "0.4.17"
0f3ee0
 serde = { version = "1.0.137", features = ["derive"] }
0f3ee0
 once_cell = "1.12.0"
0f3ee0
diff --git a/rust/src/clib/apply.rs b/rust/src/clib/apply.rs
0f3ee0
index 9a0d6fbc..67d39730 100644
0f3ee0
--- a/rust/src/clib/apply.rs
0f3ee0
+++ b/rust/src/clib/apply.rs
0f3ee0
@@ -74,7 +74,7 @@ pub extern "C" fn nmstate_net_state_apply(
0f3ee0
     };
0f3ee0
 
0f3ee0
     let mut net_state =
0f3ee0
-        match nmstate::NetworkState::new_from_json(net_state_str) {
0f3ee0
+        match nmstate::NetworkState::new_from_yaml(net_state_str) {
0f3ee0
             Ok(n) => n,
0f3ee0
             Err(e) => {
0f3ee0
                 unsafe {
0f3ee0
diff --git a/rust/src/clib/gen_conf.rs b/rust/src/clib/gen_conf.rs
0f3ee0
index f63fb7b0..1ad7156b 100644
0f3ee0
--- a/rust/src/clib/gen_conf.rs
0f3ee0
+++ b/rust/src/clib/gen_conf.rs
0f3ee0
@@ -68,7 +68,7 @@ pub extern "C" fn nmstate_generate_configurations(
0f3ee0
         }
0f3ee0
     };
0f3ee0
 
0f3ee0
-    let net_state = match nmstate::NetworkState::new_from_json(net_state_str) {
0f3ee0
+    let net_state = match nmstate::NetworkState::new_from_yaml(net_state_str) {
0f3ee0
         Ok(n) => n,
0f3ee0
         Err(e) => {
0f3ee0
             unsafe {
0f3ee0
@@ -80,28 +80,44 @@ pub extern "C" fn nmstate_generate_configurations(
0f3ee0
         }
0f3ee0
     };
0f3ee0
 
0f3ee0
+    let input_is_json =
0f3ee0
+        serde_json::from_str::<serde_json::Value>(net_state_str).is_ok();
0f3ee0
     let result = net_state.gen_conf();
0f3ee0
     unsafe {
0f3ee0
         *log = CString::new(logger.drain(now)).unwrap().into_raw();
0f3ee0
     }
0f3ee0
     match result {
0f3ee0
-        Ok(s) => match serde_json::to_string(&s) {
0f3ee0
-            Ok(cfgs) => unsafe {
0f3ee0
-                *configs = CString::new(cfgs).unwrap().into_raw();
0f3ee0
-                NMSTATE_PASS
0f3ee0
-            },
0f3ee0
-            Err(e) => unsafe {
0f3ee0
-                *err_msg =
0f3ee0
-                    CString::new(format!("serde_json::to_string failure: {e}"))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                *err_kind =
0f3ee0
-                    CString::new(format!("{}", nmstate::ErrorKind::Bug))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                NMSTATE_FAIL
0f3ee0
-            },
0f3ee0
-        },
0f3ee0
+        Ok(s) => {
0f3ee0
+            let serialize = if input_is_json {
0f3ee0
+                serde_json::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to JSON: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            } else {
0f3ee0
+                serde_yaml::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to YAML: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            };
0f3ee0
+
0f3ee0
+            match serialize {
0f3ee0
+                Ok(cfgs) => unsafe {
0f3ee0
+                    *configs = CString::new(cfgs).unwrap().into_raw();
0f3ee0
+                    NMSTATE_PASS
0f3ee0
+                },
0f3ee0
+                Err(e) => unsafe {
0f3ee0
+                    *err_msg =
0f3ee0
+                        CString::new(e.msg().to_string()).unwrap().into_raw();
0f3ee0
+                    *err_kind =
0f3ee0
+                        CString::new(e.kind().to_string()).unwrap().into_raw();
0f3ee0
+                    NMSTATE_FAIL
0f3ee0
+                },
0f3ee0
+            }
0f3ee0
+        }
0f3ee0
         Err(e) => {
0f3ee0
             unsafe {
0f3ee0
                 *err_msg = CString::new(e.msg()).unwrap().into_raw();
0f3ee0
diff --git a/rust/src/clib/nmstate.h.in b/rust/src/clib/nmstate.h.in
0f3ee0
index 0879d47e..391477fd 100644
0f3ee0
--- a/rust/src/clib/nmstate.h.in
0f3ee0
+++ b/rust/src/clib/nmstate.h.in
0f3ee0
@@ -1,19 +1,4 @@
0f3ee0
-/*
0f3ee0
- * Copyright 2021 Red Hat
0f3ee0
- *
0f3ee0
- * Licensed under the Apache License, Version 2.0 (the "License");
0f3ee0
- * you may not use this file except in compliance with the License.
0f3ee0
- * You may obtain a copy of the License at
0f3ee0
- *
0f3ee0
- *     http://www.apache.org/licenses/LICENSE-2.0
0f3ee0
- *
0f3ee0
- * Unless required by applicable law or agreed to in writing, software
0f3ee0
- * distributed under the License is distributed on an "AS IS" BASIS,
0f3ee0
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0f3ee0
- * See the License for the specific language governing permissions and
0f3ee0
- * limitations under the License.
0f3ee0
- */
0f3ee0
-
0f3ee0
+// SPDX-License-Identifier: Apache-2.0
0f3ee0
 
0f3ee0
 #ifndef _LIBNMSTATE_H_
0f3ee0
 #define _LIBNMSTATE_H_
0f3ee0
@@ -44,6 +29,7 @@ extern "C" {
0f3ee0
 #define NMSTATE_FLAG_NO_COMMIT              1 << 5
0f3ee0
 #define NMSTATE_FLAG_MEMORY_ONLY            1 << 6
0f3ee0
 #define NMSTATE_FLAG_RUNNING_CONFIG_ONLY    1 << 7
0f3ee0
+#define NMSTATE_FLAG_YAML_OUTPUT            1 << 8
0f3ee0
 
0f3ee0
 /**
0f3ee0
  * nmstate_net_state_retrieve - Retrieve network state
0f3ee0
@@ -52,7 +38,7 @@ extern "C" {
0f3ee0
  *      0.1
0f3ee0
  *
0f3ee0
  * Description:
0f3ee0
- *      Retrieve network state in the format of JSON.
0f3ee0
+ *      Retrieve network state in the format of JSON or YAML.
0f3ee0
  *
0f3ee0
  * @flags:
0f3ee0
  *      Flags for special use cases:
0f3ee0
@@ -60,6 +46,13 @@ extern "C" {
0f3ee0
  *              No flag
0f3ee0
  *          * NMSTATE_FLAG_KERNEL_ONLY
0f3ee0
  *              Do not use external plugins, show kernel status only.
0f3ee0
+ *          * NMSTATE_FLAG_INCLUDE_SECRETS
0f3ee0
+ *              No not hide sercerts like password.
0f3ee0
+ *          * NMSTATE_FLAG_RUNNING_CONFIG_ONLY
0f3ee0
+ *              Only include running config excluding running status like auto
0f3ee0
+ *              IP addresses and routes, LLDP neighbors.
0f3ee0
+ *          * NMSTATE_FLAG_YAML_OUTPUT
0f3ee0
+ *              Show the state in YAML format
0f3ee0
  * @state:
0f3ee0
  *      Output pointer of char array for network state in json format.
0f3ee0
  *      The memory should be freed by nmstate_net_state_free().
0f3ee0
@@ -90,7 +83,7 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
0f3ee0
  *      0.1
0f3ee0
  *
0f3ee0
  * Description:
0f3ee0
- *      Apply network state in the format of JSON.
0f3ee0
+ *      Apply network state in the format of JSON or YAML.
0f3ee0
  *
0f3ee0
  * @flags:
0f3ee0
  *      Flags for special use cases:
0f3ee0
@@ -98,8 +91,12 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
0f3ee0
  *              No flag
0f3ee0
  *          * NMSTATE_FLAG_KERNEL_ONLY
0f3ee0
  *              Do not use external plugins, apply to kernel only.
0f3ee0
+ *          * NMSTATE_FLAG_NO_VERIFY
0f3ee0
+ *              Do not verify state after applied
0f3ee0
  *          * NMSTATE_FLAG_NO_COMMIT
0f3ee0
  *              Do not commit new state after verification
0f3ee0
+ *          * NMSTATE_FLAG_MEMORY_ONLY
0f3ee0
+ *              No not store network state to persistent.
0f3ee0
  * @state:
0f3ee0
  *      Pointer of char array for network state in json format.
0f3ee0
  * @log:
0f3ee0
@@ -119,7 +116,8 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
0f3ee0
  *          * NMSTATE_FAIL
0f3ee0
  *              On failure.
0f3ee0
  */
0f3ee0
-int nmstate_net_state_apply(uint32_t flags, const char *state, uint32_t rollback_timeout, char **log,
0f3ee0
+int nmstate_net_state_apply(uint32_t flags, const char *state,
0f3ee0
+                            uint32_t rollback_timeout, char **log,
0f3ee0
                             char **err_kind, char **err_msg);
0f3ee0
 
0f3ee0
 /**
0f3ee0
@@ -151,8 +149,8 @@ int nmstate_net_state_apply(uint32_t flags, const char *state, uint32_t rollback
0f3ee0
  *          * NMSTATE_FAIL
0f3ee0
  *              On failure.
0f3ee0
  */
0f3ee0
-int nmstate_checkpoint_commit(const char *checkpoint, char **log, char **err_kind,
0f3ee0
-                          char **err_msg);
0f3ee0
+int nmstate_checkpoint_commit(const char *checkpoint, char **log, 
0f3ee0
+                              char **err_kind, char **err_msg);
0f3ee0
 
0f3ee0
 /**
0f3ee0
  * nmstate_checkpoint_rollback - Rollback the checkpoint
0f3ee0
@@ -183,8 +181,8 @@ int nmstate_checkpoint_commit(const char *checkpoint, char **log, char **err_kin
0f3ee0
  *          * NMSTATE_FAIL
0f3ee0
  *              On failure.
0f3ee0
  */
0f3ee0
-int nmstate_checkpoint_rollback(const char *checkpoint, char **log, char **err_kind,
0f3ee0
-                                 char **err_msg);
0f3ee0
+int nmstate_checkpoint_rollback(const char *checkpoint, char **log, 
0f3ee0
+                                char **err_kind, char **err_msg);
0f3ee0
 
0f3ee0
 /**
0f3ee0
  * nmstate_generate_configurations - Generate network configurations
0f3ee0
@@ -199,9 +197,10 @@ int nmstate_checkpoint_rollback(const char *checkpoint, char **log, char **err_k
0f3ee0
  *      as value.
0f3ee0
  *
0f3ee0
  * @state:
0f3ee0
- *      Pointer of char array for network state in json format.
0f3ee0
+ *      Pointer of char array for network state in JSON or YAML format.
0f3ee0
  * @configs:
0f3ee0
- *      Output pointer of char array for network configures in json format.
0f3ee0
+ *      Output pointer of char array for network configures in JSON or
0f3ee0
+ *      YAML(depend on which format you use in @state) format.
0f3ee0
  *      The memory should be freed by nmstate_net_state_free().
0f3ee0
  * @log:
0f3ee0
  *      Output pointer of char array for logging.
0f3ee0
@@ -231,14 +230,14 @@ int nmstate_generate_configurations(const char *state, char **configs,
0f3ee0
  *      2.2
0f3ee0
  *
0f3ee0
  * Description:
0f3ee0
- *      Generate new network state from policy again specifed state
0f3ee0
+ *      Generate new network state from policy again specified state
0f3ee0
  *
0f3ee0
  * @policy:
0f3ee0
- *      Pointer of char array for network policy in json format.
0f3ee0
+ *      Pointer of char array for network policy in JSON/YAML format.
0f3ee0
  * @current_state:
0f3ee0
  *      Pointer of char array for current network state.
0f3ee0
  * @state:
0f3ee0
- *      Output pointer of char array for network state in json format.
0f3ee0
+ *      Output pointer of char array for network state in JSON/YAML format.
0f3ee0
  *      The memory should be freed by nmstate_net_state_free().
0f3ee0
  * @log:
0f3ee0
  *      Output pointer of char array for logging.
0f3ee0
diff --git a/rust/src/clib/policy.rs b/rust/src/clib/policy.rs
0f3ee0
index ec8c46c1..ea7dd036 100644
0f3ee0
--- a/rust/src/clib/policy.rs
0f3ee0
+++ b/rust/src/clib/policy.rs
0f3ee0
@@ -67,6 +67,13 @@ pub extern "C" fn nmstate_net_state_from_policy(
0f3ee0
         }
0f3ee0
     };
0f3ee0
 
0f3ee0
+    let input_is_json =
0f3ee0
+        if let Ok(policy_str) = unsafe { CStr::from_ptr(policy) }.to_str() {
0f3ee0
+            serde_json::from_str::<serde_json::Value>(policy_str).is_ok()
0f3ee0
+        } else {
0f3ee0
+            false
0f3ee0
+        };
0f3ee0
+
0f3ee0
     let mut policy = match deserilize_from_c_char::<NetworkPolicy>(
0f3ee0
         policy, err_kind, err_msg,
0f3ee0
     ) {
0f3ee0
@@ -86,23 +93,37 @@ pub extern "C" fn nmstate_net_state_from_policy(
0f3ee0
     }
0f3ee0
 
0f3ee0
     match result {
0f3ee0
-        Ok(s) => match serde_json::to_string(&s) {
0f3ee0
-            Ok(state_str) => unsafe {
0f3ee0
-                *state = CString::new(state_str).unwrap().into_raw();
0f3ee0
-                NMSTATE_PASS
0f3ee0
-            },
0f3ee0
-            Err(e) => unsafe {
0f3ee0
-                *err_msg =
0f3ee0
-                    CString::new(format!("serde_json::to_string failure: {e}"))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                *err_kind =
0f3ee0
-                    CString::new(format!("{}", nmstate::ErrorKind::Bug))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                NMSTATE_FAIL
0f3ee0
-            },
0f3ee0
-        },
0f3ee0
+        Ok(s) => {
0f3ee0
+            let serialize = if input_is_json {
0f3ee0
+                serde_json::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to JSON: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            } else {
0f3ee0
+                serde_yaml::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to YAML: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            };
0f3ee0
+
0f3ee0
+            match serialize {
0f3ee0
+                Ok(state_str) => unsafe {
0f3ee0
+                    *state = CString::new(state_str).unwrap().into_raw();
0f3ee0
+                    NMSTATE_PASS
0f3ee0
+                },
0f3ee0
+                Err(e) => unsafe {
0f3ee0
+                    *err_msg =
0f3ee0
+                        CString::new(e.msg().to_string()).unwrap().into_raw();
0f3ee0
+                    *err_kind =
0f3ee0
+                        CString::new(e.kind().to_string()).unwrap().into_raw();
0f3ee0
+                    NMSTATE_FAIL
0f3ee0
+                },
0f3ee0
+            }
0f3ee0
+        }
0f3ee0
         Err(e) => {
0f3ee0
             unsafe {
0f3ee0
                 *err_msg = CString::new(e.msg()).unwrap().into_raw();
0f3ee0
@@ -144,7 +165,7 @@ where
0f3ee0
         }
0f3ee0
     };
0f3ee0
 
0f3ee0
-    match serde_json::from_str(content_str) {
0f3ee0
+    match serde_yaml::from_str(content_str) {
0f3ee0
         Ok(n) => Some(n),
0f3ee0
         Err(e) => {
0f3ee0
             unsafe {
0f3ee0
diff --git a/rust/src/clib/query.rs b/rust/src/clib/query.rs
0f3ee0
index a24b9c83..12e44d05 100644
0f3ee0
--- a/rust/src/clib/query.rs
0f3ee0
+++ b/rust/src/clib/query.rs
0f3ee0
@@ -14,6 +14,7 @@ pub(crate) const NMSTATE_FLAG_INCLUDE_SECRETS: u32 = 1 << 4;
0f3ee0
 pub(crate) const NMSTATE_FLAG_NO_COMMIT: u32 = 1 << 5;
0f3ee0
 pub(crate) const NMSTATE_FLAG_MEMORY_ONLY: u32 = 1 << 6;
0f3ee0
 pub(crate) const NMSTATE_FLAG_RUNNING_CONFIG_ONLY: u32 = 1 << 7;
0f3ee0
+pub(crate) const NMSTATE_FLAG_YAML_OUTPUT: u32 = 1 << 8;
0f3ee0
 
0f3ee0
 #[allow(clippy::not_unsafe_ptr_arg_deref)]
0f3ee0
 #[no_mangle]
0f3ee0
@@ -72,23 +73,37 @@ pub extern "C" fn nmstate_net_state_retrieve(
0f3ee0
     }
0f3ee0
 
0f3ee0
     match result {
0f3ee0
-        Ok(s) => match serde_json::to_string(&s) {
0f3ee0
-            Ok(state_str) => unsafe {
0f3ee0
-                *state = CString::new(state_str).unwrap().into_raw();
0f3ee0
-                NMSTATE_PASS
0f3ee0
-            },
0f3ee0
-            Err(e) => unsafe {
0f3ee0
-                *err_msg =
0f3ee0
-                    CString::new(format!("serde_json::to_string failure: {e}"))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                *err_kind =
0f3ee0
-                    CString::new(format!("{}", nmstate::ErrorKind::Bug))
0f3ee0
-                        .unwrap()
0f3ee0
-                        .into_raw();
0f3ee0
-                NMSTATE_FAIL
0f3ee0
-            },
0f3ee0
-        },
0f3ee0
+        Ok(s) => {
0f3ee0
+            let serialize = if (flags & NMSTATE_FLAG_YAML_OUTPUT) > 0 {
0f3ee0
+                serde_yaml::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to YAML: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            } else {
0f3ee0
+                serde_json::to_string(&s).map_err(|e| {
0f3ee0
+                    nmstate::NmstateError::new(
0f3ee0
+                        nmstate::ErrorKind::Bug,
0f3ee0
+                        format!("Failed to convert state {s:?} to JSON: {e}"),
0f3ee0
+                    )
0f3ee0
+                })
0f3ee0
+            };
0f3ee0
+
0f3ee0
+            match serialize {
0f3ee0
+                Ok(state_str) => unsafe {
0f3ee0
+                    *state = CString::new(state_str).unwrap().into_raw();
0f3ee0
+                    NMSTATE_PASS
0f3ee0
+                },
0f3ee0
+                Err(e) => unsafe {
0f3ee0
+                    *err_msg =
0f3ee0
+                        CString::new(e.msg().to_string()).unwrap().into_raw();
0f3ee0
+                    *err_kind =
0f3ee0
+                        CString::new(e.kind().to_string()).unwrap().into_raw();
0f3ee0
+                    NMSTATE_FAIL
0f3ee0
+                },
0f3ee0
+            }
0f3ee0
+        }
0f3ee0
         Err(e) => {
0f3ee0
             unsafe {
0f3ee0
                 *err_msg = CString::new(e.msg()).unwrap().into_raw();
0f3ee0
diff --git a/rust/src/clib/test/nmpolicy_test.c b/rust/src/clib/test/nmpolicy_json_test.c
0f3ee0
similarity index 96%
0f3ee0
rename from rust/src/clib/test/nmpolicy_test.c
0f3ee0
rename to rust/src/clib/test/nmpolicy_json_test.c
0f3ee0
index 7a71a5f5..8a0444d4 100644
0f3ee0
--- a/rust/src/clib/test/nmpolicy_test.c
0f3ee0
+++ b/rust/src/clib/test/nmpolicy_json_test.c
0f3ee0
@@ -1,3 +1,6 @@
0f3ee0
+// SPDX-License-Identifier: Apache-2.0
0f3ee0
+
0f3ee0
+#include <assert.h>
0f3ee0
 #include <stddef.h>
0f3ee0
 #include <stdint.h>
0f3ee0
 #include <stdio.h>
0f3ee0
@@ -91,6 +94,8 @@ int main(void) {
0f3ee0
 		rc = EXIT_FAILURE;
0f3ee0
 	}
0f3ee0
 
0f3ee0
+	assert(state[0] == '{');
0f3ee0
+
0f3ee0
 	nmstate_cstring_free(state);
0f3ee0
 	nmstate_cstring_free(err_kind);
0f3ee0
 	nmstate_cstring_free(err_msg);
0f3ee0
diff --git a/rust/src/clib/test/nmpolicy_yaml_test.c b/rust/src/clib/test/nmpolicy_yaml_test.c
0f3ee0
new file mode 100644
0f3ee0
index 00000000..7984f509
0f3ee0
--- /dev/null
0f3ee0
+++ b/rust/src/clib/test/nmpolicy_yaml_test.c
0f3ee0
@@ -0,0 +1,80 @@
0f3ee0
+// SPDX-License-Identifier: Apache-2.0
0f3ee0
+
0f3ee0
+#include <assert.h>
0f3ee0
+#include <stddef.h>
0f3ee0
+#include <stdint.h>
0f3ee0
+#include <stdio.h>
0f3ee0
+#include <stdlib.h>
0f3ee0
+
0f3ee0
+#include <nmstate.h>
0f3ee0
+
0f3ee0
+int main(void) {
0f3ee0
+	int rc = EXIT_SUCCESS;
0f3ee0
+	const char *policy = "\
0f3ee0
+capture:\n\
0f3ee0
+  default-gw: override me with the cache\n\
0f3ee0
+  base-iface: >\n\
0f3ee0
+    interfaces.name == capture.default-gw.routes.running.0.next-hop-interface\n\
0f3ee0
+  base-iface-routes: >\n\
0f3ee0
+    routes.running.next-hop-interface ==\n\
0f3ee0
+    capture.default-gw.routes.running.0.next-hop-interface\n\
0f3ee0
+  bridge-routes: >\n\
0f3ee0
+    capture.base-iface-routes | routes.running.next-hop-interface:=\"br1\"\n\
0f3ee0
+desired:\n\
0f3ee0
+  interfaces:\n\
0f3ee0
+  - name: br1\n\
0f3ee0
+    description: Linux bridge with base interface as a port\n\
0f3ee0
+    type: linux-bridge\n\
0f3ee0
+    state: up\n\
0f3ee0
+    bridge:\n\
0f3ee0
+      options:\n\
0f3ee0
+        stp:\n\
0f3ee0
+          enabled: false\n\
0f3ee0
+      port:\n\
0f3ee0
+      - name: '{{ capture.base-iface.interfaces.0.name }}'\n\
0f3ee0
+    ipv4: '{{ capture.base-iface.interfaces.0.ipv4 }}'\n\
0f3ee0
+  routes:\n\
0f3ee0
+    config: '{{ capture.bridge-routes.routes.running }}'";
0f3ee0
+	const char *current_state = "\
0f3ee0
+interfaces:\n\
0f3ee0
+- name: eth1\n\
0f3ee0
+  type: ethernet\n\
0f3ee0
+  state: up\n\
0f3ee0
+  mac-address: 1c:c1:0c:32:3b:ff\n\
0f3ee0
+  ipv4:\n\
0f3ee0
+    address:\n\
0f3ee0
+    - ip: 192.0.2.251\n\
0f3ee0
+      prefix-length: 24\n\
0f3ee0
+    dhcp: false\n\
0f3ee0
+    enabled: true\n\
0f3ee0
+routes:\n\
0f3ee0
+  config:\n\
0f3ee0
+  - destination: 0.0.0.0/0\n\
0f3ee0
+    next-hop-address: 192.0.2.1\n\
0f3ee0
+    next-hop-interface: eth1\n\
0f3ee0
+  running:\n\
0f3ee0
+  - destination: 0.0.0.0/0\n\
0f3ee0
+    next-hop-address: 192.0.2.1\n\
0f3ee0
+    next-hop-interface: eth1";
0f3ee0
+	char *state = NULL;
0f3ee0
+	char *err_kind = NULL;
0f3ee0
+	char *err_msg = NULL;
0f3ee0
+	char *log = NULL;
0f3ee0
+
0f3ee0
+	if (nmstate_net_state_from_policy(policy, current_state, &state, &log,
0f3ee0
+					  &err_kind, &err_msg) == NMSTATE_PASS)
0f3ee0
+	{
0f3ee0
+		printf("%s\n", state);
0f3ee0
+	} else {
0f3ee0
+		printf("%s: %s\n", err_kind, err_msg);
0f3ee0
+		rc = EXIT_FAILURE;
0f3ee0
+	}
0f3ee0
+
0f3ee0
+	assert(state[0] != '{');
0f3ee0
+
0f3ee0
+	nmstate_cstring_free(state);
0f3ee0
+	nmstate_cstring_free(err_kind);
0f3ee0
+	nmstate_cstring_free(err_msg);
0f3ee0
+	nmstate_cstring_free(log);
0f3ee0
+	exit(rc);
0f3ee0
+}
0f3ee0
diff --git a/rust/src/clib/test/nmstate_test.c b/rust/src/clib/test/nmstate_json_test.c
0f3ee0
similarity index 87%
0f3ee0
rename from rust/src/clib/test/nmstate_test.c
0f3ee0
rename to rust/src/clib/test/nmstate_json_test.c
0f3ee0
index 0e79cb15..1bfbcda7 100644
0f3ee0
--- a/rust/src/clib/test/nmstate_test.c
0f3ee0
+++ b/rust/src/clib/test/nmstate_json_test.c
0f3ee0
@@ -1,3 +1,6 @@
0f3ee0
+// SPDX-License-Identifier: Apache-2.0
0f3ee0
+
0f3ee0
+#include <assert.h>
0f3ee0
 #include <stddef.h>
0f3ee0
 #include <stdint.h>
0f3ee0
 #include <stdio.h>
0f3ee0
@@ -21,6 +24,8 @@ int main(void) {
0f3ee0
 		rc = EXIT_FAILURE;
0f3ee0
 	}
0f3ee0
 
0f3ee0
+	assert(state[0] == '{');
0f3ee0
+
0f3ee0
 	nmstate_cstring_free(state);
0f3ee0
 	nmstate_cstring_free(err_kind);
0f3ee0
 	nmstate_cstring_free(err_msg);
0f3ee0
diff --git a/rust/src/clib/test/nmstate_yaml_test.c b/rust/src/clib/test/nmstate_yaml_test.c
0f3ee0
new file mode 100644
0f3ee0
index 00000000..de0f2486
0f3ee0
--- /dev/null
0f3ee0
+++ b/rust/src/clib/test/nmstate_yaml_test.c
0f3ee0
@@ -0,0 +1,34 @@
0f3ee0
+// SPDX-License-Identifier: Apache-2.0
0f3ee0
+
0f3ee0
+#include <assert.h>
0f3ee0
+#include <stddef.h>
0f3ee0
+#include <stdint.h>
0f3ee0
+#include <stdio.h>
0f3ee0
+#include <stdlib.h>
0f3ee0
+
0f3ee0
+#include <nmstate.h>
0f3ee0
+
0f3ee0
+int main(void) {
0f3ee0
+	int rc = EXIT_SUCCESS;
0f3ee0
+	char *state = NULL;
0f3ee0
+	char *err_kind = NULL;
0f3ee0
+	char *err_msg = NULL;
0f3ee0
+	char *log = NULL;
0f3ee0
+	uint32_t flag = NMSTATE_FLAG_KERNEL_ONLY | NMSTATE_FLAG_YAML_OUTPUT;
0f3ee0
+
0f3ee0
+	if (nmstate_net_state_retrieve(flag, &state, &log, &err_kind, &err_msg)
0f3ee0
+	    == NMSTATE_PASS) {
0f3ee0
+		printf("%s\n", state);
0f3ee0
+	} else {
0f3ee0
+		printf("%s: %s\n", err_kind, err_msg);
0f3ee0
+		rc = EXIT_FAILURE;
0f3ee0
+	}
0f3ee0
+
0f3ee0
+	assert(state[0] != '{');
0f3ee0
+
0f3ee0
+	nmstate_cstring_free(state);
0f3ee0
+	nmstate_cstring_free(err_kind);
0f3ee0
+	nmstate_cstring_free(err_msg);
0f3ee0
+	nmstate_cstring_free(log);
0f3ee0
+	exit(rc);
0f3ee0
+}
0f3ee0
diff --git a/rust/src/lib/Cargo.toml b/rust/src/lib/Cargo.toml
0f3ee0
index a27d0e1a..0142026d 100644
0f3ee0
--- a/rust/src/lib/Cargo.toml
0f3ee0
+++ b/rust/src/lib/Cargo.toml
0f3ee0
@@ -15,6 +15,9 @@ edition = "2018"
0f3ee0
 [lib]
0f3ee0
 path = "lib.rs"
0f3ee0
 
0f3ee0
+[dependencies]
0f3ee0
+serde_yaml = "0.9"
0f3ee0
+
0f3ee0
 [dependencies.nispor]
0f3ee0
 version = "1.2.9"
0f3ee0
 optional = true
0f3ee0
diff --git a/rust/src/lib/net_state.rs b/rust/src/lib/net_state.rs
0f3ee0
index 8ab79642..fe5fea78 100644
0f3ee0
--- a/rust/src/lib/net_state.rs
0f3ee0
+++ b/rust/src/lib/net_state.rs
0f3ee0
@@ -274,7 +274,19 @@ impl NetworkState {
0f3ee0
             Ok(s) => Ok(s),
0f3ee0
             Err(e) => Err(NmstateError::new(
0f3ee0
                 ErrorKind::InvalidArgument,
0f3ee0
-                format!("Invalid json string: {e}"),
0f3ee0
+                format!("Invalid JSON string: {e}"),
0f3ee0
+            )),
0f3ee0
+        }
0f3ee0
+    }
0f3ee0
+
0f3ee0
+    /// Wrapping function of [serde_yaml::from_str()] with error mapped to
0f3ee0
+    /// [NmstateError].
0f3ee0
+    pub fn new_from_yaml(net_state_yaml: &str) -> Result<Self, NmstateError> {
0f3ee0
+        match serde_yaml::from_str(net_state_yaml) {
0f3ee0
+            Ok(s) => Ok(s),
0f3ee0
+            Err(e) => Err(NmstateError::new(
0f3ee0
+                ErrorKind::InvalidArgument,
0f3ee0
+                format!("Invalid YAML string: {e}"),
0f3ee0
             )),
0f3ee0
         }
0f3ee0
     }
0f3ee0
-- 
0f3ee0
2.39.2
0f3ee0