mirror of
https://github.com/lune-org/lune.git
synced 2025-04-19 03:13:54 +01:00
Merge branch 'main' into feature/process-stream
This commit is contained in:
commit
db3893c4b9
4 changed files with 19 additions and 25 deletions
25
crates/lune-std-fs/src/lib.rs
Normal file → Executable file
25
crates/lune-std-fs/src/lib.rs
Normal file → Executable file
|
@ -1,7 +1,7 @@
|
||||||
#![allow(clippy::cargo_common_metadata)]
|
#![allow(clippy::cargo_common_metadata)]
|
||||||
|
|
||||||
use std::io::ErrorKind as IoErrorKind;
|
use std::io::ErrorKind as IoErrorKind;
|
||||||
use std::path::{PathBuf, MAIN_SEPARATOR};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use bstr::{BString, ByteSlice};
|
use bstr::{BString, ByteSlice};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
@ -50,29 +50,16 @@ async fn fs_read_dir(_: &Lua, path: String) -> LuaResult<Vec<String>> {
|
||||||
let mut dir_strings = Vec::new();
|
let mut dir_strings = Vec::new();
|
||||||
let mut dir = fs::read_dir(&path).await.into_lua_err()?;
|
let mut dir = fs::read_dir(&path).await.into_lua_err()?;
|
||||||
while let Some(dir_entry) = dir.next_entry().await.into_lua_err()? {
|
while let Some(dir_entry) = dir.next_entry().await.into_lua_err()? {
|
||||||
if let Some(dir_path_str) = dir_entry.path().to_str() {
|
if let Some(dir_name_str) = dir_entry.file_name().to_str() {
|
||||||
dir_strings.push(dir_path_str.to_owned());
|
dir_strings.push(dir_name_str.to_owned());
|
||||||
} else {
|
} else {
|
||||||
return Err(LuaError::RuntimeError(format!(
|
return Err(LuaError::RuntimeError(format!(
|
||||||
"File path could not be converted into a string: '{}'",
|
"File name could not be converted into a string: '{}'",
|
||||||
dir_entry.path().display()
|
dir_entry.file_name().to_string_lossy()
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut dir_string_prefix = path;
|
Ok(dir_strings)
|
||||||
if !dir_string_prefix.ends_with(MAIN_SEPARATOR) {
|
|
||||||
dir_string_prefix.push(MAIN_SEPARATOR);
|
|
||||||
}
|
|
||||||
let dir_strings_no_prefix = dir_strings
|
|
||||||
.iter()
|
|
||||||
.map(|inner_path| {
|
|
||||||
inner_path
|
|
||||||
.trim()
|
|
||||||
.trim_start_matches(&dir_string_prefix)
|
|
||||||
.to_owned()
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
Ok(dir_strings_no_prefix)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fs_write_file(_: &Lua, (path, contents): (String, BString)) -> LuaResult<()> {
|
async fn fs_write_file(_: &Lua, (path, contents): (String, BString)) -> LuaResult<()> {
|
||||||
|
|
|
@ -13,6 +13,7 @@ use async_compression::{
|
||||||
BrotliDecoder, BrotliEncoder, GzipDecoder, GzipEncoder, ZlibDecoder, ZlibEncoder,
|
BrotliDecoder, BrotliEncoder, GzipDecoder, GzipEncoder, ZlibDecoder, ZlibEncoder,
|
||||||
},
|
},
|
||||||
Level::Best as CompressionQuality,
|
Level::Best as CompressionQuality,
|
||||||
|
Level::Precise as PreciseCompressionQuality,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,6 +120,7 @@ impl<'lua> FromLua<'lua> for CompressDecompressFormat {
|
||||||
pub async fn compress<'lua>(
|
pub async fn compress<'lua>(
|
||||||
source: impl AsRef<[u8]>,
|
source: impl AsRef<[u8]>,
|
||||||
format: CompressDecompressFormat,
|
format: CompressDecompressFormat,
|
||||||
|
level: Option<i32>,
|
||||||
) -> LuaResult<Vec<u8>> {
|
) -> LuaResult<Vec<u8>> {
|
||||||
if let CompressDecompressFormat::LZ4 = format {
|
if let CompressDecompressFormat::LZ4 = format {
|
||||||
let source = source.as_ref().to_vec();
|
let source = source.as_ref().to_vec();
|
||||||
|
@ -130,18 +132,22 @@ pub async fn compress<'lua>(
|
||||||
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
let reader = BufReader::new(source.as_ref());
|
let reader = BufReader::new(source.as_ref());
|
||||||
|
let compression_quality = match level {
|
||||||
|
Some(l) => PreciseCompressionQuality(l),
|
||||||
|
None => CompressionQuality,
|
||||||
|
};
|
||||||
|
|
||||||
match format {
|
match format {
|
||||||
CompressDecompressFormat::Brotli => {
|
CompressDecompressFormat::Brotli => {
|
||||||
let mut encoder = BrotliEncoder::with_quality(reader, CompressionQuality);
|
let mut encoder = BrotliEncoder::with_quality(reader, compression_quality);
|
||||||
copy(&mut encoder, &mut bytes).await?;
|
copy(&mut encoder, &mut bytes).await?;
|
||||||
}
|
}
|
||||||
CompressDecompressFormat::GZip => {
|
CompressDecompressFormat::GZip => {
|
||||||
let mut encoder = GzipEncoder::with_quality(reader, CompressionQuality);
|
let mut encoder = GzipEncoder::with_quality(reader, compression_quality);
|
||||||
copy(&mut encoder, &mut bytes).await?;
|
copy(&mut encoder, &mut bytes).await?;
|
||||||
}
|
}
|
||||||
CompressDecompressFormat::ZLib => {
|
CompressDecompressFormat::ZLib => {
|
||||||
let mut encoder = ZlibEncoder::with_quality(reader, CompressionQuality);
|
let mut encoder = ZlibEncoder::with_quality(reader, compression_quality);
|
||||||
copy(&mut encoder, &mut bytes).await?;
|
copy(&mut encoder, &mut bytes).await?;
|
||||||
}
|
}
|
||||||
CompressDecompressFormat::LZ4 => unreachable!(),
|
CompressDecompressFormat::LZ4 => unreachable!(),
|
||||||
|
|
|
@ -46,9 +46,9 @@ fn serde_decode(lua: &Lua, (format, bs): (EncodeDecodeFormat, BString)) -> LuaRe
|
||||||
|
|
||||||
async fn serde_compress(
|
async fn serde_compress(
|
||||||
lua: &Lua,
|
lua: &Lua,
|
||||||
(format, bs): (CompressDecompressFormat, BString),
|
(format, bs, level): (CompressDecompressFormat, BString, Option<i32>),
|
||||||
) -> LuaResult<LuaString> {
|
) -> LuaResult<LuaString> {
|
||||||
let bytes = compress(bs, format).await?;
|
let bytes = compress(bs, format, level).await?;
|
||||||
lua.create_string(bytes)
|
lua.create_string(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,9 +136,10 @@ end
|
||||||
|
|
||||||
@param format The format to use
|
@param format The format to use
|
||||||
@param s The string to compress
|
@param s The string to compress
|
||||||
|
@param level The compression level to use, clamped to the format's limits. The best compression level is used by default
|
||||||
@return The compressed string
|
@return The compressed string
|
||||||
]=]
|
]=]
|
||||||
function serde.compress(format: CompressDecompressFormat, s: buffer | string): string
|
function serde.compress(format: CompressDecompressFormat, s: buffer | string, level: number?): string
|
||||||
return nil :: any
|
return nil :: any
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue