Skip to content

Commit

Permalink
sistana: feat: Fragment.apply_nepattern(capture_mode: bool)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyElaina committed Sep 21, 2024
1 parent d000dc4 commit da65981
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/arclet/alconna/sistana/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from dataclasses import dataclass
from elaina_segment import Segment, Quoted, UnmatchedQuoted

from .model.capture import RegexCapture
from .model.fragment import _Fragment


Expand Down Expand Up @@ -34,25 +36,29 @@ def _transform(v: Segment):
self.validator = _validate
self.transformer = _transform

def apply_nepattern(self):
def apply_nepattern(self, capture_mode: bool = False):
if self.type is None:
return

from nepattern import type_parser

pat = type_parser(self.type.value)

def _validate(v: Segment):
if isinstance(v, (Quoted, UnmatchedQuoted)):
if isinstance(v.ref, str):
v = str(v)
else:
v = v.ref
if capture_mode:
self.capture = RegexCapture(pat.alias)
else:
def _validate(v: Segment):
if isinstance(v, (Quoted, UnmatchedQuoted)):
if isinstance(v.ref, str):
v = str(v)
else:
v = v.ref

return pat.validate(v).success
return pat.validate(v).success

self.validator = _validate

def _transform(v: Segment):
return pat.transform(str(v)).value()

self.validator = _validate
self.transformer = _transform
3 changes: 2 additions & 1 deletion src/arclet/alconna/sistana/model/fragment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Callable, Iterable
from typing import Any, Callable, Iterable

from ..utils.misc import Some
from .capture import Capture, SimpleCapture
Expand All @@ -18,6 +18,7 @@ class _Fragment:

separators: str | None = None
hybrid_separators: bool = True

capture: Capture = SimpleCapture()
receiver: Rx[Any] = Rx()
validator: Callable[[Any], bool] | None = None
Expand Down

0 comments on commit da65981

Please sign in to comment.