From 510ef8e178929cf5e0c7fd5a5120fecf5f1b79f2 Mon Sep 17 00:00:00 2001 From: delta Date: Tue, 5 Mar 2024 14:48:59 +0100 Subject: idk anymore --- .config/awesome/quarrel/native/src/util.rs | 101 +++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 .config/awesome/quarrel/native/src/util.rs (limited to '.config/awesome/quarrel/native/src/util.rs') diff --git a/.config/awesome/quarrel/native/src/util.rs b/.config/awesome/quarrel/native/src/util.rs new file mode 100644 index 0000000..85a2574 --- /dev/null +++ b/.config/awesome/quarrel/native/src/util.rs @@ -0,0 +1,101 @@ +use std::{ + cell::RefCell, + fs::File, + fs::OpenOptions, + io::{ + Read, + Seek, + Write + }, + rc::Rc, + str::FromStr, +}; + +use html_escape::decode_html_entities_to_string; +use mlua::prelude::*; +use serde::Serialize; + +pub fn decode_html(_: &Lua, string: String) -> LuaResult { + let mut output = String::new(); + decode_html_entities_to_string(string, &mut output); + Ok(output) +} + +enum ReadMode { + Line, + Number, + All, +} + +impl FromStr for ReadMode { + type Err = (); + + fn from_str(value: &str) -> Result { + Ok(match value { + "l" => Self::Line, + "n" => Self::Number, + _ => Self::All, + }) + } +} + +#[derive(Serialize)] +#[serde(untagged)] +enum StringOrNumber { + String(String), + Number(u64), +} + +pub struct FileHandle(Rc>); + +impl LuaUserData for FileHandle { + fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method("read", |lua, this: &FileHandle, mode: String| { + let content = this.read()?; + Ok(lua.to_value(&match ReadMode::from_str(&mode).unwrap() { + ReadMode::Line => StringOrNumber::String( + content + .lines() + .next() + .map_or_else(String::new, |slice| slice.trim().to_owned()), + ), + ReadMode::Number => StringOrNumber::Number( + content.trim().parse::().map_err(LuaError::external)?, + ), + ReadMode::All => StringOrNumber::String(content), + })) + }); + methods.add_method("write", |_, this: &FileHandle, content: String| { + this.write(content.as_bytes()) + }); + methods.add_method("lines", |_, this: &FileHandle, (): ()| { + Ok(this + .read()? + .lines() + .map(ToOwned::to_owned) + .collect::>()) + }); + methods.add_method("rewind", |_, this: &FileHandle, (): ()| this.rewind()); + } +} + +impl FileHandle { + pub fn new(_: &Lua, path: String) -> LuaResult { + Ok(Self(Rc::new(RefCell::new(File::open(path)?)))) + // Ok(Self(Rc::new(RefCell::new(OpenOptions::new().write(true).read(true).open(path)?)))) + } + + fn read(&self) -> LuaResult { + let mut content = String::new(); + self.0.borrow_mut().read_to_string(&mut content)?; + Ok(content) + } + + fn rewind(&self) -> LuaResult<()> { + self.0.borrow_mut().rewind().map_err(LuaError::external) + } + + fn write(&self, buf: &[u8]) -> LuaResult { + self.0.borrow_mut().write(buf).map_err(LuaError::external) + } +} -- cgit v1.2.3