Rename lb_libx to lb_xlib

This commit is contained in:
lumi 2025-06-26 00:40:26 +10:00
parent da5acd6bc8
commit ba969b9e56
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
9 changed files with 60 additions and 56 deletions

1
Cargo.lock generated
View File

@ -1031,6 +1031,7 @@ dependencies = [
"luaify", "luaify",
"luajit", "luajit",
"sysexits", "sysexits",
"thiserror",
"tokio", "tokio",
"tracing", "tracing",
] ]

View File

@ -13,6 +13,7 @@ derive_more = { version = "2.0.1", features = ["full"] }
luaffi = { path = "../luaffi" } luaffi = { path = "../luaffi" }
luajit = { path = "../luajit" } luajit = { path = "../luajit" }
sysexits = "0.9.0" sysexits = "0.9.0"
thiserror = "2.0.12"
tokio = { version = "1.45.1", features = ["rt", "time", "fs", "net", "process", "signal", "tracing"] } tokio = { version = "1.45.1", features = ["rt", "time", "fs", "net", "process", "signal", "tracing"] }
tracing = "0.1.41" tracing = "0.1.41"

View File

@ -2,10 +2,10 @@
use luaffi::{cdef, metatype}; use luaffi::{cdef, metatype};
#[cdef] #[cdef]
pub struct lb_libchannel; pub struct lb_chanlib;
#[metatype] #[metatype]
impl lb_libchannel { impl lb_chanlib {
#[new] #[new]
extern "Lua-C" fn new() -> Self { extern "Lua-C" fn new() -> Self {
Self Self

View File

@ -3,7 +3,7 @@
//! //!
//! # Exports //! # Exports
//! //!
//! See [`lb_libfs`] for items exported by this library. //! See [`lb_fslib`] for items exported by this library.
use luaffi::{cdef, metatype}; use luaffi::{cdef, metatype};
use std::io; use std::io;
use tokio::fs; use tokio::fs;
@ -16,10 +16,10 @@ use tokio::fs;
/// local fs = require("lb:fs"); /// local fs = require("lb:fs");
/// ``` /// ```
#[cdef] #[cdef]
pub struct lb_libfs; pub struct lb_fslib;
#[metatype] #[metatype]
impl lb_libfs { impl lb_fslib {
#[new] #[new]
extern "Lua-C" fn new() -> Self { extern "Lua-C" fn new() -> Self {
Self Self

View File

@ -1,4 +1,4 @@
pub mod channel; pub mod chan;
pub mod fs; pub mod fs;
pub mod net; pub mod net;
pub mod runtime; pub mod runtime;

View File

@ -3,27 +3,42 @@
//! //!
//! # Exports //! # Exports
//! //!
//! See [`lb_libnet`] for items exported by this library. //! See [`lb_netlib`] for items exported by this library.
use derive_more::{From, FromStr}; use derive_more::{From, FromStr};
use luaffi::{cdef, metatype}; use luaffi::{cdef, metatype};
use std::{ use std::net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
io, use thiserror::Error;
net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
};
use tokio::net::{TcpListener, TcpSocket, TcpStream}; use tokio::net::{TcpListener, TcpSocket, TcpStream};
/// Errors that can be thrown by this library.
///
/// Functions which return this error will **throw** in Lua. The error message can be caught by
/// using [`pcall(f, ...)`](https://www.lua.org/manual/5.1/manual.html#pdf-pcall).
#[derive(Debug, Error)]
pub enum Error {
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
InvalidAddr(#[from] AddrParseError),
#[error("socket was already converted")]
SocketConsumed,
}
type Result<T> = std::result::Result<T, Error>;
/// Items exported by the `lb:net` library. /// Items exported by the `lb:net` library.
/// ///
/// This library can be obtained by calling `require` in Lua. /// This library can be obtained by calling
/// [`require("lb:net")`](https://www.lua.org/manual/5.1/manual.html#pdf-require) in Lua.
/// ///
/// ```lua /// ```lua
/// local net = require("lb:net"); /// local net = require("lb:net");
/// ``` /// ```
#[cdef] #[cdef]
pub struct lb_libnet; pub struct lb_netlib;
#[metatype] #[metatype]
impl lb_libnet { impl lb_netlib {
#[new] #[new]
extern "Lua-C" fn new() -> Self { extern "Lua-C" fn new() -> Self {
Self Self
@ -59,11 +74,7 @@ impl lb_libnet {
/// If `s` is an [`lb_ipaddr`], a copy of that value is returned. If `s` is an /// If `s` is an [`lb_ipaddr`], a copy of that value is returned. If `s` is an
/// [`lb_socketaddr`], the IP address part of the socket address is returned. Otherwise, parses /// [`lb_socketaddr`], the IP address part of the socket address is returned. Otherwise, parses
/// `s` as an IP address string. Both IPv4 or IPv6 addresses are supported. /// `s` as an IP address string. Both IPv4 or IPv6 addresses are supported.
/// pub extern "Lua" fn ipaddr(&self, s: any) -> Result<lb_ipaddr> {
/// # Errors
///
/// Throws if `s` cannot be parsed as an IP address.
pub extern "Lua" fn ipaddr(&self, s: any) -> lb_ipaddr {
if __istype(__ct.lb_ipaddr, s) { if __istype(__ct.lb_ipaddr, s) {
__new(__ct.lb_ipaddr, s) // copy constructor __new(__ct.lb_ipaddr, s) // copy constructor
} else if __istype(__ct.lb_socketaddr, s) { } else if __istype(__ct.lb_socketaddr, s) {
@ -73,8 +84,8 @@ impl lb_libnet {
} }
} }
extern "Lua-C" fn __parse_ipaddr(&self, s: &str) -> Result<lb_ipaddr, AddrParseError> { extern "Lua-C" fn __parse_ipaddr(&self, s: &str) -> Result<lb_ipaddr> {
s.parse() Ok(s.parse()?)
} }
/// Creates an [`lb_socketaddr`] from the given input. /// Creates an [`lb_socketaddr`] from the given input.
@ -86,11 +97,7 @@ impl lb_libnet {
/// socket address string. Both IPv4 and IPv6 addresses are supported. /// socket address string. Both IPv4 and IPv6 addresses are supported.
/// ///
/// If `port` is not specified, `0` is used as the default. /// If `port` is not specified, `0` is used as the default.
/// pub extern "Lua" fn socketaddr(&self, s: any, port: any) -> Result<lb_socketaddr> {
/// # Errors
///
/// Throws if `s` cannot be parsed as an IP or socket address.
pub extern "Lua" fn socketaddr(&self, s: any, port: any) -> lb_socketaddr {
if port != () { if port != () {
self.__new_socketaddr(self.ipaddr(s), port) self.__new_socketaddr(self.ipaddr(s), port)
} else { } else {
@ -108,30 +115,22 @@ impl lb_libnet {
SocketAddr::new(ip.0, port).into() SocketAddr::new(ip.0, port).into()
} }
extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr, AddrParseError> { extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr> {
s.parse() Ok(s.parse()?)
} }
/// Creates a new TCP socket configured for IPv4. /// Creates a new TCP socket configured for IPv4.
/// ///
/// See [`TcpSocket::new_v4`]. /// See [`TcpSocket::new_v4`].
/// pub extern "Lua-C" fn tcp_v4(&self) -> Result<lb_tcpsocket> {
/// # Errors Ok(Some(TcpSocket::new_v4()?).into())
///
/// Throws if an error was encountered during the socket creation.
pub extern "Lua-C" fn tcp_v4(&self) -> io::Result<lb_tcpsocket> {
TcpSocket::new_v4().map(lb_tcpsocket)
} }
/// Creates a new TCP socket configured for IPv6. /// Creates a new TCP socket configured for IPv6.
/// ///
/// See [`TcpSocket::new_v6`]. /// See [`TcpSocket::new_v6`].
/// pub extern "Lua-C" fn tcp_v6(&self) -> Result<lb_tcpsocket> {
/// # Errors Ok(Some(TcpSocket::new_v6()?).into())
///
/// Throws if an error was encountered during the socket creation.
pub extern "Lua-C" fn tcp_v6(&self) -> io::Result<lb_tcpsocket> {
TcpSocket::new_v6().map(lb_tcpsocket)
} }
} }
@ -274,7 +273,7 @@ impl lb_socketaddr {
/// Sets the IP part of this address. /// Sets the IP part of this address.
/// ///
/// This function accepts the same arguments as [`ipaddr`](lb_libnet::ipaddr). /// This function accepts the same arguments as [`ipaddr`](lb_netlib::ipaddr).
pub extern "Lua" fn set_ip(&mut self, s: any) -> &mut Self { pub extern "Lua" fn set_ip(&mut self, s: any) -> &mut Self {
if __istype(__ct.lb_ipaddr, s) { if __istype(__ct.lb_ipaddr, s) {
self.__set_ip(s); self.__set_ip(s);
@ -290,8 +289,8 @@ impl lb_socketaddr {
self.0.set_ip(ip.0); self.0.set_ip(ip.0);
} }
extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<(), AddrParseError> { extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<()> {
s.parse().map(|ip| self.0.set_ip(ip)) Ok(self.0.set_ip(s.parse()?))
} }
/// Returns the port part of this address. /// Returns the port part of this address.
@ -300,7 +299,7 @@ impl lb_socketaddr {
} }
/// Sets the port part of this address. /// Sets the port part of this address.
pub extern "Lua" fn set_port(&mut self, port: number) -> &mut Self { pub extern "Lua" fn set_port(&mut self, port: integer) -> &mut Self {
self.__set_port(port); self.__set_port(port);
self self
} }
@ -316,13 +315,17 @@ impl lb_socketaddr {
} }
} }
/// A TCP socket which has not yet been converted to a [`lb_tcpstream`] or [`lb_tcplistener`]. /// A TCP socket which has not yet been converted to an [`lb_tcpstream`] or [`lb_tcplistener`].
#[derive(Debug, From)] #[derive(Debug, From)]
#[cdef] #[cdef]
pub struct lb_tcpsocket(#[opaque] TcpSocket); pub struct lb_tcpsocket(#[opaque] Option<TcpSocket>);
#[metatype] #[metatype]
impl lb_tcpsocket {} impl lb_tcpsocket {
pub extern "Lua-C" fn keepalive(&self) -> Result<bool> {
todo!()
}
}
#[derive(Debug, From)] #[derive(Debug, From)]
#[cdef] #[cdef]

View File

@ -1,4 +1,4 @@
use crate::{channel::lb_libchannel, fs::lb_libfs, net::lb_libnet, task::lb_libtask}; use crate::{chan::lb_chanlib, fs::lb_fslib, net::lb_netlib, task::lb_tasklib};
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use luaffi::{Registry, Type}; use luaffi::{Registry, Type};
use luajit::{Chunk, State}; use luajit::{Chunk, State};
@ -18,10 +18,10 @@ impl Builder {
let mut registry = Registry::new(); let mut registry = Registry::new();
registry registry
.preload::<lb_libtask>("lb:task") .preload::<lb_tasklib>("lb:task")
.preload::<lb_libchannel>("lb:channel") .preload::<lb_chanlib>("lb:channel")
.preload::<lb_libfs>("lb:fs") .preload::<lb_fslib>("lb:fs")
.preload::<lb_libnet>("lb:net"); .preload::<lb_netlib>("lb:net");
Self { registry } Self { registry }
} }

View File

@ -4,10 +4,10 @@ use std::{ffi::c_int, process};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
#[cdef] #[cdef]
pub struct lb_libtask; pub struct lb_tasklib;
#[metatype] #[metatype]
impl lb_libtask { impl lb_tasklib {
#[new] #[new]
extern "Lua-C" fn new() -> Self { extern "Lua-C" fn new() -> Self {
Self Self

View File

@ -1,2 +1 @@
pub use lb::fs; pub use lb::*;
pub use lb::net;