feat: with iocat

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-02 12:45:56 +02:00
parent 2b29a9d0e8
commit ae42899727
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
5 changed files with 105 additions and 9 deletions

View File

@ -2,17 +2,13 @@
name = "rlua-searcher"
version = "0.1.0"
edition = "2021"
authors = ["Andy Weidenbaum <atweiden@ioiojo.com>"]
authors = ["Andy Weidenbaum <atweiden@ioiojo.com>", "Kasper J. Hermansen <contact@kjuulh.io>"]
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"

100
src/io_cat.rs Normal file
View File

@ -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<dyn Cat + Send + Sync>;
pub type CatMap<K> = HashMap<K, CatBox>;
pub trait Cat {
fn cat(&self) -> io::Result<String>;
}
impl Cat for PathBuf {
fn cat(&self) -> io::Result<String> {
self.as_path().cat()
}
}
impl Cat for &Path {
fn cat(&self) -> io::Result<String> {
let mut input = File::open(self)?;
read_to_string(&mut input)
}
}
impl Cat for Cow<'_, Path> {
fn cat(&self) -> io::Result<String> {
self.as_ref().cat()
}
}
impl Cat for String {
fn cat(&self) -> io::Result<String> {
self.as_str().cat()
}
}
impl Cat for &str {
fn cat(&self) -> io::Result<String> {
let mut input = Cursor::new(self);
read_to_string(&mut input)
}
}
impl Cat for Cow<'_, str> {
fn cat(&self) -> io::Result<String> {
self.as_ref().cat()
}
}
fn read_to_string<R>(input: &mut R) -> io::Result<String>
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<Cow<'static, str>> = 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")
);
}
}

View File

@ -1,4 +1,5 @@
mod error;
mod io_cat;
mod searcher;
mod types;

View File

@ -3,5 +3,5 @@ use std::result;
use crate::error::Error;
pub(crate) type CatMap = io_cat::CatMap<Cow<'static, str>>;
pub(crate) type CatMap = crate::io_cat::CatMap<Cow<'static, str>>;
pub type Result<A> = result::Result<A, Error>;

View File

@ -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;