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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b8bf36283e6565f3c5fe13d52a74b42f69435113f2bc8df671b7feefe0e6d58
4
- data.tar.gz: bcc4149800d29bb5ed30e938fa44c0f87bf9e0b5afb8226e8ac3a78961ccad92
3
+ metadata.gz: 894ff6fe88c1e4a0e7bbb426419954f98413f568f8ac361784e60537074033fd
4
+ data.tar.gz: face561e86ac59fb767535689377e361f171574b77722ba55a55056c96630767
5
5
  SHA512:
6
- metadata.gz: cc3cf01a0b18a7a902fa550a0603d23aebc1faa2a3c23dd92097fd0fc59905ec37add860ed930ef0e980df85fb3bf65b253c0d3635f19728d8c2aeb21dc46a3c
7
- data.tar.gz: abcb6321dba81f9e451a7aed4525f09b458b9b5323bcfe2fe39ec5a43ed9a298ddf0f1b6e5254b4f1b71d4922baaaf5e1d50984be9a4db0c6f9bc6ec4fea11cf
6
+ metadata.gz: 2d3917434cd6824082cb8256ef752e10af0153d29da3b00365f0ed5ceebcef5626e422998b52a7f1a3c63b44ab2b49586f079557e1cb4888a08cf2d4f128c9bc
7
+ data.tar.gz: 36cf6993a5ca3f586357b8d46b9a038ad53cab83eae759fa1506ab58989cdd3c97dad69fe15a44f3ecea49eb3ac7287f35a75a01582e3bd13d493e58029a523a
data/Cargo.lock CHANGED
@@ -344,7 +344,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
344
344
 
345
345
  [[package]]
346
346
  name = "xml2json"
347
- version = "0.1.0"
347
+ version = "0.2.1"
348
348
  dependencies = [
349
349
  "magnus",
350
350
  "xml2json-rs",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "xml2json"
3
- version = "0.1.0"
3
+ version = "0.2.1"
4
4
  edition = "2021"
5
5
  authors = ["uvlad7 <uvlad7@gmail.com>"]
6
6
  license = "MIT"
@@ -1,47 +1,182 @@
1
- use magnus::{define_module, function, prelude::*, Error};
2
- use xml2json_rs::{XmlBuilder, JsonBuilder};
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
- // struct RbXmlConfig {
9
- // val: XmlConfig,
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
- // impl RbXmlConfig {
13
- // fn root_name(&mut self, key: Option<String>) -> &mut RbXmlConfig {
14
- // self.val.root_name(key);
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
- // fn build_xml(json_s: String) -> Result<String, Error> {
24
- // build_xml_impl(json_s, None)
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
- // fn build_xml_impl(json_s: String, config: Option<RbXmlConfig>) -> Result<String, Error> {
28
- // let mut xml_builder = config.map_or_else(|| XmlBuilder::default(), |conf| conf.val.finalize());
29
- // xml_builder.build_from_json_string(&json_s).or_else(|error| {
30
- // Err(Error::new(magnus::exception::arg_error(), error.to_string()))
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(Error::new(magnus::exception::runtime_error(), error.details()))
93
+ Err(map_xml2json_err(error))
38
94
  })
39
95
  }
40
96
 
41
- fn build_json(xml: String) -> Result<String, Error> {
42
- let json_builder = JsonBuilder::default();
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(Error::new(magnus::exception::runtime_error(), error.details()))
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.define_singleton_method("build", function!(build_xml, 1))?;
55
- // xml.define_class("Config", magnus::class::object())?;
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
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Xml2Json
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.1"
5
5
  end
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
@@ -1,5 +1,5 @@
1
1
  module Xml2Json
2
2
  module Json
3
- def self.build: (xml: String) -> String
3
+ def self.build: (xml: String, opts: Hash[Symbol, Object]?) -> String
4
4
  end
5
5
  end
@@ -0,0 +1,7 @@
1
+ module Xml2Json
2
+ module Xml
3
+ module Encoding
4
+ UTF8: String
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Xml2Json
2
+ module Xml
3
+ module Version
4
+ XML10: String
5
+ XML11: String
6
+ end
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  module Xml2Json
2
2
  module Xml
3
- def self.build: (json_s: String) -> String
3
+ def self.build: (json_s: String, opts: Hash[Symbol, Object]?) -> String
4
4
  end
5
5
  end
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.1.1
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-09 00:00:00.000000000 Z
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.1.1
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: