Use serde_yaml::with::singleton_map_recursive

pull/522/head
Arijit Basu 2 years ago
parent 77852b435f
commit 65eee2dc90
No known key found for this signature in database
GPG Key ID: 0F8EF5258DC38077

@ -162,12 +162,8 @@ pub struct InputBuffer {
#[derive(Debug, Serialize, Deserialize)]
pub struct App {
pub bin: String,
pub version: String,
#[serde(with = "serde_yaml::with::singleton_map_recursive")]
pub config: Config,
pub pwd: String,
pub directory_buffer: Option<DirectoryBuffer>,
pub last_focus: HashMap<String, Option<String>>,

@ -1,6 +1,5 @@
use crate::app;
use crate::{app, yaml};
use anyhow::{bail, Context, Result};
use app::ExternalMsg;
use serde_json as json;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
@ -108,7 +107,7 @@ impl Cli {
"--on-load" => {
while let Some(msg) = args.next_if(|msg| !msg.starts_with('-')) {
cli.on_load.push(msg.trim().try_into()?);
cli.on_load.push(yaml::from_str(&msg)?);
}
}
@ -215,7 +214,8 @@ fn fmt_msg_in(args: Vec<String>) -> Result<String> {
bail!("too many arguments")
}
// Validate
let msg = json::to_string(&ExternalMsg::try_from(msg.as_str())?)?;
let msg: yaml::Value = yaml::from_str(&msg)?;
let msg = json::to_string(&msg)?;
Ok(msg)
}

@ -17,6 +17,7 @@ pub mod pipe;
pub mod pwd_watcher;
pub mod runner;
pub mod ui;
pub mod yaml;
#[cfg(test)]
mod tests {

@ -2,8 +2,6 @@ use crate::{app::Node, input::InputOperation};
use indexmap::IndexSet;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json as json;
use serde_yaml as yaml;
use std::cmp::Ordering;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
@ -1085,24 +1083,6 @@ pub enum ExternalMsg {
Terminate,
}
impl TryFrom<&str> for ExternalMsg {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let msg = if let Ok(val) = json::from_str(value) {
val
} else if value.starts_with('!') {
yaml::from_str(value)?
} else if let Some((msg, args)) = value.split_once(' ') {
let msg = format!("!{} {}", msg.trim_end_matches(':'), args);
yaml::from_str(&msg)?
} else {
yaml::from_str(value)?
};
Ok(msg)
}
}
impl ExternalMsg {
pub fn is_read_only(&self) -> bool {
!matches!(

@ -1,4 +1,5 @@
use crate::app::ExternalMsg;
use crate::yaml;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
@ -69,7 +70,7 @@ pub fn read_all(pipe: &str, delimiter: char) -> Result<Vec<ExternalMsg>> {
let mut msgs = vec![];
for msg in in_str.trim_matches(delimiter).split(delimiter) {
if !msg.is_empty() {
msgs.push(msg.try_into()?);
msgs.push(yaml::from_str(msg)?);
}
}
Ok(msgs)

@ -9,6 +9,7 @@ use crate::lua;
use crate::pipe;
use crate::pwd_watcher;
use crate::ui;
use crate::yaml;
use anyhow::{bail, Error, Result};
use crossterm::event;
use crossterm::execute;
@ -386,13 +387,13 @@ impl Runner {
}
PrintAppStateAndQuit => {
let out = serde_yaml::to_string(&app)?;
let out = yaml::to_string(&app)?;
result = Ok(Some(out));
break 'outer;
}
Debug(path) => {
fs::write(&path, serde_yaml::to_string(&app)?)?;
fs::write(&path, yaml::to_string(&app)?)?;
}
ClearScreen => {

@ -0,0 +1,22 @@
use serde_yaml::with::singleton_map_recursive;
pub use serde_yaml::Result;
pub use serde_yaml::Value;
pub fn to_string<T>(value: &T) -> Result<String>
where
T: ?Sized + serde::Serialize,
{
let mut vec = Vec::with_capacity(128);
let mut serializer = serde_yaml::Serializer::new(&mut vec);
singleton_map_recursive::serialize(&value, &mut serializer)?;
String::from_utf8(vec).map_err(serde::ser::Error::custom)
}
pub fn from_str<'de, T>(s: &'de str) -> Result<T>
where
T: serde::Deserialize<'de>,
{
let deserializer = serde_yaml::Deserializer::from_str(&s);
singleton_map_recursive::deserialize(deserializer)
}
Loading…
Cancel
Save