-
Notifications
You must be signed in to change notification settings - Fork 991
Expand file tree
/
Copy pathsort.rs
More file actions
129 lines (108 loc) 路 3.19 KB
/
Copy pathsort.rs
File metadata and controls
129 lines (108 loc) 路 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::str::FromStr;
use anyhow::bail;
use mlua::{FromLua, IntoLua, Lua, LuaSerdeExt, Value};
use serde::{Deserialize, Serialize};
use yazi_binding::SER_OPT;
use yazi_fs::{SortBy, SortFallback};
use yazi_shared::{data::Data, event::ActionCow};
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct SortOpt {
pub by: Option<SortBy>,
pub reverse: Option<SortBoolState>,
pub dir_first: Option<SortBoolState>,
pub sensitive: Option<bool>,
pub translit: Option<SortBoolState>,
pub fallback: Option<SortFallback>,
}
impl TryFrom<ActionCow> for SortOpt {
type Error = anyhow::Error;
fn try_from(a: ActionCow) -> Result<Self, Self::Error> {
Ok(Self {
by: a.first().ok().map(str::parse).transpose()?,
reverse: a.get("reverse").ok(),
dir_first: a.get("dir-first").ok(),
sensitive: a.get("sensitive").ok(),
translit: a.get("translit").ok(),
fallback: a.get("fallback").ok().map(str::parse).transpose()?,
})
}
}
impl FromLua for SortOpt {
fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> { lua.from_value(value) }
}
impl IntoLua for SortOpt {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> { lua.to_value_with(&self, SER_OPT) }
}
// --- State
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SortBoolState {
#[default]
None,
On,
Off,
Toggle,
}
impl serde::Serialize for SortBoolState {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::On => serializer.serialize_bool(true),
Self::Off => serializer.serialize_bool(false),
Self::Toggle => serializer.serialize_str("toggle"),
Self::None => serializer.serialize_str("none"),
}
}
}
impl FromStr for SortBoolState {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"yes" => Ok(Self::On),
"no" => Ok(Self::Off),
"toggle" => Ok(Self::Toggle),
_ => bail!("unknown sort bool state: {s:?}"),
}
}
}
impl<'de> serde::Deserialize<'de> for SortBoolState {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::{self, Visitor};
struct V;
impl<'de> Visitor<'de> for V {
type Value = SortBoolState;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(r#"a boolean or one of "yes", "no", "toggle""#)
}
fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
Ok(if v { SortBoolState::On } else { SortBoolState::Off })
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
v.parse().map_err(de::Error::custom)
}
fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
self.visit_str(&v)
}
}
deserializer.deserialize_any(V)
}
}
impl SortBoolState {
pub fn bool(self, old: bool) -> bool {
match self {
Self::None => old,
Self::On => true,
Self::Off => false,
Self::Toggle => !old,
}
}
}
impl TryFrom<&Data> for SortBoolState {
type Error = anyhow::Error;
fn try_from(value: &Data) -> Result<Self, Self::Error> {
match value {
Data::Boolean(true) => Ok(Self::On),
Data::Boolean(false) => Ok(Self::Off),
Data::String(s) => s.parse(),
_ => bail!("not a valid bool state"),
}
}
}