Add docs on public functions, fix captures being returned even if not found

This commit is contained in:
Filip Tibell 2024-04-20 22:54:12 +02:00
parent 12f5824da9
commit 54081c1b0f
No known key found for this signature in database
3 changed files with 28 additions and 3 deletions

View file

@ -16,14 +16,26 @@ self_cell! {
} }
} }
/**
A wrapper over the `regex::Captures` struct that can be used from Lua.
*/
pub struct LuaCaptures { pub struct LuaCaptures {
inner: LuaCapturesInner, inner: LuaCapturesInner,
} }
impl LuaCaptures { impl LuaCaptures {
pub fn new(pattern: &Regex, text: String) -> Self { /**
Self { Create a new `LuaCaptures` instance from a `Regex` pattern and a `String` text.
inner: LuaCapturesInner::new(Arc::from(text), |owned| pattern.captures(owned.as_str())),
Returns `Some(_)` if captures were found, `None` if no captures were found.
*/
pub fn new(pattern: &Regex, text: String) -> Option<Self> {
let inner =
LuaCapturesInner::new(Arc::from(text), |owned| pattern.captures(owned.as_str()));
if inner.borrow_dependent().is_some() {
Some(Self { inner })
} else {
None
} }
} }

View file

@ -3,6 +3,9 @@ use std::{ops::Range, sync::Arc};
use mlua::prelude::*; use mlua::prelude::*;
use regex::Match; use regex::Match;
/**
A wrapper over the `regex::Match` struct that can be used from Lua.
*/
pub struct LuaMatch { pub struct LuaMatch {
text: Arc<String>, text: Arc<String>,
start: usize, start: usize,
@ -10,6 +13,9 @@ pub struct LuaMatch {
} }
impl LuaMatch { impl LuaMatch {
/**
Create a new `LuaMatch` instance from a `String` text and a `regex::Match`.
*/
pub fn new(text: Arc<String>, matched: Match) -> Self { pub fn new(text: Arc<String>, matched: Match) -> Self {
Self { Self {
text, text,

View file

@ -5,11 +5,18 @@ use regex::Regex;
use super::{captures::LuaCaptures, matches::LuaMatch}; use super::{captures::LuaCaptures, matches::LuaMatch};
/**
A wrapper over the `regex::Regex` struct that can be used from Lua.
*/
#[derive(Debug, Clone)]
pub struct LuaRegex { pub struct LuaRegex {
inner: Regex, inner: Regex,
} }
impl LuaRegex { impl LuaRegex {
/**
Create a new `LuaRegex` instance from a `String` pattern.
*/
pub fn new(pattern: String) -> LuaResult<Self> { pub fn new(pattern: String) -> LuaResult<Self> {
Regex::new(&pattern) Regex::new(&pattern)
.map(|inner| Self { inner }) .map(|inner| Self { inner })