diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:25:25 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:25:25 +0200 |
| commit | 5ff507db2f6667a0305e2382037710f6082d75da (patch) | |
| tree | 94954bc8547e62ac8cbdbfd327d21712deb9fef2 /src/sheet/loader.rs | |
| parent | 63cfcbe7a7745b276de58ec92e0141b958c44feb (diff) | |
imlement native csv loader
Diffstat (limited to 'src/sheet/loader.rs')
| -rw-r--r-- | src/sheet/loader.rs | 48 |
1 files changed, 0 insertions, 48 deletions
diff --git a/src/sheet/loader.rs b/src/sheet/loader.rs deleted file mode 100644 index 35b6da2..0000000 --- a/src/sheet/loader.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::{collections::HashMap, path::Path, ptr::addr_of_mut}; - -use mlua::{Lua, UserData}; -use once_cell::sync::Lazy; - -use crate::lua::{ownedfunction::OwnedFunction, runnable::Runnable}; - -use super::{register::SheetId, tablized::Tablized}; - -#[derive(Default)] -pub struct Loader { - loaders: HashMap<String, Box<dyn Runnable<String, Tablized>>>, -} - -impl Loader { - pub fn new() -> Self { - Self { - loaders: HashMap::new(), - } - } - - pub fn load_sheet(&self, path: String, lua: &Lua) -> Option<SheetId> { - let p = Path::new(&path); - let loader = self.loaders.get(p.extension()?.to_str()?)?; - match loader.run(path, lua) { - Ok(table) => table.to_sheet(lua).ok(), - Err(_) => None, - } - } - - pub fn add(&mut self, extension: String, func: Box<dyn Runnable<String, Tablized>>) { - self.loaders.insert(extension, func); - } - - pub fn get() -> &'static mut Self { - static mut GLOBAL_LOADER: Lazy<Loader> = Lazy::new(|| Loader::new()); - unsafe { &mut *addr_of_mut!(GLOBAL_LOADER) } - } -} - -impl UserData for Loader { - fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { - methods.add_function("add", |_, (extension, func): (String, OwnedFunction)| { - Loader::get().add(extension, Box::new(func)); - Ok(()) - }); - } -} |