ron_utils/
lib.rs

1use std::{fs::read_to_string, path::Path};
2
3use ron_reboot::utf8_parser::ast_from_str;
4pub use ron_reboot::{print_error, Error};
5
6pub fn validate_str(s: &str) -> Result<(), ron_reboot::Error> {
7    ast_from_str(s).map(|_| ())
8}
9
10pub fn validate_file(p: impl AsRef<Path>) -> Result<(), ron_reboot::Error> {
11    ast_from_str(&read_fs_string(p)?).map(|_| ())
12}
13
14#[cfg(feature = "serde1")]
15pub fn validate_typed_str<'a, T: serde::Deserialize<'a>>(
16    s: &'a str,
17) -> Result<(), ron_reboot::Error> {
18    ron_reboot::from_str_serde(s)
19}
20
21#[cfg(feature = "serde1")]
22pub fn validate_typed_file<T: serde::de::DeserializeOwned>(
23    p: impl AsRef<Path>,
24) -> Result<(), ron_reboot::Error> {
25    ron_reboot::utf8_parser::serde::from_str(&read_fs_string(p)?)
26}
27
28fn read_fs_string(path: impl AsRef<Path>) -> Result<String, ron_reboot::Error> {
29    let path = path.as_ref();
30    read_to_string(path)
31        .map_err(ron_reboot::Error::from)
32        .map_err(|e: ron_reboot::Error| e.context_file_name(path.display().to_string()))
33}