Blame SOURCES/Enable_clib_yml_api.patch

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