aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--crates/shirabe-php-shim/Cargo.toml1
-rw-r--r--crates/shirabe-php-shim/src/lib.rs49
-rw-r--r--crates/shirabe/src/json/json_file.rs7
-rw-r--r--crates/shirabe/src/json/json_manipulator.rs68
5 files changed, 77 insertions, 49 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 55fac3c..92e8cf8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1012,6 +1012,7 @@ version = "0.0.1"
dependencies = [
"anyhow",
"indexmap",
+ "serde",
"zip",
]
diff --git a/crates/shirabe-php-shim/Cargo.toml b/crates/shirabe-php-shim/Cargo.toml
index 881a979..30e132f 100644
--- a/crates/shirabe-php-shim/Cargo.toml
+++ b/crates/shirabe-php-shim/Cargo.toml
@@ -6,6 +6,7 @@ edition.workspace = true
[dependencies]
anyhow.workspace = true
indexmap.workspace = true
+serde.workspace = true
zip.workspace = true
[lints]
diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs
index cb487a2..77e51ac 100644
--- a/crates/shirabe-php-shim/src/lib.rs
+++ b/crates/shirabe-php-shim/src/lib.rs
@@ -13,6 +13,51 @@ pub enum PhpMixed {
Object(ArrayObject),
}
+impl serde::Serialize for PhpMixed {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ use serde::ser::{SerializeMap, SerializeSeq};
+ match self {
+ PhpMixed::Null => serializer.serialize_none(),
+ PhpMixed::Bool(b) => serializer.serialize_bool(*b),
+ PhpMixed::Int(i) => serializer.serialize_i64(*i),
+ PhpMixed::Float(f) => serializer.serialize_f64(*f),
+ PhpMixed::String(s) => serializer.serialize_str(s),
+ PhpMixed::List(items) => {
+ let mut seq = serializer.serialize_seq(Some(items.len()))?;
+ for item in items {
+ seq.serialize_element(item)?;
+ }
+ seq.end()
+ }
+ PhpMixed::Array(entries) => {
+ let mut map = serializer.serialize_map(Some(entries.len()))?;
+ for (k, v) in entries {
+ map.serialize_entry(k, v)?;
+ }
+ map.end()
+ }
+ PhpMixed::Object(object) => object.serialize(serializer),
+ }
+ }
+}
+
+impl serde::Serialize for ArrayObject {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ use serde::ser::SerializeMap;
+ let mut map = serializer.serialize_map(Some(self.data.len()))?;
+ for (k, v) in &self.data {
+ map.serialize_entry(k, v)?;
+ }
+ map.end()
+ }
+}
+
impl PhpMixed {
pub fn as_bool(&self) -> Option<bool> {
match self {
@@ -815,7 +860,7 @@ pub const JSON_UNESCAPED_SLASHES: i64 = 64;
pub const JSON_PRETTY_PRINT: i64 = 128;
pub const JSON_THROW_ON_ERROR: i64 = 4194304;
-pub fn json_encode(_value: &PhpMixed) -> Option<String> {
+pub fn json_encode<T: serde::Serialize + ?Sized>(_value: &T) -> Option<String> {
todo!()
}
@@ -1078,7 +1123,7 @@ pub fn random_int(_min: i64, _max: i64) -> i64 {
todo!()
}
-pub fn json_encode_ex(_value: &PhpMixed, _flags: i64) -> Option<String> {
+pub fn json_encode_ex<T: serde::Serialize + ?Sized>(_value: &T, _flags: i64) -> Option<String> {
todo!()
}
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index 06f3946..0aa44fc 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -426,11 +426,14 @@ impl JsonFile {
Ok(true)
}
- pub fn encode(data: &PhpMixed) -> String {
+ pub fn encode<T: serde::Serialize + ?Sized>(data: &T) -> String {
Self::encode_with_options(data, JsonEncodeOptions::default())
}
- pub fn encode_with_options(data: &PhpMixed, options: JsonEncodeOptions) -> String {
+ pub fn encode_with_options<T: serde::Serialize + ?Sized>(
+ data: &T,
+ options: JsonEncodeOptions,
+ ) -> String {
let json = json_encode_ex(data, options.to_flags());
let json = match json {
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs
index d907e91..0fd2053 100644
--- a/crates/shirabe/src/json/json_manipulator.rs
+++ b/crates/shirabe/src/json/json_manipulator.rs
@@ -88,10 +88,7 @@ impl JsonManipulator {
let regex = format!(
"{{{}^(?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())),
- None
- ),
+ preg_quote(&JsonFile::encode(r#type), None),
);
let mut matches: IndexMap<String, String> = IndexMap::new();
if !Preg::is_match_named(&regex, &self.contents, &mut matches).unwrap_or(false) {
@@ -126,11 +123,7 @@ impl JsonManipulator {
move |m: &IndexMap<CaptureKey, String>| -> String {
format!(
"{}{}\"{}\"",
- JsonFile::encode(&PhpMixed::String(str_replace(
- "\\/",
- "/",
- &existing_owned
- ))),
+ JsonFile::encode(&str_replace("\\/", "/", &existing_owned)),
m.get(&CaptureKey::ByName("separator".to_string()))
.cloned()
.unwrap_or_default(),
@@ -162,8 +155,8 @@ impl JsonManipulator {
self.newline,
self.indent,
self.indent,
- JsonFile::encode(&PhpMixed::String(package.to_string())),
- JsonFile::encode(&PhpMixed::String(constraint.to_string())),
+ JsonFile::encode(package),
+ JsonFile::encode(constraint),
groups_1
),
"\\$",
@@ -177,8 +170,8 @@ impl JsonManipulator {
self.newline,
self.indent,
self.indent,
- JsonFile::encode(&PhpMixed::String(package.to_string())),
- JsonFile::encode(&PhpMixed::String(constraint.to_string())),
+ JsonFile::encode(package),
+ JsonFile::encode(constraint),
self.newline,
self.indent
);
@@ -405,7 +398,7 @@ impl JsonManipulator {
.get(&CaptureKey::ByName("start".to_string()))
.cloned()
.unwrap_or_default(),
- JsonFile::encode(&PhpMixed::String(url_owned.clone())),
+ JsonFile::encode(&url_owned),
repository_matches
.get(&CaptureKey::ByName("end".to_string()))
.cloned()
@@ -681,10 +674,7 @@ impl JsonManipulator {
let node_regex = format!(
"{{{}^(?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())),
- None
- )
+ preg_quote(&JsonFile::encode(main_node), None)
);
let mut match_map: IndexMap<String, String> = IndexMap::new();
@@ -792,7 +782,7 @@ impl JsonManipulator {
self.newline,
self.indent,
self.indent,
- JsonFile::encode(&PhpMixed::String(name_owned.clone())),
+ JsonFile::encode(&name_owned),
self.format(&value_local, 1, false)?,
whitespace
),
@@ -808,7 +798,7 @@ impl JsonManipulator {
&format!(
"{{{}{}: {},{}{}{}",
whitespace,
- JsonFile::encode(&PhpMixed::String(name_owned.clone())),
+ JsonFile::encode(&name_owned),
self.format(&value_local, 1, false)?,
self.newline,
self.indent,
@@ -833,7 +823,7 @@ impl JsonManipulator {
self.newline,
self.indent,
self.indent,
- JsonFile::encode(&PhpMixed::String(name_owned.clone())),
+ JsonFile::encode(&name_owned),
self.format(&value_local, 1, false)?,
whitespace
);
@@ -881,10 +871,7 @@ impl JsonManipulator {
let node_regex = format!(
"{{{}^(?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())),
- None
- )
+ preg_quote(&JsonFile::encode(main_node), None)
);
let mut match_map: IndexMap<String, String> = IndexMap::new();
let match_result = match Preg::is_match_named(&node_regex, &self.contents, &mut match_map) {
@@ -1146,10 +1133,7 @@ impl JsonManipulator {
let node_regex = format!(
"{{{}^(?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())),
- None
- )
+ preg_quote(&JsonFile::encode(main_node), None)
);
let mut match_map: IndexMap<String, String> = IndexMap::new();
@@ -1313,10 +1297,7 @@ impl JsonManipulator {
let node_regex = format!(
"{{{}^(?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())),
- None
- )
+ preg_quote(&JsonFile::encode(main_node), None)
);
let mut match_map: IndexMap<String, String> = IndexMap::new();
@@ -1416,10 +1397,7 @@ impl JsonManipulator {
let node_regex = format!(
"{{{}^(?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())),
- None
- )
+ preg_quote(&JsonFile::encode(main_node), None)
);
let mut match_map: IndexMap<String, String> = IndexMap::new();
let match_result = match Preg::is_match_named(&node_regex, &self.contents, &mut match_map) {
@@ -1510,7 +1488,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())), None)
+ preg_quote(&JsonFile::encode(key), None)
);
let mut matches: IndexMap<String, String> = IndexMap::new();
if decoded.as_array().and_then(|a| a.get(key)).is_some()
@@ -1525,7 +1503,7 @@ impl JsonManipulator {
self.contents = format!(
"{}{}: {}{}",
matches.get("start").cloned().unwrap_or_default(),
- JsonFile::encode(&PhpMixed::String(key.to_string())),
+ JsonFile::encode(key),
content,
matches.get("end").cloned().unwrap_or_default()
);
@@ -1549,7 +1527,7 @@ impl JsonManipulator {
",{}{}{}: {}{}}}",
self.newline,
self.indent,
- JsonFile::encode(&PhpMixed::String(key.to_string())),
+ JsonFile::encode(key),
content,
self.newline
),
@@ -1568,7 +1546,7 @@ impl JsonManipulator {
&format!(
"{}{}: {}{}}}",
self.indent,
- JsonFile::encode(&PhpMixed::String(key.to_string())),
+ JsonFile::encode(key),
content,
self.newline
),
@@ -1592,7 +1570,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())), None)
+ preg_quote(&JsonFile::encode(key), None)
);
let mut matches: IndexMap<String, String> = IndexMap::new();
if Preg::is_match_named(&regex, &self.contents, &mut matches).unwrap_or(false) {
@@ -1636,7 +1614,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())), None)
+ preg_quote(&JsonFile::encode(key), None)
);
let mut matches: IndexMap<String, String> = IndexMap::new();
if Preg::is_match_named(&regex, &self.contents, &mut matches).unwrap_or(false) {
@@ -1722,7 +1700,7 @@ impl JsonManipulator {
elems.push(format!(
"{}{}: {}",
str_repeat(&self.indent, (depth + 2) as usize),
- JsonFile::encode(&PhpMixed::String(key.clone())),
+ JsonFile::encode(key),
self.format(val, depth + 1, false)?
));
}
@@ -1795,7 +1773,7 @@ impl ManipulatorFormatter {
elems.push(format!(
"{}{}: {}",
str_repeat(&self.indent, (depth + 2) as usize),
- JsonFile::encode(&PhpMixed::String(key.clone())),
+ JsonFile::encode(key),
self.format(val, depth + 1, false)?
));
}