Rename lb_libx to lb_xlib
This commit is contained in:
parent
da5acd6bc8
commit
ba969b9e56
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1031,6 +1031,7 @@ dependencies = [
|
||||
"luaify",
|
||||
"luajit",
|
||||
"sysexits",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
@ -13,6 +13,7 @@ derive_more = { version = "2.0.1", features = ["full"] }
|
||||
luaffi = { path = "../luaffi" }
|
||||
luajit = { path = "../luajit" }
|
||||
sysexits = "0.9.0"
|
||||
thiserror = "2.0.12"
|
||||
tokio = { version = "1.45.1", features = ["rt", "time", "fs", "net", "process", "signal", "tracing"] }
|
||||
tracing = "0.1.41"
|
||||
|
||||
|
@ -2,10 +2,10 @@
|
||||
use luaffi::{cdef, metatype};
|
||||
|
||||
#[cdef]
|
||||
pub struct lb_libchannel;
|
||||
pub struct lb_chanlib;
|
||||
|
||||
#[metatype]
|
||||
impl lb_libchannel {
|
||||
impl lb_chanlib {
|
||||
#[new]
|
||||
extern "Lua-C" fn new() -> Self {
|
||||
Self
|
@ -3,7 +3,7 @@
|
||||
//!
|
||||
//! # Exports
|
||||
//!
|
||||
//! See [`lb_libfs`] for items exported by this library.
|
||||
//! See [`lb_fslib`] for items exported by this library.
|
||||
use luaffi::{cdef, metatype};
|
||||
use std::io;
|
||||
use tokio::fs;
|
||||
@ -16,10 +16,10 @@ use tokio::fs;
|
||||
/// local fs = require("lb:fs");
|
||||
/// ```
|
||||
#[cdef]
|
||||
pub struct lb_libfs;
|
||||
pub struct lb_fslib;
|
||||
|
||||
#[metatype]
|
||||
impl lb_libfs {
|
||||
impl lb_fslib {
|
||||
#[new]
|
||||
extern "Lua-C" fn new() -> Self {
|
||||
Self
|
||||
|
@ -1,4 +1,4 @@
|
||||
pub mod channel;
|
||||
pub mod chan;
|
||||
pub mod fs;
|
||||
pub mod net;
|
||||
pub mod runtime;
|
||||
|
@ -3,27 +3,42 @@
|
||||
//!
|
||||
//! # 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 luaffi::{cdef, metatype};
|
||||
use std::{
|
||||
io,
|
||||
net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
|
||||
};
|
||||
use std::net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use thiserror::Error;
|
||||
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.
|
||||
///
|
||||
/// 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
|
||||
/// local net = require("lb:net");
|
||||
/// ```
|
||||
#[cdef]
|
||||
pub struct lb_libnet;
|
||||
pub struct lb_netlib;
|
||||
|
||||
#[metatype]
|
||||
impl lb_libnet {
|
||||
impl lb_netlib {
|
||||
#[new]
|
||||
extern "Lua-C" fn new() -> 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
|
||||
/// [`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.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Throws if `s` cannot be parsed as an IP address.
|
||||
pub extern "Lua" fn ipaddr(&self, s: any) -> lb_ipaddr {
|
||||
pub extern "Lua" fn ipaddr(&self, s: any) -> Result<lb_ipaddr> {
|
||||
if __istype(__ct.lb_ipaddr, s) {
|
||||
__new(__ct.lb_ipaddr, s) // copy constructor
|
||||
} 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> {
|
||||
s.parse()
|
||||
extern "Lua-C" fn __parse_ipaddr(&self, s: &str) -> Result<lb_ipaddr> {
|
||||
Ok(s.parse()?)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// If `port` is not specified, `0` is used as the default.
|
||||
///
|
||||
/// # 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 {
|
||||
pub extern "Lua" fn socketaddr(&self, s: any, port: any) -> Result<lb_socketaddr> {
|
||||
if port != () {
|
||||
self.__new_socketaddr(self.ipaddr(s), port)
|
||||
} else {
|
||||
@ -108,30 +115,22 @@ impl lb_libnet {
|
||||
SocketAddr::new(ip.0, port).into()
|
||||
}
|
||||
|
||||
extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr, AddrParseError> {
|
||||
s.parse()
|
||||
extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr> {
|
||||
Ok(s.parse()?)
|
||||
}
|
||||
|
||||
/// Creates a new TCP socket configured for IPv4.
|
||||
///
|
||||
/// See [`TcpSocket::new_v4`].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// 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)
|
||||
pub extern "Lua-C" fn tcp_v4(&self) -> Result<lb_tcpsocket> {
|
||||
Ok(Some(TcpSocket::new_v4()?).into())
|
||||
}
|
||||
|
||||
/// Creates a new TCP socket configured for IPv6.
|
||||
///
|
||||
/// See [`TcpSocket::new_v6`].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// 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)
|
||||
pub extern "Lua-C" fn tcp_v6(&self) -> Result<lb_tcpsocket> {
|
||||
Ok(Some(TcpSocket::new_v6()?).into())
|
||||
}
|
||||
}
|
||||
|
||||
@ -274,7 +273,7 @@ impl lb_socketaddr {
|
||||
|
||||
/// 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 {
|
||||
if __istype(__ct.lb_ipaddr, s) {
|
||||
self.__set_ip(s);
|
||||
@ -290,8 +289,8 @@ impl lb_socketaddr {
|
||||
self.0.set_ip(ip.0);
|
||||
}
|
||||
|
||||
extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<(), AddrParseError> {
|
||||
s.parse().map(|ip| self.0.set_ip(ip))
|
||||
extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<()> {
|
||||
Ok(self.0.set_ip(s.parse()?))
|
||||
}
|
||||
|
||||
/// Returns the port part of this address.
|
||||
@ -300,7 +299,7 @@ impl lb_socketaddr {
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
@ -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)]
|
||||
#[cdef]
|
||||
pub struct lb_tcpsocket(#[opaque] TcpSocket);
|
||||
pub struct lb_tcpsocket(#[opaque] Option<TcpSocket>);
|
||||
|
||||
#[metatype]
|
||||
impl lb_tcpsocket {}
|
||||
impl lb_tcpsocket {
|
||||
pub extern "Lua-C" fn keepalive(&self) -> Result<bool> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, From)]
|
||||
#[cdef]
|
||||
|
@ -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 luaffi::{Registry, Type};
|
||||
use luajit::{Chunk, State};
|
||||
@ -18,10 +18,10 @@ impl Builder {
|
||||
let mut registry = Registry::new();
|
||||
|
||||
registry
|
||||
.preload::<lb_libtask>("lb:task")
|
||||
.preload::<lb_libchannel>("lb:channel")
|
||||
.preload::<lb_libfs>("lb:fs")
|
||||
.preload::<lb_libnet>("lb:net");
|
||||
.preload::<lb_tasklib>("lb:task")
|
||||
.preload::<lb_chanlib>("lb:channel")
|
||||
.preload::<lb_fslib>("lb:fs")
|
||||
.preload::<lb_netlib>("lb:net");
|
||||
|
||||
Self { registry }
|
||||
}
|
||||
|
@ -4,10 +4,10 @@ use std::{ffi::c_int, process};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
#[cdef]
|
||||
pub struct lb_libtask;
|
||||
pub struct lb_tasklib;
|
||||
|
||||
#[metatype]
|
||||
impl lb_libtask {
|
||||
impl lb_tasklib {
|
||||
#[new]
|
||||
extern "Lua-C" fn new() -> Self {
|
||||
Self
|
||||
|
@ -1,2 +1 @@
|
||||
pub use lb::fs;
|
||||
pub use lb::net;
|
||||
pub use lb::*;
|
||||
|
Loading…
x
Reference in New Issue
Block a user