From ae428997270b9c11e3f0307dbe08e1239c85793b Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 2 Aug 2023 12:45:56 +0200 Subject: [PATCH] feat: with iocat Signed-off-by: kjuulh --- Cargo.toml | 10 ++--- src/io_cat.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/types.rs | 2 +- tests/tests.rs | 1 - 5 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 src/io_cat.rs diff --git a/Cargo.toml b/Cargo.toml index ae599d3..b926d7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,17 +2,13 @@ name = "rlua-searcher" version = "0.1.0" edition = "2021" -authors = ["Andy Weidenbaum "] +authors = ["Andy Weidenbaum ", "Kasper J. Hermansen "] license = "MIT OR Apache-2.0" keywords = ["lua"] -homepage = "https://git.sr.ht/~ioiojo/rlua-searcher" -repository = "https://git.sr.ht/~ioiojo/rlua-searcher" +homepage = "https://git.front.kjuulh.io/kjuulh/rlua-searcher" +repository = "https://git.front.kjuulh.io/kjuulh/rlua-searcher" readme = "README.md" description = "Require Lua modules by name" [dependencies] rlua = "0.19.5" - -[dependencies.io-cat] -git = "https://git.sr.ht/~ioiojo/io-cat" -rev = "74167240f6a284505a1f0a334432352878a7ba99" diff --git a/src/io_cat.rs b/src/io_cat.rs new file mode 100644 index 0000000..f981cae --- /dev/null +++ b/src/io_cat.rs @@ -0,0 +1,100 @@ +use std::borrow::Cow; +use std::collections::HashMap; +use std::fs::File; +use std::io; +use std::io::{BufReader, Cursor, Read}; +use std::path::{Path, PathBuf}; +use std::string::String; + +pub type CatBox = Box; +pub type CatMap = HashMap; + +pub trait Cat { + fn cat(&self) -> io::Result; +} + +impl Cat for PathBuf { + fn cat(&self) -> io::Result { + self.as_path().cat() + } +} + +impl Cat for &Path { + fn cat(&self) -> io::Result { + let mut input = File::open(self)?; + read_to_string(&mut input) + } +} + +impl Cat for Cow<'_, Path> { + fn cat(&self) -> io::Result { + self.as_ref().cat() + } +} + +impl Cat for String { + fn cat(&self) -> io::Result { + self.as_str().cat() + } +} + +impl Cat for &str { + fn cat(&self) -> io::Result { + let mut input = Cursor::new(self); + read_to_string(&mut input) + } +} + +impl Cat for Cow<'_, str> { + fn cat(&self) -> io::Result { + self.as_ref().cat() + } +} + +fn read_to_string(input: &mut R) -> io::Result +where + R: Read, +{ + let mut text = String::new(); + let mut reader = BufReader::new(input); + reader.read_to_string(&mut text)?; + Ok(text) +} + +#[cfg(test)] +mod tests { + use std::borrow::Cow; + use std::env; + use std::path::Path; + + use super::CatMap; + + #[test] + fn it_works() { + const ENV_VAR_OS_CARGO_MANIFEST_DIR: &str = + "Unexpectedly could not read `CARGO_MANIFEST_DIR` environment variable"; + let mut cat_map: CatMap> = CatMap::new(); + cat_map.insert(Cow::from("Apr"), Box::new("Showers")); + cat_map.insert( + Cow::from("May"), + Box::new( + Path::new(&env::var_os("CARGO_MANIFEST_DIR").expect(ENV_VAR_OS_CARGO_MANIFEST_DIR)) + .join("testdata") + .join("may.txt"), + ), + ); + assert_eq!( + cat_map.get(&Cow::from("Apr")).unwrap().cat().unwrap(), + String::from("Showers") + ); + assert_eq!( + cat_map + .get(&Cow::from("May")) + .unwrap() + .cat() + .unwrap() + .trim_end(), + String::from("Flowers") + ); + } +} diff --git a/src/lib.rs b/src/lib.rs index bee63c2..8ea23ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod error; +mod io_cat; mod searcher; mod types; diff --git a/src/types.rs b/src/types.rs index 15d23dc..9ffbadc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,5 +3,5 @@ use std::result; use crate::error::Error; -pub(crate) type CatMap = io_cat::CatMap>; +pub(crate) type CatMap = crate::io_cat::CatMap>; pub type Result = result::Result; diff --git a/tests/tests.rs b/tests/tests.rs index d4513b4..384d42f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,4 +1,3 @@ -use io_cat::CatMap; use rlua::{Context, Function, Lua, Table, UserData, UserDataMethods, Value}; use rlua_searcher::{AddSearcher, Result}; use std::borrow::Cow;