Use more descriptive module name instead of 'utils'

This commit is contained in:
Filip Tibell 2023-02-21 11:55:30 +01:00
parent 0737c3254e
commit 36a3bd2113
No known key found for this signature in database
20 changed files with 47 additions and 57 deletions

View file

@ -3,7 +3,7 @@ use std::path::{PathBuf, MAIN_SEPARATOR};
use mlua::prelude::*; use mlua::prelude::*;
use tokio::fs; use tokio::fs;
use crate::utils::table::TableBuilder; use crate::lua::table::TableBuilder;
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> { pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
TableBuilder::new(lua)? TableBuilder::new(lua)?

View file

@ -6,15 +6,13 @@ use console::style;
use hyper::Server; use hyper::Server;
use tokio::{sync::mpsc, task}; use tokio::{sync::mpsc, task};
use crate::{ use crate::lua::{
lua::{ net::{
net::{ NetClient, NetClientBuilder, NetLocalExec, NetService, NetWebSocket, RequestConfig,
NetClient, NetClientBuilder, NetLocalExec, NetService, NetWebSocket, RequestConfig, ServeConfig,
ServeConfig,
},
task::{TaskScheduler, TaskSchedulerAsyncExt},
}, },
utils::{net::get_request_user_agent_header, table::TableBuilder}, table::TableBuilder,
task::{TaskScheduler, TaskSchedulerAsyncExt},
}; };
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> { pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
@ -22,7 +20,7 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
// web requests and store it in the lua registry, // web requests and store it in the lua registry,
// allowing us to reuse headers and internal structs // allowing us to reuse headers and internal structs
let client = NetClientBuilder::new() let client = NetClientBuilder::new()
.headers(&[("User-Agent", get_request_user_agent_header())])? .headers(&[("User-Agent", create_user_agent_header())])?
.build()?; .build()?;
lua.set_named_registry_value("net.client", client)?; lua.set_named_registry_value("net.client", client)?;
// Create the global table for net // Create the global table for net
@ -35,6 +33,15 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.build_readonly() .build_readonly()
} }
fn create_user_agent_header() -> String {
let (github_owner, github_repo) = env!("CARGO_PKG_REPOSITORY")
.strip_prefix("https://github.com/")
.unwrap()
.split_once('/')
.unwrap();
format!("{github_owner}-{github_repo}-cli")
}
fn net_json_encode(_: &'static Lua, (val, pretty): (LuaValue, Option<bool>)) -> LuaResult<String> { fn net_json_encode(_: &'static Lua, (val, pretty): (LuaValue, Option<bool>)) -> LuaResult<String> {
if let Some(true) = pretty { if let Some(true) = pretty {
serde_json::to_string_pretty(&val).map_err(LuaError::external) serde_json::to_string_pretty(&val).map_err(LuaError::external)

View file

@ -10,9 +10,8 @@ use mlua::prelude::*;
use os_str_bytes::RawOsString; use os_str_bytes::RawOsString;
use tokio::process::Command; use tokio::process::Command;
use crate::{ use crate::lua::{
lua::task::TaskScheduler, process::pipe_and_inherit_child_process_stdio, table::TableBuilder, task::TaskScheduler,
utils::{process::pipe_and_inherit_child_process_stdio, table::TableBuilder},
}; };
const PROCESS_EXIT_IMPL_LUA: &str = r#" const PROCESS_EXIT_IMPL_LUA: &str = r#"

View file

@ -6,7 +6,7 @@ use std::{
use mlua::prelude::*; use mlua::prelude::*;
use crate::utils::table::TableBuilder; use crate::lua::table::TableBuilder;
const REQUIRE_IMPL_LUA: &str = r#" const REQUIRE_IMPL_LUA: &str = r#"
local source = info(1, "s") local source = info(1, "s")

View file

@ -2,14 +2,14 @@ use blocking::unblock;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select}; use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
use mlua::prelude::*; use mlua::prelude::*;
use crate::{ use crate::lua::{
lua::stdio::{PromptKind, PromptOptions, PromptResult}, stdio::{
utils::{
formatting::{ formatting::{
format_style, pretty_format_multi_value, style_from_color_str, style_from_style_str, format_style, pretty_format_multi_value, style_from_color_str, style_from_style_str,
}, },
table::TableBuilder, prompt::{PromptKind, PromptOptions, PromptResult},
}, },
table::TableBuilder,
}; };
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> { pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {

View file

@ -1,14 +1,12 @@
use mlua::prelude::*; use mlua::prelude::*;
use crate::{ use crate::lua::{
lua::{ async_ext::LuaAsyncExt,
async_ext::LuaAsyncExt, table::TableBuilder,
task::{ task::{
LuaThreadOrFunction, LuaThreadOrTaskReference, TaskKind, TaskReference, TaskScheduler, LuaThreadOrFunction, LuaThreadOrTaskReference, TaskKind, TaskReference, TaskScheduler,
TaskSchedulerScheduleExt, TaskSchedulerScheduleExt,
},
}, },
utils::table::TableBuilder,
}; };
const SPAWN_IMPL_LUA: &str = r#" const SPAWN_IMPL_LUA: &str = r#"

View file

@ -1,8 +1,8 @@
use mlua::prelude::*; use mlua::prelude::*;
use crate::utils::{ use crate::{
formatting::{format_label, pretty_format_multi_value}, lua::stdio::formatting::{format_label, pretty_format_multi_value},
table::TableBuilder, lua::table::TableBuilder,
}; };
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> { pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {

View file

@ -6,16 +6,15 @@ use tokio::task::LocalSet;
pub(crate) mod globals; pub(crate) mod globals;
pub(crate) mod lua; pub(crate) mod lua;
pub(crate) mod utils;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use crate::utils::formatting::pretty_format_luau_error;
pub use globals::LuneGlobal; pub use globals::LuneGlobal;
pub use lua::create_lune_lua; pub use lua::create_lune_lua;
use lua::stdio::formatting::pretty_format_luau_error;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Lune { pub struct Lune {
args: Vec<String>, args: Vec<String>,

View file

@ -2,7 +2,7 @@ use async_trait::async_trait;
use futures_util::Future; use futures_util::Future;
use mlua::prelude::*; use mlua::prelude::*;
use crate::{lua::task::TaskScheduler, utils::table::TableBuilder}; use crate::{lua::table::TableBuilder, lua::task::TaskScheduler};
use super::task::TaskSchedulerAsyncExt; use super::task::TaskSchedulerAsyncExt;

View file

@ -2,7 +2,9 @@ mod create;
pub mod async_ext; pub mod async_ext;
pub mod net; pub mod net;
pub mod process;
pub mod stdio; pub mod stdio;
pub mod table;
pub mod task; pub mod task;
pub use create::create as create_lune_lua; pub use create::create as create_lune_lua;

View file

@ -13,8 +13,8 @@ use hyper_tungstenite::{is_upgrade_request as is_ws_upgrade_request, upgrade as
use tokio::task; use tokio::task;
use crate::{ use crate::{
lua::table::TableBuilder,
lua::task::{TaskScheduler, TaskSchedulerAsyncExt, TaskSchedulerScheduleExt}, lua::task::{TaskScheduler, TaskSchedulerAsyncExt, TaskSchedulerScheduleExt},
utils::table::TableBuilder,
}; };
use super::{NetServeResponse, NetWebSocket}; use super::{NetServeResponse, NetWebSocket};

View file

@ -10,7 +10,7 @@ use tokio::{
sync::Mutex, sync::Mutex,
}; };
use crate::utils::table::TableBuilder; use crate::lua::table::TableBuilder;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct NetWebSocket<T> { pub struct NetWebSocket<T> {

View file

@ -3,7 +3,8 @@ use std::process::ExitStatus;
use mlua::prelude::*; use mlua::prelude::*;
use tokio::{io, process::Child, task}; use tokio::{io, process::Child, task};
use crate::utils::futures::AsyncTeeWriter; mod tee_writer;
use tee_writer::AsyncTeeWriter;
pub async fn pipe_and_inherit_child_process_stdio( pub async fn pipe_and_inherit_child_process_stdio(
mut child: Child, mut child: Child,

View file

@ -1,3 +1,2 @@
mod prompt; pub mod formatting;
pub mod prompt;
pub use prompt::{PromptKind, PromptOptions, PromptResult};

View file

@ -0,0 +1,3 @@
mod builder;
pub use builder::TableBuilder;

View file

@ -1,5 +0,0 @@
pub mod formatting;
pub mod futures;
pub mod net;
pub mod process;
pub mod table;

View file

@ -1,13 +0,0 @@
pub fn get_github_owner_and_repo() -> (String, String) {
let (github_owner, github_repo) = env!("CARGO_PKG_REPOSITORY")
.strip_prefix("https://github.com/")
.unwrap()
.split_once('/')
.unwrap();
(github_owner.to_owned(), github_repo.to_owned())
}
pub fn get_request_user_agent_header() -> String {
let (github_owner, github_repo) = get_github_owner_and_repo();
format!("{github_owner}-{github_repo}-cli")
}