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 | |
| 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')
21 files changed, 200 insertions, 177 deletions
diff --git a/crates/shirabe/src/advisory/auditor.rs b/crates/shirabe/src/advisory/auditor.rs index af54d06..ffe1a12 100644 --- a/crates/shirabe/src/advisory/auditor.rs +++ b/crates/shirabe/src/advisory/auditor.rs @@ -168,13 +168,9 @@ impl Auditor { ), ); - io.write(&JsonFile::encode_with_indent( - &PhpMixed::Array(json.into_iter().map(|(k, v)| (k, Box::new(v))).collect()), - shirabe_php_shim::JSON_UNESCAPED_SLASHES - | shirabe_php_shim::JSON_PRETTY_PRINT - | shirabe_php_shim::JSON_UNESCAPED_UNICODE, - JsonFile::INDENT_DEFAULT, - )); + io.write(&JsonFile::encode(&PhpMixed::Array( + json.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), + ))); return Ok(audit_bitmask); } diff --git a/crates/shirabe/src/command/check_platform_reqs_command.rs b/crates/shirabe/src/command/check_platform_reqs_command.rs index bfef6a5..edacf0b 100644 --- a/crates/shirabe/src/command/check_platform_reqs_command.rs +++ b/crates/shirabe/src/command/check_platform_reqs_command.rs @@ -312,10 +312,9 @@ impl CheckPlatformReqsCommand { }) .collect(); - io.write(&JsonFile::encode( - &PhpMixed::List(rows.into_iter().map(Box::new).collect()), - 448, - )); + io.write(&JsonFile::encode(&PhpMixed::List( + rows.into_iter().map(Box::new).collect(), + ))); } else { let rows: Vec<PhpMixed> = results .iter() diff --git a/crates/shirabe/src/command/config_command.rs b/crates/shirabe/src/command/config_command.rs index 476be75..40a12e1 100644 --- a/crates/shirabe/src/command/config_command.rs +++ b/crates/shirabe/src/command/config_command.rs @@ -8,12 +8,11 @@ use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_external_packages::symfony::component::console::input::InputInterface; use shirabe_external_packages::symfony::component::console::output::OutputInterface; use shirabe_php_shim::{ - ArrayObject, InvalidArgumentException, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, - JsonObject, PhpMixed, RuntimeException, array_filter, array_filter_use_key, array_is_list, - array_map, array_merge, array_unique, call_user_func, count, escapeshellcmd, exec, explode, - file_exists, file_get_contents, implode, in_array, is_array, is_bool, is_dir, is_numeric, - is_object, is_string, json_encode, key, sort, sprintf, str_replace, str_starts_with, strpos, - strtolower, system, touch, var_export, + ArrayObject, InvalidArgumentException, JsonObject, PhpMixed, RuntimeException, array_filter, + array_filter_use_key, array_is_list, array_map, array_merge, array_unique, call_user_func, + count, escapeshellcmd, exec, explode, file_exists, file_get_contents, implode, in_array, + is_array, is_bool, is_dir, is_numeric, is_object, is_string, json_encode, key, sort, sprintf, + str_replace, str_starts_with, strpos, strtolower, system, touch, var_export, }; use crate::advisory::Auditor; @@ -26,6 +25,7 @@ use crate::console::input::InputArgument; use crate::factory::Factory; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::package::base_package::{self, BasePackage}; use crate::util::Filesystem; @@ -476,7 +476,13 @@ impl ConfigCommand { } let value_str = if is_array(&value) || is_object(&value) || is_bool(&value) { - JsonFile::encode(&value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) + JsonFile::encode_with_options( + &value, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, + ) } else { value.as_string().unwrap_or("").to_string() }; diff --git a/crates/shirabe/src/command/fund_command.rs b/crates/shirabe/src/command/fund_command.rs index 9d5720e..a01a67c 100644 --- a/crates/shirabe/src/command/fund_command.rs +++ b/crates/shirabe/src/command/fund_command.rs @@ -162,7 +162,7 @@ impl FundCommand { io.write("Thank you!"); } else if format == "json" { let fundings_mixed: PhpMixed = fundings.clone().into(); - io.write(&JsonFile::encode(&fundings_mixed, 448)); + io.write(&JsonFile::encode(&fundings_mixed)); } else { io.write("No funding links were found in your package dependencies. This doesn't mean they don't need your support!"); } diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs index e401534..a46b6ca 100644 --- a/crates/shirabe/src/command/init_command.rs +++ b/crates/shirabe/src/command/init_command.rs @@ -301,7 +301,7 @@ impl InitCommand { .into_iter() .map(|(k, v)| (k, Box::new(v))) .collect(); - let json = JsonFile::encode(&PhpMixed::Array(options_for_encode.clone()), 448); + let json = JsonFile::encode(&PhpMixed::Array(options_for_encode.clone())); if input.is_interactive() { io.write_error3(&format!("\n{}\n", json), true, io_interface::NORMAL); diff --git a/crates/shirabe/src/command/licenses_command.rs b/crates/shirabe/src/command/licenses_command.rs index ddd7d3d..81f9b67 100644 --- a/crates/shirabe/src/command/licenses_command.rs +++ b/crates/shirabe/src/command/licenses_command.rs @@ -247,15 +247,12 @@ impl LicensesCommand { .collect(), ), ); - io.write(&JsonFile::encode( - &PhpMixed::Array( - output_map - .into_iter() - .map(|(k, v)| (k, Box::new(v))) - .collect(), - ), - 448, - )); + io.write(&JsonFile::encode(&PhpMixed::Array( + output_map + .into_iter() + .map(|(k, v)| (k, Box::new(v))) + .collect(), + ))); } "summary" => { let mut used_licenses: IndexMap<String, i64> = IndexMap::new(); diff --git a/crates/shirabe/src/command/repository_command.rs b/crates/shirabe/src/command/repository_command.rs index bf000e4..d57a98d 100644 --- a/crates/shirabe/src/command/repository_command.rs +++ b/crates/shirabe/src/command/repository_command.rs @@ -380,7 +380,7 @@ impl RepositoryCommand { .get("url") .and_then(|v| v.as_string()) .map(|s| s.to_string()) - .unwrap_or_else(|| JsonFile::encode(repo, 448)); + .unwrap_or_else(|| JsonFile::encode(repo)); io.write(&format!("[{}] <info>{}</info> {}", name, r#type, url)); } } diff --git a/crates/shirabe/src/command/search_command.rs b/crates/shirabe/src/command/search_command.rs index 5d6a31b..2c5d27d 100644 --- a/crates/shirabe/src/command/search_command.rs +++ b/crates/shirabe/src/command/search_command.rs @@ -182,7 +182,7 @@ impl SearchCommand { } else if format == "json" { // TODO(phase-b): JsonFile::encode takes &PhpMixed; convert Vec<SearchResult> into PhpMixed let _ = &results; - io.write(&JsonFile::encode(&PhpMixed::Null, 448)); + io.write(&JsonFile::encode(&PhpMixed::Null)); } Ok(0) diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index f992798..bb7b909 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -575,12 +575,9 @@ impl ShowCommand { .collect(), ))]), ); - self.get_io().write(&JsonFile::encode( - &PhpMixed::Array( - wrapper.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), - ), - 0, - )); + self.get_io().write(&JsonFile::encode(&PhpMixed::Array( + wrapper.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), + ))); } else { self.display_package_tree(vec![array_tree]); } @@ -700,10 +697,9 @@ impl ShowCommand { .collect(), ), ); - self.get_io().write(&JsonFile::encode( - &PhpMixed::Array(wrapper.into_iter().map(|(k, v)| (k, Box::new(v))).collect()), - 0, - )); + self.get_io().write(&JsonFile::encode(&PhpMixed::Array( + wrapper.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), + ))); } else { self.display_package_tree(array_tree); } @@ -1152,15 +1148,12 @@ impl ShowCommand { ); } let io = self.get_io(); - io.write(&JsonFile::encode( - &PhpMixed::Array( - json_map - .into_iter() - .map(|(k, v)| (k, Box::new(v))) - .collect(), - ), - 0, - )); + io.write(&JsonFile::encode(&PhpMixed::Array( + json_map + .into_iter() + .map(|(k, v)| (k, Box::new(v))) + .collect(), + ))); } else { if input.get_option("latest").as_bool() == Some(true) && view_data.values().any(|v| !v.is_empty()) @@ -2025,10 +2018,9 @@ impl ShowCommand { json = Self::append_links(json, package); - self.get_io().write(&JsonFile::encode( - &PhpMixed::Array(json.into_iter().map(|(k, v)| (k, Box::new(v))).collect()), - 0, - )); + self.get_io().write(&JsonFile::encode(&PhpMixed::Array( + json.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), + ))); Ok(()) } diff --git a/crates/shirabe/src/config/json_config_source.rs b/crates/shirabe/src/config/json_config_source.rs index 28481db..5d81793 100644 --- a/crates/shirabe/src/config/json_config_source.rs +++ b/crates/shirabe/src/config/json_config_source.rs @@ -199,12 +199,7 @@ impl JsonConfigSource { } } } - self.file.write2( - config, - shirabe_php_shim::JSON_UNESCAPED_SLASHES - | shirabe_php_shim::JSON_PRETTY_PRINT - | shirabe_php_shim::JSON_UNESCAPED_UNICODE, - )?; + self.file.write(config)?; } match self.file.validate_schema(JsonFile::LAX_SCHEMA, None) { diff --git a/crates/shirabe/src/factory.rs b/crates/shirabe/src/factory.rs index 90d8593..bb6ff28 100644 --- a/crates/shirabe/src/factory.rs +++ b/crates/shirabe/src/factory.rs @@ -766,15 +766,12 @@ impl Factory { composer_full .set_locker(std::rc::Rc::new(std::cell::RefCell::new(locker))); } else { - let lock_contents = JsonFile::encode( - &PhpMixed::Array( - local_config_data - .iter() - .map(|(k, v)| (k.clone(), Box::new(v.clone()))) - .collect(), - ), - 448, - ); + let lock_contents = JsonFile::encode(&PhpMixed::Array( + local_config_data + .iter() + .map(|(k, v)| (k.clone(), Box::new(v.clone()))) + .collect(), + )); let locker = Locker::new( io.clone(), JsonFile::new(Platform::get_dev_null(), None, Some(io.clone()))?, diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index 0b1b010..06f3946 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -24,6 +24,50 @@ use crate::json::JsonValidationException; use crate::util::Filesystem; use crate::util::HttpDownloader; +#[derive(Debug, Clone)] +pub struct JsonEncodeOptions { + pub unescaped_slashes: bool, + pub pretty_print: bool, + pub unescaped_unicode: bool, + pub indent: String, +} + +impl Default for JsonEncodeOptions { + fn default() -> Self { + Self { + unescaped_slashes: true, + pretty_print: true, + unescaped_unicode: true, + indent: JsonFile::INDENT_DEFAULT.to_string(), + } + } +} + +impl JsonEncodeOptions { + pub fn none() -> Self { + Self { + unescaped_slashes: false, + pretty_print: false, + unescaped_unicode: false, + indent: JsonFile::INDENT_DEFAULT.to_string(), + } + } + + fn to_flags(&self) -> i64 { + let mut flags = 0; + if self.unescaped_slashes { + flags |= JSON_UNESCAPED_SLASHES; + } + if self.pretty_print { + flags |= JSON_PRETTY_PRINT; + } + if self.unescaped_unicode { + flags |= JSON_UNESCAPED_UNICODE; + } + flags + } +} + /// Reads/writes json files. #[derive(Debug)] pub struct JsonFile { @@ -171,21 +215,15 @@ impl JsonFile { } pub fn write(&self, hash: PhpMixed) -> Result<()> { - self.write2( - hash, - JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, - ) + self.write_with_options(hash, JsonEncodeOptions::default()) } - /// Writes json file. - /// - /// @param mixed[] $hash writes hash into json file - /// @param int $options json_encode options - /// @throws \UnexpectedValueException|\Exception - /// @return void - pub fn write2(&self, hash: PhpMixed, options: i64) -> Result<()> { + pub fn write_with_options(&self, hash: PhpMixed, options: JsonEncodeOptions) -> Result<()> { if self.path == "php://memory" { - file_put_contents(&self.path, Self::encode(&hash, options).as_bytes()); + file_put_contents( + &self.path, + Self::encode_with_options(&hash, options.clone()).as_bytes(), + ); return Ok(()); } @@ -220,12 +258,8 @@ impl JsonFile { &self.path, &format!( "{}{}", - Self::encode(&hash, options), - if options & JSON_PRETTY_PRINT != 0 { - "\n" - } else { - "" - }, + Self::encode_with_options(&hash, options.clone()), + if options.pretty_print { "\n" } else { "" }, ), )?; Ok(()) @@ -392,18 +426,12 @@ impl JsonFile { Ok(true) } - /// Encodes an array into (optionally pretty-printed) JSON - /// - /// @param mixed $data Data to encode into a formatted JSON string - /// @param int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) - /// @param string $indent Indentation string - /// @return string Encoded json - pub fn encode(data: &PhpMixed, options: i64) -> String { - Self::encode_with_indent(data, options, Self::INDENT_DEFAULT) + pub fn encode(data: &PhpMixed) -> String { + Self::encode_with_options(data, JsonEncodeOptions::default()) } - pub fn encode_with_indent(data: &PhpMixed, options: i64, indent: &str) -> String { - let json = json_encode_ex(data, options); + pub fn encode_with_options(data: &PhpMixed, options: JsonEncodeOptions) -> String { + let json = json_encode_ex(data, options.to_flags()); let json = match json { Some(j) => j, @@ -415,9 +443,9 @@ impl JsonFile { } }; - if (options & JSON_PRETTY_PRINT) > 0 && indent != Self::INDENT_DEFAULT { + if options.pretty_print && options.indent != Self::INDENT_DEFAULT { // Pretty printing and not using default indentation - let indent_owned = indent.to_string(); + let indent_owned = options.indent.clone(); return Preg::replace_callback( r"#^ {4,}#m", move |m: &indexmap::IndexMap< 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)) } } diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs index e90a8f0..59b81b1 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -14,6 +14,7 @@ use shirabe_php_shim::{ use crate::installer::InstallationManager; use crate::io::IOInterface; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::package::BasePackageHandle; use crate::package::CompleteAliasPackageHandle; @@ -133,14 +134,14 @@ impl Locker { Ok(hash( "md5", - &JsonFile::encode( + &JsonFile::encode_with_options( &PhpMixed::Array( relevant_content .into_iter() .map(|(k, v)| (k, Box::new(v))) .collect(), ), - 0, + JsonEncodeOptions::none(), ), )) } @@ -634,13 +635,9 @@ impl Locker { } else { self.virtual_file_written = true; let parsed = JsonFile::parse_json( - Some(&JsonFile::encode_with_indent( - &PhpMixed::Array(lock.into_iter().map(|(k, v)| (k, Box::new(v))).collect()), - shirabe_php_shim::JSON_UNESCAPED_SLASHES - | shirabe_php_shim::JSON_PRETTY_PRINT - | shirabe_php_shim::JSON_UNESCAPED_UNICODE, - JsonFile::INDENT_DEFAULT, - )), + Some(&JsonFile::encode(&PhpMixed::Array( + lock.into_iter().map(|(k, v)| (k, Box::new(v))).collect(), + ))), None, )?; let parsed_map: IndexMap<String, PhpMixed> = match parsed { diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs index 9e5e48c..591497d 100644 --- a/crates/shirabe/src/repository/composer_repository.rs +++ b/crates/shirabe/src/repository/composer_repository.rs @@ -4,10 +4,9 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::metadata_minifier::MetadataMinifier; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - Countable, InvalidArgumentException, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, - LogicException, PHP_EOL, PhpMixed, RuntimeException, UnexpectedValueException, - extension_loaded, hash, http_build_query, in_array, json_decode, parse_url_all, realpath, - strtolower, strtr, urlencode, var_export, + Countable, InvalidArgumentException, LogicException, PHP_EOL, PhpMixed, RuntimeException, + UnexpectedValueException, extension_loaded, hash, http_build_query, in_array, json_decode, + parse_url_all, realpath, strtolower, strtr, urlencode, var_export, }; use shirabe_semver::compiling_matcher::CompilingMatcher; @@ -22,6 +21,7 @@ use crate::downloader::TransportException; use crate::event_dispatcher::EventDispatcher; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::package::BasePackageHandle; use crate::package::PackageInterface; @@ -2976,7 +2976,10 @@ impl ComposerRepository { .map(|(k, v)| (k.clone(), Box::new(v.clone()))) .collect(), ); - json = JsonFile::encode(&as_mixed, 0); + json = JsonFile::encode_with_options( + &as_mixed, + JsonEncodeOptions::none(), + ); } } self.cache.write(ck, &json); @@ -3168,7 +3171,7 @@ impl ComposerRepository { .map(|(k, v)| (k.clone(), Box::new(v.clone()))) .collect(), ); - json = JsonFile::encode(&as_mixed, 0); + json = JsonFile::encode_with_options(&as_mixed, JsonEncodeOptions::none()); } if !self.cache.is_read_only() { self.cache.write(cache_key, &json); @@ -3346,7 +3349,13 @@ impl ComposerRepository { .map(|(k, v)| (k.clone(), Box::new(v.clone()))) .collect(), ); - json = JsonFile::encode(&as_mixed, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + json = JsonFile::encode_with_options( + &as_mixed, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, + ); } if !self.cache.is_read_only() { self.cache.write(cache_key, &json); diff --git a/crates/shirabe/src/repository/vcs/forgejo_driver.rs b/crates/shirabe/src/repository/vcs/forgejo_driver.rs index d5c4a89..ca7e9dd 100644 --- a/crates/shirabe/src/repository/vcs/forgejo_driver.rs +++ b/crates/shirabe/src/repository/vcs/forgejo_driver.rs @@ -13,6 +13,7 @@ use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::GitDriver; use crate::repository::vcs::VcsDriverBase; @@ -341,17 +342,17 @@ impl ForgejoDriver { )?; if self.inner.should_cache(identifier) { if let Some(ref composer_map) = c { - // TODO(phase-b): JsonFile::encode_with_options does not exist; use encode - let encoded = JsonFile::encode( + let encoded = JsonFile::encode_with_options( &PhpMixed::Array( composer_map .iter() .map(|(k, v)| (k.clone(), Box::new(v.clone()))) .collect(), ), - (shirabe_php_shim::JSON_UNESCAPED_UNICODE - | shirabe_php_shim::JSON_UNESCAPED_SLASHES) - as i64, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ); self.inner .cache diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs index 8a0dffd..85288bd 100644 --- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs @@ -16,6 +16,7 @@ use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::GitDriver; use crate::repository::vcs::VcsDriverBase; @@ -264,7 +265,7 @@ impl GitBitbucketDriver { if self.inner.should_cache(identifier) { self.inner.cache.as_mut().unwrap().write( identifier, - &JsonFile::encode_with_indent( + &JsonFile::encode_with_options( &PhpMixed::Array( composer .clone() @@ -273,9 +274,10 @@ impl GitBitbucketDriver { .map(|(k, v)| (k, Box::new(v))) .collect(), ), - shirabe_php_shim::JSON_UNESCAPED_UNICODE - | shirabe_php_shim::JSON_UNESCAPED_SLASHES, - JsonFile::INDENT_DEFAULT, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ), )?; } diff --git a/crates/shirabe/src/repository/vcs/github_driver.rs b/crates/shirabe/src/repository/vcs/github_driver.rs index 0b9066b..076f4a0 100644 --- a/crates/shirabe/src/repository/vcs/github_driver.rs +++ b/crates/shirabe/src/repository/vcs/github_driver.rs @@ -16,6 +16,7 @@ use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::GitDriver; use crate::repository::vcs::VcsDriverBase; @@ -278,10 +279,12 @@ impl GitHubDriver { self.inner.cache.as_mut().map(|c| { c.write( identifier, - &JsonFile::encode( + &JsonFile::encode_with_options( &php_value, - shirabe_php_shim::JSON_UNESCAPED_UNICODE - | shirabe_php_shim::JSON_UNESCAPED_SLASHES, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ), ) }); diff --git a/crates/shirabe/src/repository/vcs/gitlab_driver.rs b/crates/shirabe/src/repository/vcs/gitlab_driver.rs index e8701bf..6f2c4e9 100644 --- a/crates/shirabe/src/repository/vcs/gitlab_driver.rs +++ b/crates/shirabe/src/repository/vcs/gitlab_driver.rs @@ -16,6 +16,7 @@ use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::GitDriver; use crate::repository::vcs::VcsDriverBase; @@ -276,7 +277,7 @@ impl GitLabDriver { self.inner.cache.as_mut().map(|c| { c.write( identifier, - &JsonFile::encode( + &JsonFile::encode_with_options( &PhpMixed::Array( composer_map .clone() @@ -284,8 +285,10 @@ impl GitLabDriver { .map(|(k, v)| (k, Box::new(v))) .collect(), ), - shirabe_php_shim::JSON_UNESCAPED_UNICODE - | shirabe_php_shim::JSON_UNESCAPED_SLASHES, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ), ) }); diff --git a/crates/shirabe/src/repository/vcs/svn_driver.rs b/crates/shirabe/src/repository/vcs/svn_driver.rs index 23ea703..fc5abb9 100644 --- a/crates/shirabe/src/repository/vcs/svn_driver.rs +++ b/crates/shirabe/src/repository/vcs/svn_driver.rs @@ -5,14 +5,15 @@ use chrono::{DateTime, TimeZone, Utc}; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, RuntimeException, array_key_exists, - is_array, max, sprintf, stripos, strrpos, strtr, substr, trim, + PhpMixed, RuntimeException, array_key_exists, is_array, max, sprintf, stripos, strrpos, strtr, + substr, trim, }; use crate::cache::Cache; use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::VcsDriverBase; use crate::util::Filesystem; @@ -203,12 +204,15 @@ impl SvnDriver { }; if self.should_cache(identifier) { - let encoded = JsonFile::encode( + let encoded = JsonFile::encode_with_options( &composer .clone() .map(PhpMixed::from) .unwrap_or(PhpMixed::Null), - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ); self.inner .cache diff --git a/crates/shirabe/src/repository/vcs/vcs_driver.rs b/crates/shirabe/src/repository/vcs/vcs_driver.rs index 15dd722..8c3b571 100644 --- a/crates/shirabe/src/repository/vcs/vcs_driver.rs +++ b/crates/shirabe/src/repository/vcs/vcs_driver.rs @@ -3,14 +3,13 @@ use chrono::{DateTime, Utc}; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; -use shirabe_php_shim::{ - JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, extension_loaded, -}; +use shirabe_php_shim::{PhpMixed, extension_loaded}; use crate::cache::Cache; use crate::config::Config; use crate::downloader::TransportException; use crate::io::IOInterface; +use crate::json::JsonEncodeOptions; use crate::json::JsonFile; use crate::repository::vcs::VcsDriverInterface; use crate::util::Filesystem; @@ -188,9 +187,12 @@ pub trait VcsDriver: VcsDriverInterface { .map(|(k, v)| (k.clone(), Box::new(v.clone()))) .collect(), ); - let encoded = JsonFile::encode( + let encoded = JsonFile::encode_with_options( &composer_mixed, - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, + JsonEncodeOptions { + pretty_print: false, + ..Default::default() + }, ); self.cache_mut().map(|c| c.write(identifier, &encoded)); } |
