From 5aaa92f760a975c6617b5d1fb7af8e58fed127b7 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 08:20:53 +0900 Subject: refactor(check-platform-reqs-command): model the JSON output as structs Replace the hand-built `IndexMap` rows behind `check-platform-reqs --format=json` with structs deriving `serde::Serialize`, so key order and the `null` values PHP keeps for a missing failed requirement or provider live in the type rather than being rebuilt at every insertion site. Test the provider against its raw value the way PHP's `$provider === ''` does, instead of against the `strip_tags` result. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/command/check_platform_reqs_command.rs | 84 +++++++++------------- 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/crates/shirabe/src/command/check_platform_reqs_command.rs b/crates/shirabe/src/command/check_platform_reqs_command.rs index 3742cdac..d1cbf33c 100644 --- a/crates/shirabe/src/command/check_platform_reqs_command.rs +++ b/crates/shirabe/src/command/check_platform_reqs_command.rs @@ -27,6 +27,24 @@ struct CheckResult { provider: String, } +/// Shape of one entry of the `check-platform-reqs --format=json` output. +#[derive(Debug, serde::Serialize)] +struct PlatformRequirementJson { + name: String, + version: String, + status: String, + failed_requirement: Option, + provider: Option, +} + +#[derive(Debug, serde::Serialize)] +struct FailedRequirementJson { + source: String, + r#type: String, + target: String, + constraint: String, +} + #[derive(Debug)] pub struct CheckPlatformReqsCommand { base_command_data: BaseCommandData, @@ -63,61 +81,27 @@ impl CheckPlatformReqsCommand { let io = self.get_io(); if format == "json" { - let rows: Vec = results + let rows: Vec = results .iter() - .map(|result| { - let mut row = IndexMap::new(); - row.insert( - "name".to_string(), - PhpMixed::String(result.platform_package.clone()), - ); - row.insert( - "version".to_string(), - PhpMixed::String(result.version.clone()), - ); - row.insert( - "status".to_string(), - PhpMixed::String(strip_tags(&result.status)), - ); - if let Some(link) = &result.link { - let mut failed_req = IndexMap::new(); - failed_req.insert( - "source".to_string(), - PhpMixed::String(link.get_source().to_string()), - ); - failed_req.insert( - "type".to_string(), - PhpMixed::String(link.get_description().to_string()), - ); - failed_req.insert( - "target".to_string(), - PhpMixed::String(link.get_target().to_string()), - ); - failed_req.insert( - "constraint".to_string(), - PhpMixed::String(link.get_pretty_constraint().to_string()), - ); - row.insert( - "failed_requirement".to_string(), - PhpMixed::Array(failed_req), - ); + .map(|result| PlatformRequirementJson { + name: result.platform_package.clone(), + version: result.version.clone(), + status: strip_tags(&result.status), + failed_requirement: result.link.as_ref().map(|link| FailedRequirementJson { + source: link.get_source().to_string(), + r#type: link.get_description().to_string(), + target: link.get_target().to_string(), + constraint: link.get_pretty_constraint().to_string(), + }), + provider: if result.provider.is_empty() { + None } else { - row.insert("failed_requirement".to_string(), PhpMixed::Null); - } - let provider_str = strip_tags(&result.provider); - row.insert( - "provider".to_string(), - if provider_str.is_empty() { - PhpMixed::Null - } else { - PhpMixed::String(provider_str) - }, - ); - PhpMixed::Array(row) + Some(strip_tags(&result.provider)) + }, }) .collect(); - io.write(&JsonFile::encode(&PhpMixed::List(rows))?); + io.write(&JsonFile::encode(&rows)?); } else { let rows: Vec = results .iter() -- cgit v1.3.1-4-g156e