rusty_json 1.2.1 → 1.2.2

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
  SHA1:
3
- metadata.gz: 586d28acc06307f6920e376bcae55b025ce42021
4
- data.tar.gz: 6851dd830392776b050b8c14730267a5518e1066
3
+ metadata.gz: 8229cb337a37bcc8564b5a89faea33d555df6baf
4
+ data.tar.gz: a59744de5deffd9f5e4574c9d2851da6b57a7552
5
5
  SHA512:
6
- metadata.gz: 40753f7dd3e28fec338b956250501a9bced6d0b60cced4ae1130328087725fd8ed6620e51a8b7298492a1c9b76a6cc1a587b8b67febb53e39ed675f3e1285338
7
- data.tar.gz: a3810f86f425c2b341527a5f061bbe513ba846f28c22494f1d29b383332dcb1ee5849a03ece019f7edf7a42c76bb88fe842388c4fddb90165ea827f26730a412
6
+ metadata.gz: feb861a20062c7e8fec5dd896dbd3783215568e8730a2c4e2e943f074020fa14876de0ecc19ce00ad8462f550768786d03e7318e6dd2a04dd8672fc95f8f04ee
7
+ data.tar.gz: b83f60923de006bffe464f745bccb756a898950161712ef37c3635b5ff5a59fb77c4ea20dade8b721d296b5b22ae1022823fd666fef90b8892e00e67f22bfc39
@@ -2,13 +2,27 @@ require 'json'
2
2
  require 'set'
3
3
 
4
4
  module RustyJson
5
+ # Parser is the base class that actually parses JSON into Rust structs
6
+ #
7
+ # Example:
8
+ # ```ruby
9
+ # name = 'Test'
10
+ # json = '{"hello": "world"}'
11
+ # parser = Parser.new(name, json)
12
+ # parser.parse
13
+ # ```
14
+
5
15
  class Parser
16
+ # BASE_TYPES are merely Rust base types that we use before adding our own
17
+ # Structs to them.
6
18
  BASE_TYPES = {
7
19
  String => 'String',
8
20
  Fixnum => 'i64',
9
21
  Float => 'f64',
10
22
  }
11
23
 
24
+ # @param name [String] the name of the returned root JSON struct
25
+ # @param json [String] the JSON string to parse into a Rust struct
12
26
  def initialize(name, json)
13
27
  @name = name
14
28
  @json = json
@@ -16,6 +30,8 @@ module RustyJson
16
30
  @structs = Set.new
17
31
  end
18
32
 
33
+ # parse takes the given JSON string and turns it into a string of
34
+ # Rust structs, suitable for use with rustc_serialize.
19
35
  def parse
20
36
  @parsed = JSON.parse(@json)
21
37
  if @parsed.is_a? Hash
@@ -27,6 +43,8 @@ module RustyJson
27
43
  private
28
44
 
29
45
  def parse_name(n)
46
+ n = n.split('_').collect(&:capitalize).join
47
+
30
48
  if @struct_names.include? n
31
49
  i = 2
32
50
  while @struct_names.include? "#{n}_#{i}"
@@ -48,10 +66,11 @@ module RustyJson
48
66
  end
49
67
 
50
68
  def parse_parts(name, values, struct)
69
+ name = name.gsub('-', '_').gsub(':', '_').gsub('__', '_')
51
70
  if values.is_a? Array
52
71
  struct = parse_array(name, values, struct)
53
72
  elsif values.is_a? Hash
54
- n = parse_name(name.split('_').collect(&:capitalize).join)
73
+ n = parse_name(name)
55
74
  @struct_names << n
56
75
  s = possible_new_struct( parse_hash(n, values) )
57
76
  struct.add_value(name, s)
@@ -1,6 +1,16 @@
1
1
  module RustyJson
2
+ #RustyJson:L:RustStruct is the object that will be translated into
3
+ # a Rust struct.
4
+ #
5
+ # @!attribute name
6
+ # @return [String] name of the struct
7
+ # @!attribute values
8
+ # @return [Hash] sub objects in the struct
9
+ # @!attribute root
10
+ # @return is this the root level JSON object?
11
+
2
12
  class RustStruct
