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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::collections::HashMap;

use mlua::{AnyUserData, ExternalError, IntoLua, Lua, Value, Variadic};
use yazi_shared::OrderedFloat;

use crate::elements::Renderable;

pub fn cast_to_renderable(ud: AnyUserData) -> Option<Box<dyn Renderable + Send>> {
	if let Ok(c) = ud.take::<crate::elements::Paragraph>() {
		Some(Box::new(c))
	} else if let Ok(c) = ud.take::<crate::elements::List>() {
		Some(Box::new(c))
	} else if let Ok(c) = ud.take::<crate::elements::Bar>() {
		Some(Box::new(c))
	} else if let Ok(c) = ud.take::<crate::elements::Clear>() {
		Some(Box::new(c))
	} else if let Ok(c) = ud.take::<crate::elements::Border>() {
		Some(Box::new(c))
	} else if let Ok(c) = ud.take::<crate::elements::Gauge>() {
		Some(Box::new(c))
	} else {
		None
	}
}

#[derive(Debug)]
pub enum ValueSendable {
	Nil,
	Boolean(bool),
	Integer(i64),
	Number(f64),
	String(Vec<u8>),
	Table(HashMap<ValueSendableKey, ValueSendable>),
}

impl<'a> TryFrom<Value<'a>> for ValueSendable {
	type Error = mlua::Error;

	fn try_from(value: Value) -> Result<Self, Self::Error> {
		Ok(match value {
			Value::Nil => ValueSendable::Nil,
			Value::Boolean(b) => ValueSendable::Boolean(b),
			Value::LightUserData(_) => Err("light userdata is not supported".into_lua_err())?,
			Value::Integer(n) => ValueSendable::Integer(n),
			Value::Number(n) => ValueSendable::Number(n),
			Value::String(s) => ValueSendable::String(s.as_bytes().to_vec()),
			Value::Table(t) => {
				let mut map = HashMap::with_capacity(t.len().map(|l| l as usize)?);
				for result in t.pairs::<Value, Value>() {
					let (k, v) = result?;
					map.insert(ValueSendable::try_from(k)?.try_into()?, v.try_into()?);
				}
				ValueSendable::Table(map)
			}
			Value::Function(_) => Err("function is not supported".into_lua_err())?,
			Value::Thread(_) => Err("thread is not supported".into_lua_err())?,
			Value::UserData(_) => Err("userdata is not supported".into_lua_err())?,
			Value::Error(_) => Err("error is not supported".into_lua_err())?,
		})
	}
}

impl<'lua> IntoLua<'lua> for ValueSendable {
	fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
		match self {
			ValueSendable::Nil => Ok(Value::Nil),
			ValueSendable::Boolean(b) => Ok(Value::Boolean(b)),
			ValueSendable::Integer(n) => Ok(Value::Integer(n)),
			ValueSendable::Number(n) => Ok(Value::Number(n)),
			ValueSendable::String(s) => Ok(Value::String(lua.create_string(s)?)),
			ValueSendable::Table(t) => {
				let seq_len = t.keys().filter(|&k| !k.is_numeric()).count();
				let table = lua.create_table_with_capacity(seq_len, t.len() - seq_len)?;
				for (k, v) in t {
					table.raw_set(k, v)?;
				}
				Ok(Value::Table(table))
			}
		}
	}
}

impl ValueSendable {
	pub fn try_from_variadic(values: Variadic<Value>) -> mlua::Result<Vec<ValueSendable>> {
		let mut vec = Vec::with_capacity(values.len());
		for value in values {
			vec.push(ValueSendable::try_from(value)?);
		}
		Ok(vec)
	}

	pub fn into_table_string(self) -> HashMap<String, String> {
		let ValueSendable::Table(table) = self else {
			return Default::default();
		};

		let mut map = HashMap::with_capacity(table.len());
		for pair in table {
			let (ValueSendableKey::String(k), ValueSendable::String(v)) = pair else {
				continue;
			};
			if let (Ok(k), Ok(v)) = (String::from_utf8(k), String::from_utf8(v)) {
				map.insert(k, v);
			}
		}
		map
	}
}

#[derive(Debug, Hash, PartialEq, Eq)]
pub enum ValueSendableKey {
	Nil,
	Boolean(bool),
	Integer(i64),
	Number(OrderedFloat),
	String(Vec<u8>),
}

impl ValueSendableKey {
	#[inline]
	fn is_numeric(&self) -> bool { matches!(self, Self::Integer(_) | Self::Number(_)) }
}

impl TryInto<ValueSendableKey> for ValueSendable {
	type Error = mlua::Error;

	fn try_into(self) -> Result<ValueSendableKey, Self::Error> {
		Ok(match self {
			ValueSendable::Nil => ValueSendableKey::Nil,
			ValueSendable::Boolean(b) => ValueSendableKey::Boolean(b),
			ValueSendable::Integer(n) => ValueSendableKey::Integer(n),
			ValueSendable::Number(n) => ValueSendableKey::Number(OrderedFloat::new(n)),
			ValueSendable::String(s) => ValueSendableKey::String(s),
			ValueSendable::Table(_) => Err("table is not supported".into_lua_err())?,
		})
	}
}

impl<'lua> IntoLua<'lua> for ValueSendableKey {
	fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
		match self {
			ValueSendableKey::Nil => Ok(Value::Nil),
			ValueSendableKey::Boolean(b) => Ok(Value::Boolean(b)),
			ValueSendableKey::Integer(n) => Ok(Value::Integer(n)),
			ValueSendableKey::Number(n) => Ok(Value::Number(n.get())),
			ValueSendableKey::String(s) => Ok(Value::String(lua.create_string(s)?)),
		}
	}
}