xml2json-rb 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +1 -1
- data/ext/xml2json/Cargo.toml +1 -1
- data/ext/xml2json/src/lib.rs +172 -33
- data/lib/xml2json/version.rb +1 -1
- data/lib/xml2json/xml2json.so +0 -0
- data/lib/xml2json.rb +15 -2
- data/sig/xml2_json/json.rbs +1 -1
- data/sig/xml2_json/xml/encoding.rbs +7 -0
- data/sig/xml2_json/xml/version.rbs +8 -0
- data/sig/xml2_json/xml.rbs +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 894ff6fe88c1e4a0e7bbb426419954f98413f568f8ac361784e60537074033fd
|
4
|
+
data.tar.gz: face561e86ac59fb767535689377e361f171574b77722ba55a55056c96630767
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d3917434cd6824082cb8256ef752e10af0153d29da3b00365f0ed5ceebcef5626e422998b52a7f1a3c63b44ab2b49586f079557e1cb4888a08cf2d4f128c9bc
|
7
|
+
data.tar.gz: 36cf6993a5ca3f586357b8d46b9a038ad53cab83eae759fa1506ab58989cdd3c97dad69fe15a44f3ecea49eb3ac7287f35a75a01582e3bd13d493e58029a523a
|
data/Cargo.lock
CHANGED
data/ext/xml2json/Cargo.toml
CHANGED
data/ext/xml2json/src/lib.rs
CHANGED
@@ -1,47 +1,182 @@
|
|
1
|
-
use magnus::{define_module, function, prelude::*, Error};
|
2
|
-
use
|
1
|
+
use magnus::{define_module, function, prelude::*, Error, Value};
|
2
|
+
use magnus::scan_args::{scan_args};
|
3
|
+
use xml2json_rs::{XmlBuilder, JsonBuilder, JsonConfig, XmlConfig, Declaration, Version, Encoding, Indentation};
|
3
4
|
|
4
5
|
// fn hello(subject: String) -> String {
|
5
6
|
// format!("Hello from Rust, {}!", subject)
|
6
7
|
// }
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
#[macro_export]
|
10
|
+
macro_rules! set_arg {
|
11
|
+
($config:expr, $opts_hash:expr, $arg:ident, $arg_type:ident) => (
|
12
|
+
let arg_value = $opts_hash.lookup::<_, Option<$arg_type>>(magnus::Symbol::new(stringify!($arg)))?;
|
13
|
+
if arg_value.is_some() {
|
14
|
+
$config.$arg(arg_value.unwrap());
|
15
|
+
}
|
16
|
+
);
|
17
|
+
}
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
// self
|
16
|
-
// }
|
17
|
-
//
|
18
|
-
// fn build(self, json_s: String) -> Result<String, Error> {
|
19
|
-
// build_xml_impl(json_s, Some(self))
|
20
|
-
// }
|
21
|
-
// }
|
19
|
+
fn map_xml2json_err(error: xml2json_rs::X2JError) -> Error {
|
20
|
+
Error::new(magnus::exception::runtime_error(), error.details())
|
21
|
+
}
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
fn build_xml(args: &[Value]) -> Result<String, Error> {
|
24
|
+
let args = scan_args::<_, _, (), (), _, ()>(args)?;
|
25
|
+
let (json_s, ): (String, ) = args.required;
|
26
|
+
let (opts, ): (Option<magnus::RHash>, ) = args.optional;
|
27
|
+
let _: () = args.keywords;
|
26
28
|
|
27
|
-
|
28
|
-
//
|
29
|
-
//
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
let mut xml_builder: XmlBuilder;
|
30
|
+
if opts.is_some() { // yep, even if it's an empty hash
|
31
|
+
// println!("{}", opts.unwrap().to_string());
|
32
|
+
let opts_hash = opts.unwrap();
|
33
|
+
let mut config = XmlConfig::new();
|
34
|
+
set_arg!(config, opts_hash, root_name, String);
|
35
|
+
set_arg!(config, opts_hash, attrkey, String);
|
36
|
+
set_arg!(config, opts_hash, charkey, String);
|
37
|
+
|
38
|
+
let decl_version = opts_hash.lookup::<_, Option<Value>>(magnus::Symbol::new("version"))?;
|
39
|
+
let decl_encoding = opts_hash.lookup::<_, Option<Value>>(magnus::Symbol::new("encoding"))?;
|
40
|
+
let decl_standalone = opts_hash.lookup::<_, Option<bool>>(magnus::Symbol::new("standalone"))?;
|
41
|
+
if decl_version.is_some()
|
42
|
+
|| decl_encoding.is_some()
|
43
|
+
|| decl_standalone.is_some()
|
44
|
+
{ // something is specified
|
45
|
+
// I didn't find a way to get defaults without copying them
|
46
|
+
let decl_version_val;
|
47
|
+
if decl_version.is_some() {
|
48
|
+
let decl_version_str = unsafe { decl_version.unwrap().to_s() }?.into_owned();
|
49
|
+
decl_version_val = Version::try_from(decl_version_str.as_str())
|
50
|
+
.map_err(map_xml2json_err)?;
|
51
|
+
} else { decl_version_val = Version::XML10; }
|
52
|
+
let decl_encoding_val;
|
53
|
+
if decl_encoding.is_some() {
|
54
|
+
let decl_encoding_str = unsafe { decl_encoding.unwrap().to_s() }?.into_owned();
|
55
|
+
decl_encoding_val = Some(Encoding::try_from(decl_encoding_str.as_str())
|
56
|
+
.map_err(map_xml2json_err)?);
|
57
|
+
} else { decl_encoding_val = None; }
|
58
|
+
|
59
|
+
let decl = Declaration::new(
|
60
|
+
decl_version_val,
|
61
|
+
decl_encoding_val,
|
62
|
+
decl_standalone,
|
63
|
+
);
|
64
|
+
config.decl(decl);
|
65
|
+
}
|
66
|
+
|
67
|
+
let indent = opts_hash.lookup::<_, Option<bool>>(magnus::Symbol::new("indent"))?;
|
68
|
+
if indent.unwrap_or(true) {
|
69
|
+
let indent_char = opts_hash.lookup::<_, Option<char>>(magnus::Symbol::new("indent_char"))?;
|
70
|
+
let indent_size = opts_hash.lookup::<_, Option<usize>>(magnus::Symbol::new("indent_size"))?;
|
71
|
+
if indent_char.is_some()
|
72
|
+
|| indent_size.is_some()
|
73
|
+
{
|
74
|
+
let indent_char_val: u8;
|
75
|
+
if indent_char.is_some() {
|
76
|
+
indent_char_val = u8::try_from(indent_char.unwrap()).map_err(|error| Error::new(magnus::exception::type_error(), error.to_string()))?;
|
77
|
+
} else { indent_char_val = b' '; }
|
78
|
+
|
79
|
+
let intent = Indentation::new(
|
80
|
+
indent_char_val,
|
81
|
+
indent_size.unwrap_or(2),
|
82
|
+
);
|
83
|
+
config.rendering(intent);
|
84
|
+
} else if indent.unwrap_or(false) {
|
85
|
+
// because Indentation::default is not the same as not calling config.rendering at all;
|
86
|
+
config.rendering(Indentation::default());
|
87
|
+
}
|
88
|
+
}
|
89
|
+
xml_builder = config.finalize();
|
90
|
+
} else { xml_builder = XmlBuilder::default(); }
|
33
91
|
|
34
|
-
fn build_xml(json_s: String) -> Result<String, Error> {
|
35
|
-
let mut xml_builder = XmlBuilder::default();
|
36
92
|
xml_builder.build_from_json_string(&json_s).or_else(|error| {
|
37
|
-
Err(
|
93
|
+
Err(map_xml2json_err(error))
|
38
94
|
})
|
39
95
|
}
|
40
96
|
|
41
|
-
fn build_json(
|
42
|
-
|
97
|
+
fn build_json(args: &[Value]) -> Result<String, Error> {
|
98
|
+
// https://docs.rs/magnus/latest/magnus/scan_args/fn.scan_args.html
|
99
|
+
let args = scan_args::<_, _, (), (), _, ()>(args)?;
|
100
|
+
let (xml, ): (String, ) = args.required;
|
101
|
+
let (opts, ): (Option<magnus::RHash>, ) = args.optional;
|
102
|
+
// let _: () = args.optional;
|
103
|
+
let _: () = args.keywords;
|
104
|
+
|
105
|
+
// Impossible, too long
|
106
|
+
// let kw = get_kwargs::<_, (), (
|
107
|
+
// Option<String>, // charkey
|
108
|
+
// Option<String>, // attrkey
|
109
|
+
// Option<String>, // empty_tag
|
110
|
+
// Option<bool>, // explicit_root
|
111
|
+
// Option<bool>, // trim
|
112
|
+
// Option<bool>, // ignore_attrs
|
113
|
+
// Option<bool>, // merge_attrs
|
114
|
+
// Option<bool>, // normalize_text
|
115
|
+
// Option<bool>, // lowercase_tags
|
116
|
+
// Option<bool>, // explicit_array
|
117
|
+
// Option<bool>, // explicit_charkey
|
118
|
+
// ), ()>(args.keywords, &[], &[
|
119
|
+
// "charkey",
|
120
|
+
// "attrkey",
|
121
|
+
// "empty_tag",
|
122
|
+
// "explicit_root",
|
123
|
+
// "trim",
|
124
|
+
// "ignore_attrs",
|
125
|
+
// "merge_attrs",
|
126
|
+
// "normalize_text",
|
127
|
+
// "lowercase_tags",
|
128
|
+
// "explicit_array",
|
129
|
+
// "explicit_charkey",
|
130
|
+
// ])?;
|
131
|
+
// let (
|
132
|
+
// charkey,
|
133
|
+
// attrkey,
|
134
|
+
// empty_tag,
|
135
|
+
// explicit_root,
|
136
|
+
// trim,
|
137
|
+
// ignore_attrs,
|
138
|
+
// merge_attrs,
|
139
|
+
// normalize_text,
|
140
|
+
// lowercase_tags,
|
141
|
+
// explicit_array,
|
142
|
+
// explicit_charkey,
|
143
|
+
// ) = kw.optional;
|
144
|
+
// if charkey.is_some()
|
145
|
+
// || attrkey.is_some()
|
146
|
+
// || empty_tag.is_some()
|
147
|
+
// || explicit_root.is_some()
|
148
|
+
// || trim.is_some()
|
149
|
+
// || ignore_attrs.is_some()
|
150
|
+
// || merge_attrs.is_some()
|
151
|
+
// || normalize_text.is_some()
|
152
|
+
// || lowercase_tags.is_some()
|
153
|
+
// || explicit_array.is_some()
|
154
|
+
// || explicit_charkey.is_some()
|
155
|
+
// {
|
156
|
+
// println!("any")
|
157
|
+
// } else { println!("none") }
|
158
|
+
|
159
|
+
let json_builder: JsonBuilder;
|
160
|
+
if opts.is_some() { // yep, even if it's an empty hash
|
161
|
+
// println!("{}", opts.unwrap().to_string());
|
162
|
+
let opts_hash = opts.unwrap();
|
163
|
+
let mut config = JsonConfig::new();
|
164
|
+
set_arg!(config, opts_hash, charkey, String);
|
165
|
+
set_arg!(config, opts_hash, attrkey, String);
|
166
|
+
set_arg!(config, opts_hash, empty_tag, String);
|
167
|
+
set_arg!(config, opts_hash, explicit_root, bool);
|
168
|
+
set_arg!(config, opts_hash, trim, bool);
|
169
|
+
set_arg!(config, opts_hash, ignore_attrs, bool);
|
170
|
+
set_arg!(config, opts_hash, merge_attrs, bool);
|
171
|
+
set_arg!(config, opts_hash, normalize_text, bool);
|
172
|
+
set_arg!(config, opts_hash, lowercase_tags, bool);
|
173
|
+
set_arg!(config, opts_hash, explicit_array, bool);
|
174
|
+
set_arg!(config, opts_hash, explicit_charkey, bool);
|
175
|
+
json_builder = config.finalize();
|
176
|
+
} else { json_builder = JsonBuilder::default(); }
|
177
|
+
|
43
178
|
json_builder.build_string_from_xml(&xml).or_else(|error| {
|
44
|
-
Err(
|
179
|
+
Err(map_xml2json_err(error))
|
45
180
|
})
|
46
181
|
}
|
47
182
|
|
@@ -51,9 +186,13 @@ fn init() -> Result<(), Error> {
|
|
51
186
|
// module.define_singleton_method("hello", function!(hello, 1))?;
|
52
187
|
// It's not possible to wrap XmlBuilder
|
53
188
|
let xml = module.define_module("Xml")?;
|
54
|
-
xml.
|
55
|
-
|
189
|
+
let xml_version = xml.define_module("Version")?;
|
190
|
+
xml_version.const_set("XML10", Version::XML10.to_string())?;
|
191
|
+
xml_version.const_set("XML11", Version::XML11.to_string())?;
|
192
|
+
let xml_encoding = xml.define_module("Encoding")?;
|
193
|
+
xml_encoding.const_set("UTF8", Encoding::UTF8.to_string())?;
|
194
|
+
xml.define_singleton_method("build", function!(build_xml, -1))?;
|
56
195
|
let json = module.define_module("Json")?;
|
57
|
-
json.define_singleton_method("build", function!(build_json, 1))?;
|
196
|
+
json.define_singleton_method("build", function!(build_json, -1))?;
|
58
197
|
Ok(())
|
59
198
|
}
|
data/lib/xml2json/version.rb
CHANGED
data/lib/xml2json/xml2json.so
CHANGED
Binary file
|
data/lib/xml2json.rb
CHANGED
@@ -7,15 +7,28 @@ require_relative "xml2json/xml2json"
|
|
7
7
|
module Xml2Json
|
8
8
|
# @!parse [ruby]
|
9
9
|
# module Xml
|
10
|
+
# module Version
|
11
|
+
# XML10 = "1.0"
|
12
|
+
# XML11 = "1.1"
|
13
|
+
# end
|
14
|
+
# module Encoding
|
15
|
+
# UTF8 = "UTF-8"
|
16
|
+
# end
|
17
|
+
#
|
10
18
|
# # @param json_s [String, #to_str] JSON string
|
19
|
+
# # @param opts [Hash<Symbol, Object>] config params
|
20
|
+
# # @see https://docs.rs/xml2json-rs/1.0.1/xml2json_rs/struct.XmlConfig.html rust doc
|
11
21
|
# # @return [String] XML string
|
12
22
|
# # @raise [RuntimeError] if the input string is invalid
|
13
|
-
# def self.build(json_s); end
|
23
|
+
# def self.build(json_s, opts: nil); end
|
14
24
|
# end
|
25
|
+
#
|
15
26
|
# module Json
|
16
27
|
# # @param xml [String, #to_str] XML string
|
28
|
+
# # @param opts [Hash<Symbol, Object>] config params
|
29
|
+
# # @see https://docs.rs/xml2json-rs/1.0.1/xml2json_rs/struct.JsonConfig.html rust doc
|
17
30
|
# # @return [String] JSON string
|
18
31
|
# # @raise [RuntimeError] if the input string is invalid
|
19
|
-
# def self.build(xml); end
|
32
|
+
# def self.build(xml, opts: nil); end
|
20
33
|
# end
|
21
34
|
end
|
data/sig/xml2_json/json.rbs
CHANGED
data/sig/xml2_json/xml.rbs
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml2json-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- uvlad7
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard-rustdoc
|
@@ -126,6 +126,8 @@ files:
|
|
126
126
|
- lib/xml2json/xml2json.so
|
127
127
|
- sig/xml2_json/json.rbs
|
128
128
|
- sig/xml2_json/xml.rbs
|
129
|
+
- sig/xml2_json/xml/encoding.rbs
|
130
|
+
- sig/xml2_json/xml/version.rbs
|
129
131
|
- sig/xml2json.rbs
|
130
132
|
homepage: https://github.com/uvlad7/xml2json-rb
|
131
133
|
licenses:
|
@@ -134,7 +136,7 @@ metadata:
|
|
134
136
|
homepage_uri: https://github.com/uvlad7/xml2json-rb
|
135
137
|
source_code_uri: https://github.com/uvlad7/xml2json-rb
|
136
138
|
changelog_uri: https://github.com/uvlad7/xml2json-rb/blob/main/CHANGELOG.md
|
137
|
-
documentation_uri: https://rubydoc.info/gems/xml2json-rb/0.
|
139
|
+
documentation_uri: https://rubydoc.info/gems/xml2json-rb/0.2.1
|
138
140
|
post_install_message:
|
139
141
|
rdoc_options: []
|
140
142
|
require_paths:
|