aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-17 06:43:52 +0900
committernsfisis <nsfisis@gmail.com>2026-08-17 06:43:52 +0900
commit52a0665acbbe5bf5b8c6875118fb9cdf7952453b (patch)
tree7302b0b87f7b0d406126da734ef82c80abf64204 /crates/shirabe/src/json
parentc81e9f89d9e4388f36a4427bbaa95eced659d2ce (diff)
downloadphp-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.tar.gz
php-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.tar.zst
php-shirabe-52a0665acbbe5bf5b8c6875118fb9cdf7952453b.zip
refactor(preg): wrap the preg_* $matches maps in newtypes
The five IndexMap shapes that the preg_* functions and Preg fill in are now distinct types generated by preg_match_map!, so a matches map no longer interchanges with any other map of the same key and value type. Index<usize> is kept alongside Index<&Q> because call sites such as config_command and event_dispatcher reach for a group by its position in the map rather than by its capture key.
Diffstat (limited to 'crates/shirabe/src/json')
-rw-r--r--crates/shirabe/src/json/json_file.rs7
-rw-r--r--crates/shirabe/src/json/json_manipulator.rs12
2 files changed, 9 insertions, 10 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index e31cd70d..040f472a 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -8,8 +8,7 @@ use crate::json::JsonValidationException;
use crate::util::Filesystem;
use crate::util::HttpDownloader;
use crate::util::Silencer;
-use indexmap::IndexMap;
-use shirabe_pcre::{CaptureKey, Preg};
+use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups};
use shirabe_php_shim::Catch as _;
use shirabe_php_shim::{
InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
@@ -451,7 +450,7 @@ impl JsonFile {
let indent_owned = options.indent;
return Ok(Preg::replace_callback(
php_regex!(r"#^ {4,}#m"),
- move |m: &indexmap::IndexMap<shirabe_pcre::CaptureKey, String>| -> String {
+ move |m: &shirabe_pcre::PregMatchedGroups| -> String {
let whole = m
.get(&shirabe_pcre::CaptureKey::ByIndex(0))
.map(|s| s.as_str())
@@ -555,7 +554,7 @@ impl JsonFile {
}
pub fn detect_indenting(json: Option<&str>) -> String {
- let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut m = PregMatchedGroups::new();
if Preg::is_match3(
php_regex!(r##"#^([ \t]+)"#m"##),
json.unwrap_or(""),
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs
index 08880e80..425eb192 100644
--- a/crates/shirabe/src/json/json_manipulator.rs
+++ b/crates/shirabe/src/json/json_manipulator.rs
@@ -4,7 +4,7 @@ use crate::json::JsonFile;
use crate::json::json_grammar::{self, ValueKind};
use crate::repository::PlatformRepository;
use indexmap::IndexMap;
-use shirabe_pcre::{CaptureKey, Preg};
+use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups, PregNamedGroups};
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PhpMixed, addcslashes, array_key_exists, array_keys,
array_reverse, empty, explode, implode, in_array_loose, is_array, is_int, is_numeric,
@@ -112,7 +112,7 @@ impl JsonManipulator {
&links[value_end..]
);
} else {
- let mut groups: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut groups = PregMatchedGroups::new();
if Preg::is_match3(
php_regex!("#^\\s*\\{\\s*\\S+.*?(\\s*\\}\\s*)$#s"),
&links,
@@ -740,7 +740,7 @@ impl JsonManipulator {
&children[cm.value_end..]
);
} else {
- let mut leading_match: IndexMap<String, String> = IndexMap::new();
+ let mut leading_match = PregNamedGroups::new();
if Preg::is_match_named(
php_regex!(
"#^\\{(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s"
@@ -942,7 +942,7 @@ impl JsonManipulator {
let children_clean = children_clean.ok_or_else(|| InvalidArgumentException::new("JsonManipulator: $childrenClean is not defined. Please report at https://github.com/nsfisis/php-shirabe/issues/new.".to_string()))?;
// no child data left, $name was the only key in
- let mut empty_match: IndexMap<String, String> = IndexMap::new();
+ let mut empty_match = PregNamedGroups::new();
if Preg::is_match_named(
php_regex!("#^\\{\\s*?(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\}$#s"),
&children_clean,
@@ -1039,7 +1039,7 @@ impl JsonManipulator {
return Ok(false);
}
- let mut leading_match: IndexMap<String, String> = IndexMap::new();
+ let mut leading_match = PregNamedGroups::new();
if Preg::is_match_named(
php_regex!(
"#^\\[(?P<leadingspace>\\s*?)(?P<content>\\S+.*?)?(?P<trailingspace>\\s*)\\]$#s"
@@ -1330,7 +1330,7 @@ impl JsonManipulator {
}
// append at the end of the file and keep whitespace
- let mut tail_match: IndexMap<CaptureKey, String> = IndexMap::new();
+ let mut tail_match = PregMatchedGroups::new();
if Preg::is_match3(
php_regex!("#[^{\\s](\\s*)\\}$#"),
&self.contents,