xml2json-rb 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3136f4ff83c6e8851b4fbfcb2bc343f3cf4a453a27dc566c63f3b3d661364c84
4
- data.tar.gz: 8a98b15dacac13a9fea305985e74325eeb173fe7d8d1ce19ce4e8114d98c15a2
3
+ metadata.gz: 894ff6fe88c1e4a0e7bbb426419954f98413f568f8ac361784e60537074033fd
4
+ data.tar.gz: face561e86ac59fb767535689377e361f171574b77722ba55a55056c96630767
5
5
  SHA512:
6
- metadata.gz: 96c29af1693eb23580ac8f42bca2627b8b46aa57d5fe776e4b7171263e17f1b92fee00bec36668816bdca5f9a24a34e5440c8ad4ca24032a737173c5bbda591a
7
- data.tar.gz: 05d2d14fe2c6195eb8d5c00a9e5c0e4b2f875c6f4f119f0c0e12257299b8ece2dbecbaa22102474081cc3a1d18fa7ebb991f5e47781ac9b77e49b0a8d0adfba2
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.0"
4
+ VERSION = "0.2.1"
5
5
  end
Binary file
data/lib/xml2json.rb CHANGED
@@ -3,7 +3,32 @@
3
3
  require_relative "xml2json/version"
4
4
  require_relative "xml2json/xml2json"
5
5
 
6
+ # @see https://github.com/novcn/xml2json-rs docs for the wrapped library
6
7
  module Xml2Json
7
- class Error < StandardError; end
8
- # Your code goes here...
8
+ # @!parse [ruby]
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
+ #
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
21
+ # # @return [String] XML string
22
+ # # @raise [RuntimeError] if the input string is invalid
23
+ # def self.build(json_s, opts: nil); end
24
+ # end
25
+ #
26
+ # module Json
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
30
+ # # @return [String] JSON string
31
+ # # @raise [RuntimeError] if the input string is invalid
32
+ # def self.build(xml, opts: nil); end
33
+ # end
9
34
  end
@@ -0,0 +1,5 @@
1
+ module Xml2Json
2
+ module Json
3
+ def self.build: (xml: String, opts: Hash[Symbol, Object]?) -> String
4
+ end
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
@@ -0,0 +1,5 @@
1
+ module Xml2Json
2
+ module Xml
3
+ def self.build: (json_s: String, opts: Hash[Symbol, Object]?) -> String
4
+ end
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.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-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
@@ -24,7 +24,91 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description:
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rackup
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: puma
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: This gem helps to convert xml strings to json and vise versa
28
112
  email:
29
113
  - uvlad7@gmail.com
30
114
  executables: []
@@ -40,6 +124,10 @@ files:
40
124
  - lib/xml2json.rb
41
125
  - lib/xml2json/version.rb
42
126
  - lib/xml2json/xml2json.so
127
+ - sig/xml2_json/json.rbs
128
+ - sig/xml2_json/xml.rbs
129
+ - sig/xml2_json/xml/encoding.rbs
130
+ - sig/xml2_json/xml/version.rbs
43
131
  - sig/xml2json.rbs
44
132
  homepage: https://github.com/uvlad7/xml2json-rb
45
133
  licenses:
@@ -48,6 +136,7 @@ metadata:
48
136
  homepage_uri: https://github.com/uvlad7/xml2json-rb
49
137
  source_code_uri: https://github.com/uvlad7/xml2json-rb
50
138
  changelog_uri: https://github.com/uvlad7/xml2json-rb/blob/main/CHANGELOG.md
139
+ documentation_uri: https://rubydoc.info/gems/xml2json-rb/0.2.1
51
140
  post_install_message:
52
141
  rdoc_options: []
53
142
  require_paths: