diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-05 04:45:11 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-05 04:45:11 +0900 |
| commit | d0edb5b7ac3456f99c2f2576e8992cc12d60574a (patch) | |
| tree | b69546366676c06e1c8fabddb592c202436a92b2 /crates/shirabe/src/json/json_manipulator.rs | |
| parent | 53800ab77565de1c16fb8a95e047eba1cb3a160c (diff) | |
| download | php-shirabe-d0edb5b7ac3456f99c2f2576e8992cc12d60574a.tar.gz php-shirabe-d0edb5b7ac3456f99c2f2576e8992cc12d60574a.tar.zst php-shirabe-d0edb5b7ac3456f99c2f2576e8992cc12d60574a.zip | |
refactor(json): model JsonFile encode/write options as a typed struct
Replace the i64 bitmask + encode_with_indent split with a JsonEncodeOptions
struct (Default = JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE,
default indent). encode/write each get a default form plus an explicit
encode_with_options/write_with_options variant, mirroring PHP's optional
$options argument. write_with_options always encodes with self.indent, matching
PHP write().
Also reconcile call sites with the PHP sources: most ported sites passed 0 where
Composer omits the argument (= default flags), so JsonManipulator/ShowCommand and
JsonConfigSource now use the default options; only ComposerRepository and Locker
genuinely pass 0.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json/json_manipulator.rs')
| -rw-r--r-- | crates/shirabe/src/json/json_manipulator.rs | 68 |
1 files changed, 30 insertions, 38 deletions
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs index e5510d3..d907e91 100644 --- a/crates/shirabe/src/json/json_manipulator.rs +++ b/crates/shirabe/src/json/json_manipulator.rs @@ -89,7 +89,7 @@ impl JsonManipulator { "{{{}^(?P<start>\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?)(?P<property>{}\\s*:\\s*)(?P<value>(?&json))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(r#type.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(r#type.to_string())), None ), ); @@ -126,10 +126,11 @@ impl JsonManipulator { move |m: &IndexMap<CaptureKey, String>| -> String { format!( "{}{}\"{}\"", - JsonFile::encode( - &PhpMixed::String(str_replace("\\/", "/", &existing_owned)), - 0 - ), + JsonFile::encode(&PhpMixed::String(str_replace( + "\\/", + "/", + &existing_owned + ))), m.get(&CaptureKey::ByName("separator".to_string())) .cloned() .unwrap_or_default(), @@ -161,8 +162,8 @@ impl JsonManipulator { self.newline, self.indent, self.indent, - JsonFile::encode(&PhpMixed::String(package.to_string()), 0), - JsonFile::encode(&PhpMixed::String(constraint.to_string()), 0), + JsonFile::encode(&PhpMixed::String(package.to_string())), + JsonFile::encode(&PhpMixed::String(constraint.to_string())), groups_1 ), "\\$", @@ -176,8 +177,8 @@ impl JsonManipulator { self.newline, self.indent, self.indent, - JsonFile::encode(&PhpMixed::String(package.to_string()), 0), - JsonFile::encode(&PhpMixed::String(constraint.to_string()), 0), + JsonFile::encode(&PhpMixed::String(package.to_string())), + JsonFile::encode(&PhpMixed::String(constraint.to_string())), self.newline, self.indent ); @@ -370,7 +371,7 @@ impl JsonManipulator { let object_regex = format!( "{{{}^(?P<start>\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?\"repositories\"\\s*:\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?{}\\s*:\\s*)(?P<repository>(?&object))(?P<end>.*)}}sx", Self::DEFINES, - preg_quote(&JsonFile::encode(&repository_index, 0), None) + preg_quote(&JsonFile::encode(&repository_index), None) ); let mut matches: IndexMap<String, String> = IndexMap::new(); @@ -404,7 +405,7 @@ impl JsonManipulator { .get(&CaptureKey::ByName("start".to_string())) .cloned() .unwrap_or_default(), - JsonFile::encode(&PhpMixed::String(url_owned.clone()), 0), + JsonFile::encode(&PhpMixed::String(url_owned.clone())), repository_matches .get(&CaptureKey::ByName("end".to_string())) .cloned() @@ -681,7 +682,7 @@ impl JsonManipulator { "{{{}^(?P<start> \\s* \\{{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?{}\\s*:\\s*)(?P<content>(?&object))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(main_node.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(main_node.to_string())), None ) ); @@ -791,7 +792,7 @@ impl JsonManipulator { self.newline, self.indent, self.indent, - JsonFile::encode(&PhpMixed::String(name_owned.clone()), 0), + JsonFile::encode(&PhpMixed::String(name_owned.clone())), self.format(&value_local, 1, false)?, whitespace ), @@ -807,7 +808,7 @@ impl JsonManipulator { &format!( "{{{}{}: {},{}{}{}", whitespace, - JsonFile::encode(&PhpMixed::String(name_owned.clone()), 0), + JsonFile::encode(&PhpMixed::String(name_owned.clone())), self.format(&value_local, 1, false)?, self.newline, self.indent, @@ -832,7 +833,7 @@ impl JsonManipulator { self.newline, self.indent, self.indent, - JsonFile::encode(&PhpMixed::String(name_owned.clone()), 0), + JsonFile::encode(&PhpMixed::String(name_owned.clone())), self.format(&value_local, 1, false)?, whitespace ); @@ -881,7 +882,7 @@ impl JsonManipulator { "{{{}^(?P<start> \\s* \\{{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?{}\\s*:\\s*)(?P<content>(?&object))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(main_node.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(main_node.to_string())), None ) ); @@ -1146,7 +1147,7 @@ impl JsonManipulator { "{{{}^(?P<start> \\s* \\{{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?{}\\s*:\\s*)(?P<content>(?&array))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(main_node.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(main_node.to_string())), None ) ); @@ -1313,7 +1314,7 @@ impl JsonManipulator { "{{{}^(?P<start> \\s* \\{{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?{}\\s*:\\s*)(?P<content>(?&array))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(main_node.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(main_node.to_string())), None ) ); @@ -1416,7 +1417,7 @@ impl JsonManipulator { "{{{}^(?P<start> \\s* \\{{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?{}\\s*:\\s*)(?P<content>(?&array))(?P<end>.*)}}sx", Self::DEFINES, preg_quote( - &JsonFile::encode(&PhpMixed::String(main_node.to_string()), 0), + &JsonFile::encode(&PhpMixed::String(main_node.to_string())), None ) ); @@ -1509,10 +1510,7 @@ impl JsonManipulator { let regex = format!( "{{{}^(?P<start>\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?)(?P<key>{}\\s*:\\s*(?&json))(?P<end>.*)}}sx", Self::DEFINES, - preg_quote( - &JsonFile::encode(&PhpMixed::String(key.to_string()), 0), - None - ) + preg_quote(&JsonFile::encode(&PhpMixed::String(key.to_string())), None) ); let mut matches: IndexMap<String, String> = IndexMap::new(); if decoded.as_array().and_then(|a| a.get(key)).is_some() @@ -1527,7 +1525,7 @@ impl JsonManipulator { self.contents = format!( "{}{}: {}{}", matches.get("start").cloned().unwrap_or_default(), - JsonFile::encode(&PhpMixed::String(key.to_string()), 0), + JsonFile::encode(&PhpMixed::String(key.to_string())), content, matches.get("end").cloned().unwrap_or_default() ); @@ -1551,7 +1549,7 @@ impl JsonManipulator { ",{}{}{}: {}{}}}", self.newline, self.indent, - JsonFile::encode(&PhpMixed::String(key.to_string()), 0), + JsonFile::encode(&PhpMixed::String(key.to_string())), content, self.newline ), @@ -1570,7 +1568,7 @@ impl JsonManipulator { &format!( "{}{}: {}{}}}", self.indent, - JsonFile::encode(&PhpMixed::String(key.to_string()), 0), + JsonFile::encode(&PhpMixed::String(key.to_string())), content, self.newline ), @@ -1594,10 +1592,7 @@ impl JsonManipulator { let regex = format!( "{{{}^(?P<start>\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?)(?P<removal>{}\\s*:\\s*(?&json))\\s*,?\\s*(?P<end>.*)}}sx", Self::DEFINES, - preg_quote( - &JsonFile::encode(&PhpMixed::String(key.to_string()), 0), - None - ) + preg_quote(&JsonFile::encode(&PhpMixed::String(key.to_string())), None) ); let mut matches: IndexMap<String, String> = IndexMap::new(); if Preg::is_match_named(®ex, &self.contents, &mut matches).unwrap_or(false) { @@ -1641,10 +1636,7 @@ impl JsonManipulator { let regex = format!( "{{{}^(?P<start>\\s*\\{{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?{}\\s*:\\s*)(?P<removal>\\{{(?P<removal_space>\\s*+)\\}})(?P<end>\\s*,?\\s*.*)}}sx", Self::DEFINES, - preg_quote( - &JsonFile::encode(&PhpMixed::String(key.to_string()), 0), - None - ) + preg_quote(&JsonFile::encode(&PhpMixed::String(key.to_string())), None) ); let mut matches: IndexMap<String, String> = IndexMap::new(); if Preg::is_match_named(®ex, &self.contents, &mut matches).unwrap_or(false) { @@ -1730,7 +1722,7 @@ impl JsonManipulator { elems.push(format!( "{}{}: {}", str_repeat(&self.indent, (depth + 2) as usize), - JsonFile::encode(&PhpMixed::String(key.clone()), 0), + JsonFile::encode(&PhpMixed::String(key.clone())), self.format(val, depth + 1, false)? )); } @@ -1745,7 +1737,7 @@ impl JsonManipulator { )); } - Ok(JsonFile::encode(&data, 0)) + Ok(JsonFile::encode(&data)) } pub(crate) fn detect_indenting(&mut self) { @@ -1803,7 +1795,7 @@ impl ManipulatorFormatter { elems.push(format!( "{}{}: {}", str_repeat(&self.indent, (depth + 2) as usize), - JsonFile::encode(&PhpMixed::String(key.clone()), 0), + JsonFile::encode(&PhpMixed::String(key.clone())), self.format(val, depth + 1, false)? )); } @@ -1818,6 +1810,6 @@ impl ManipulatorFormatter { )); } - Ok(JsonFile::encode(&data, 0)) + Ok(JsonFile::encode(&data)) } } |
