From 36a3bd2113ca12ec9e7c9557277f9466a034b8f6 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Tue, 21 Feb 2023 11:55:30 +0100 Subject: [PATCH] Use more descriptive module name instead of 'utils' --- packages/lib/src/globals/fs.rs | 2 +- packages/lib/src/globals/net.rs | 25 ++++++++++++------- packages/lib/src/globals/process.rs | 5 ++-- packages/lib/src/globals/require.rs | 2 +- packages/lib/src/globals/stdio.rs | 8 +++--- packages/lib/src/globals/task.rs | 14 +++++------ packages/lib/src/globals/top_level.rs | 6 ++--- packages/lib/src/lib.rs | 5 ++-- packages/lib/src/lua/async_ext.rs | 2 +- packages/lib/src/lua/mod.rs | 2 ++ packages/lib/src/lua/net/server.rs | 2 +- packages/lib/src/lua/net/websocket.rs | 2 +- .../{utils/process.rs => lua/process/mod.rs} | 3 ++- .../futures.rs => lua/process/tee_writer.rs} | 0 .../src/{utils => lua/stdio}/formatting.rs | 0 packages/lib/src/lua/stdio/mod.rs | 5 ++-- .../{utils/table.rs => lua/table/builder.rs} | 0 packages/lib/src/lua/table/mod.rs | 3 +++ packages/lib/src/utils/mod.rs | 5 ---- packages/lib/src/utils/net.rs | 13 ---------- 20 files changed, 47 insertions(+), 57 deletions(-) rename packages/lib/src/{utils/process.rs => lua/process/mod.rs} (96%) rename packages/lib/src/{utils/futures.rs => lua/process/tee_writer.rs} (100%) rename packages/lib/src/{utils => lua/stdio}/formatting.rs (100%) rename packages/lib/src/{utils/table.rs => lua/table/builder.rs} (100%) create mode 100644 packages/lib/src/lua/table/mod.rs delete mode 100644 packages/lib/src/utils/mod.rs delete mode 100644 packages/lib/src/utils/net.rs diff --git a/packages/lib/src/globals/fs.rs b/packages/lib/src/globals/fs.rs index 11a5af3..abb426d 100644 --- a/packages/lib/src/globals/fs.rs +++ b/packages/lib/src/globals/fs.rs @@ -3,7 +3,7 @@ use std::path::{PathBuf, MAIN_SEPARATOR}; use mlua::prelude::*; use tokio::fs; -use crate::utils::table::TableBuilder; +use crate::lua::table::TableBuilder; pub fn create(lua: &'static Lua) -> LuaResult { TableBuilder::new(lua)? diff --git a/packages/lib/src/globals/net.rs b/packages/lib/src/globals/net.rs index 63f1b7b..a3e68d7 100644 --- a/packages/lib/src/globals/net.rs +++ b/packages/lib/src/globals/net.rs @@ -6,15 +6,13 @@ use console::style; use hyper::Server; use tokio::{sync::mpsc, task}; -use crate::{ - lua::{ - net::{ - NetClient, NetClientBuilder, NetLocalExec, NetService, NetWebSocket, RequestConfig, - ServeConfig, - }, - task::{TaskScheduler, TaskSchedulerAsyncExt}, +use crate::lua::{ + net::{ + NetClient, NetClientBuilder, NetLocalExec, NetService, NetWebSocket, RequestConfig, + ServeConfig, }, - utils::{net::get_request_user_agent_header, table::TableBuilder}, + table::TableBuilder, + task::{TaskScheduler, TaskSchedulerAsyncExt}, }; pub fn create(lua: &'static Lua) -> LuaResult { @@ -22,7 +20,7 @@ pub fn create(lua: &'static Lua) -> LuaResult { // web requests and store it in the lua registry, // allowing us to reuse headers and internal structs let client = NetClientBuilder::new() - .headers(&[("User-Agent", get_request_user_agent_header())])? + .headers(&[("User-Agent", create_user_agent_header())])? .build()?; lua.set_named_registry_value("net.client", client)?; // Create the global table for net @@ -35,6 +33,15 @@ pub fn create(lua: &'static Lua) -> LuaResult { .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)) -> LuaResult { if let Some(true) = pretty { serde_json::to_string_pretty(&val).map_err(LuaError::external) diff --git a/packages/lib/src/globals/process.rs b/packages/lib/src/globals/process.rs index 7b4a218..0e9c16b 100644 --- a/packages/lib/src/globals/process.rs +++ b/packages/lib/src/globals/process.rs @@ -10,9 +10,8 @@ use mlua::prelude::*; use os_str_bytes::RawOsString; use tokio::process::Command; -use crate::{ - lua::task::TaskScheduler, - utils::{process::pipe_and_inherit_child_process_stdio, table::TableBuilder}, +use crate::lua::{ + process::pipe_and_inherit_child_process_stdio, table::TableBuilder, task::TaskScheduler, }; const PROCESS_EXIT_IMPL_LUA: &str = r#" diff --git a/packages/lib/src/globals/require.rs b/packages/lib/src/globals/require.rs index 33f5843..5d7d9fe 100644 --- a/packages/lib/src/globals/require.rs +++ b/packages/lib/src/globals/require.rs @@ -6,7 +6,7 @@ use std::{ use mlua::prelude::*; -use crate::utils::table::TableBuilder; +use crate::lua::table::TableBuilder; const REQUIRE_IMPL_LUA: &str = r#" local source = info(1, "s") diff --git a/packages/lib/src/globals/stdio.rs b/packages/lib/src/globals/stdio.rs index 927cc70..6c517f3 100644 --- a/packages/lib/src/globals/stdio.rs +++ b/packages/lib/src/globals/stdio.rs @@ -2,14 +2,14 @@ use blocking::unblock; use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select}; use mlua::prelude::*; -use crate::{ - lua::stdio::{PromptKind, PromptOptions, PromptResult}, - utils::{ +use crate::lua::{ + stdio::{ formatting::{ 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 { diff --git a/packages/lib/src/globals/task.rs b/packages/lib/src/globals/task.rs index f18831f..d0fd4db 100644 --- a/packages/lib/src/globals/task.rs +++ b/packages/lib/src/globals/task.rs @@ -1,14 +1,12 @@ use mlua::prelude::*; -use crate::{ - lua::{ - async_ext::LuaAsyncExt, - task::{ - LuaThreadOrFunction, LuaThreadOrTaskReference, TaskKind, TaskReference, TaskScheduler, - TaskSchedulerScheduleExt, - }, +use crate::lua::{ + async_ext::LuaAsyncExt, + table::TableBuilder, + task::{ + LuaThreadOrFunction, LuaThreadOrTaskReference, TaskKind, TaskReference, TaskScheduler, + TaskSchedulerScheduleExt, }, - utils::table::TableBuilder, }; const SPAWN_IMPL_LUA: &str = r#" diff --git a/packages/lib/src/globals/top_level.rs b/packages/lib/src/globals/top_level.rs index 9eda825..b9cf5e3 100644 --- a/packages/lib/src/globals/top_level.rs +++ b/packages/lib/src/globals/top_level.rs @@ -1,8 +1,8 @@ use mlua::prelude::*; -use crate::utils::{ - formatting::{format_label, pretty_format_multi_value}, - table::TableBuilder, +use crate::{ + lua::stdio::formatting::{format_label, pretty_format_multi_value}, + lua::table::TableBuilder, }; pub fn create(lua: &'static Lua) -> LuaResult { diff --git a/packages/lib/src/lib.rs b/packages/lib/src/lib.rs index e25bc17..245561b 100644 --- a/packages/lib/src/lib.rs +++ b/packages/lib/src/lib.rs @@ -6,16 +6,15 @@ use tokio::task::LocalSet; pub(crate) mod globals; pub(crate) mod lua; -pub(crate) mod utils; #[cfg(test)] mod tests; -use crate::utils::formatting::pretty_format_luau_error; - pub use globals::LuneGlobal; pub use lua::create_lune_lua; +use lua::stdio::formatting::pretty_format_luau_error; + #[derive(Clone, Debug, Default)] pub struct Lune { args: Vec, diff --git a/packages/lib/src/lua/async_ext.rs b/packages/lib/src/lua/async_ext.rs index b15d3d2..720d08e 100644 --- a/packages/lib/src/lua/async_ext.rs +++ b/packages/lib/src/lua/async_ext.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use futures_util::Future; use mlua::prelude::*; -use crate::{lua::task::TaskScheduler, utils::table::TableBuilder}; +use crate::{lua::table::TableBuilder, lua::task::TaskScheduler}; use super::task::TaskSchedulerAsyncExt; diff --git a/packages/lib/src/lua/mod.rs b/packages/lib/src/lua/mod.rs index 0323230..0e29aaa 100644 --- a/packages/lib/src/lua/mod.rs +++ b/packages/lib/src/lua/mod.rs @@ -2,7 +2,9 @@ mod create; pub mod async_ext; pub mod net; +pub mod process; pub mod stdio; +pub mod table; pub mod task; pub use create::create as create_lune_lua; diff --git a/packages/lib/src/lua/net/server.rs b/packages/lib/src/lua/net/server.rs index de37f99..55380ea 100644 --- a/packages/lib/src/lua/net/server.rs +++ b/packages/lib/src/lua/net/server.rs @@ -13,8 +13,8 @@ use hyper_tungstenite::{is_upgrade_request as is_ws_upgrade_request, upgrade as use tokio::task; use crate::{ + lua::table::TableBuilder, lua::task::{TaskScheduler, TaskSchedulerAsyncExt, TaskSchedulerScheduleExt}, - utils::table::TableBuilder, }; use super::{NetServeResponse, NetWebSocket}; diff --git a/packages/lib/src/lua/net/websocket.rs b/packages/lib/src/lua/net/websocket.rs index 3098c05..c05491d 100644 --- a/packages/lib/src/lua/net/websocket.rs +++ b/packages/lib/src/lua/net/websocket.rs @@ -10,7 +10,7 @@ use tokio::{ sync::Mutex, }; -use crate::utils::table::TableBuilder; +use crate::lua::table::TableBuilder; #[derive(Debug, Clone)] pub struct NetWebSocket { diff --git a/packages/lib/src/utils/process.rs b/packages/lib/src/lua/process/mod.rs similarity index 96% rename from packages/lib/src/utils/process.rs rename to packages/lib/src/lua/process/mod.rs index 7bf591a..c4274bd 100644 --- a/packages/lib/src/utils/process.rs +++ b/packages/lib/src/lua/process/mod.rs @@ -3,7 +3,8 @@ use std::process::ExitStatus; use mlua::prelude::*; 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( mut child: Child, diff --git a/packages/lib/src/utils/futures.rs b/packages/lib/src/lua/process/tee_writer.rs similarity index 100% rename from packages/lib/src/utils/futures.rs rename to packages/lib/src/lua/process/tee_writer.rs diff --git a/packages/lib/src/utils/formatting.rs b/packages/lib/src/lua/stdio/formatting.rs similarity index 100% rename from packages/lib/src/utils/formatting.rs rename to packages/lib/src/lua/stdio/formatting.rs diff --git a/packages/lib/src/lua/stdio/mod.rs b/packages/lib/src/lua/stdio/mod.rs index 41d4c14..1e6ac81 100644 --- a/packages/lib/src/lua/stdio/mod.rs +++ b/packages/lib/src/lua/stdio/mod.rs @@ -1,3 +1,2 @@ -mod prompt; - -pub use prompt::{PromptKind, PromptOptions, PromptResult}; +pub mod formatting; +pub mod prompt; diff --git a/packages/lib/src/utils/table.rs b/packages/lib/src/lua/table/builder.rs similarity index 100% rename from packages/lib/src/utils/table.rs rename to packages/lib/src/lua/table/builder.rs diff --git a/packages/lib/src/lua/table/mod.rs b/packages/lib/src/lua/table/mod.rs new file mode 100644 index 0000000..88239ac --- /dev/null +++ b/packages/lib/src/lua/table/mod.rs @@ -0,0 +1,3 @@ +mod builder; + +pub use builder::TableBuilder; diff --git a/packages/lib/src/utils/mod.rs b/packages/lib/src/utils/mod.rs deleted file mode 100644 index 61eebf6..0000000 --- a/packages/lib/src/utils/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod formatting; -pub mod futures; -pub mod net; -pub mod process; -pub mod table; diff --git a/packages/lib/src/utils/net.rs b/packages/lib/src/utils/net.rs deleted file mode 100644 index 9625650..0000000 --- a/packages/lib/src/utils/net.rs +++ /dev/null @@ -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") -}