3
- attr_reader :name, :values
13
+ attr_reader :name, :values, :root
4
14
 
5
15
  @@types = {
6
16
  String => 'String',
@@ -8,7 +18,10 @@ module RustyJson
8
18
  Float => 'f64',
9
19
  Array => 'Vec',
10
20
  }
11
-
21
+ # @param name [String] the name of the returned struct
22
+ # @param root [Boolean] is this the root struct
23
+ #
24
+ # Root is used so that we can reset child structs when the to_s is finished
12
25
  def initialize(name, root = false)
13
26
  @root = root
14
27
  @printed = false
@@ -17,6 +30,9 @@ module RustyJson
17
30
  @structs = Set.new
18
31
  end
19
32
 
33
+ # reset must be called to print the struct again, as we don't want to
34
+ # repeatedly print the same struct when it occurs recursively in the
35
+ # input JSON
20
36
  def reset
21
37
  @printed = false
22
38
  @structs.each do |s|
@@ -24,6 +40,20 @@ module RustyJson
24
40
  end
25
41
  end
26
42
 
43
+ # Add Value is how we add keys to the resulting Struct
44
+ # We need a name and a type, and potentially a subtype
45
+ #
46
+ # For example:
47
+ # types could be String, Fixnum, or Array
48
+ # If the tyoe is Array, then we need the subtype of the array,
49
+ # is it an array of integers or strings?
50
+ #
51
+ # @param name [String] what is this key in the struct
52
+ # @param type [Class] What typs are we
53
+ # @param subtype [Class] What typs are we
54
+ #
55
+ # @return true
56
+
27
57
  def add_value(name, type, subtype = nil)
28
58
  if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct
29
59
  struct = if type.class == RustyJson::RustStruct
@@ -41,21 +71,15 @@ module RustyJson
41
71
  else
42
72
  @values[name] = [type, subtype]
43
73
  end
74
+ true
44
75
  end
45
76
 
46
- def required_structs
47
- struct = ""
48
- # binding.pry
49
- @structs.to_a.each do |nested_struct|
50
- struct << nested_struct.to_s + "\n"
51
- end
52
- struct
53
- end
54
-
77
+ # two Rust structs are equal if all of their keys / value types are the same
55
78
  def == other
56
79
  self.values == other.values
57
80
  end
58
81
 
82
+ # to_s controlls how to display the RustStruct as a Rust Struct
59
83
  def to_s
60
84
  return "" if @printed
61
85
  @printed = true
@@ -83,6 +107,15 @@ struct #{@name} {
83
107
 
84
108
  private
85
109
 
110
+ def required_structs
111
+ struct = ""
112
+ # binding.pry
113
+ @structs.to_a.each do |nested_struct|
114
+ struct << nested_struct.to_s + "\n"
115
+ end
116
+ struct
117
+ end
118
+
86
119
  def self.add_type(name, value)
87
120
  @@types[name] = value
88
121
  end
@@ -1,3 +1,3 @@
1
1
  module RustyJson
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
data/lib/rusty_json.rb CHANGED
@@ -2,7 +2,26 @@ require "rusty_json/version"
2
2
  require "rusty_json/parser"
3
3
  require "rusty_json/rust_struct"
4
4
 
5
+ # RustyJson is a gem to support [Rust](https://www.rust-lang.org) development
6
+ # when using JSON. For a deeply nested JSON object, one may create many
7
+ # structs to define the syntax of the expected JSON. RustyJson merely
8
+ # Aims to reduce that :)
9
+
5
10
  module RustyJson
11
+ # Example Usage:
12
+ #
13
+ # json = File.read('json_file.json')
14
+ # rust = RustyJson.parse(json)
15
+ # File.write('output_structs.rs', rust)
16
+ #
17
+ # In the above example, RustyJson will parse the JSON in the file:
18
+ # json_file.json and then you print the rust structs out to a Rust
19
+ # File.
20
+ #
21
+ # @param json [String] the name of the returned struct
22
+ # @param name [String] the JSON to parse
23
+ # @return String
24
+
6
25
  def self.parse(json, name = 'JSON')
7
26
  parser = Parser.new(name, json)
8
27
  parser.parse
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rusty_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris MacNaughton