rusty_json 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2fe6de200028a4b1a596227c6ae8e4a78c51dda9
4
+ data.tar.gz: ba00bb917821e7c6bd969bcbb26bf3b3b11fd002
5
+ SHA512:
6
+ metadata.gz: 9f0fa73570af7e1e32b32a5161f478fb31de340e41bdf808c57e30aa5ca509492a4caf98f128213ffa0fffb539f03bec08efe25b6e6de76a759ea236413f6e0f
7
+ data.tar.gz: 52d08eedca7d9d100431d1e15340bcfc9511fe4e8812729dc9df7805777b5c9079df1ff413e32cfa404be7573adc0cf0ebd36168066f0ed3a74724392d5d9f83
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.7
5
+ - 2.2.3
6
+ - ruby-head
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: ruby-head
10
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rusty_json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris MacNaughton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # RustyJson
2
+
3
+ RustyJson is a parser for JSON that takes in a JSON string and outputs [Rust](https://www.rust-lang.org/) structs to use with [rustc_serialize](http://doc.rust-lang.org/rustc-serialize/rustc_serialize/index.html) to parse JSON.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rusty_json'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rusty_json
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ json = '{"test":64}'
25
+ parsed = RustyJson.parse(json)
26
+ ```
27
+
28
+ ```rust
29
+ struct JSON {
30
+ test: i64,
31
+ }
32
+ ```
33
+ parsed will equal the JSON struct defined above
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chrismacnaughton/rusty_json.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rusty_json"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,86 @@
1
+ require 'json'
2
+ require 'set'
3
+ require 'pry'
4
+
5
+ module RustyJson
6
+ class Parser
7
+ BASE_TYPES = {
8
+ String => 'String',
9
+ Fixnum => 'i64',
10
+ Float => 'f64',
11
+ }
12
+
13
+ def initialize(name, json)
14
+ @name = name
15
+ @json = json
16
+ @struct_names = Set.new
17
+ @structs = Set.new
18
+ end
19
+
20
+ def parse
21
+ @parsed = JSON.parse(@json)
22
+ struct = RustStruct.new(@name)
23
+ if @parsed.is_a? Hash
24
+ struct = parse_hash(@parsed, struct)
25
+ end
26
+ struct.to_s
27
+ end
28
+
29
+ private
30
+
31
+ def parse_parts(name, values, struct)
32
+ if values.is_a? Array
33
+ struct = parse_array(name, values, struct)
34
+ elsif values.is_a? Hash
35
+ n = name.split('_').collect(&:capitalize).join
36
+ # binding.pry if n.include? 'Key'
37
+ n = if @struct_names.include? n
38
+ parts = n.split('_')
39
+ if parts.count > 1
40
+ "#{parts[0]}_#{parts[1].to_i + 1}"
41
+ else
42
+ n + "_2"
43
+ end
44
+ else
45
+ n
46
+ end
47
+ @struct_names << n
48
+ s = RustStruct.new(n)
49
+ s = parse_hash(values, s)
50
+ match = @structs.find{|st| s == st}
51
+ s = match || s
52
+ if match.nil?
53
+ @struct_names << n
54
+ @structs << s
55
+ end
56
+ struct.add_value(name, s)
57
+ else
58
+ struct = parse_value(name, values, struct)
59
+ end
60
+ struct
61
+ end
62
+
63
+ def parse_hash(hash, struct)
64
+ hash.each do |name, values|
65
+ struct = parse_parts(name, values, struct)
66
+ end
67
+ struct
68
+ end
69
+
70
+ def parse_array(name, values, struct)
71
+ # binding.pry
72
+ types = Set.new
73
+ values.each do |v|
74
+ types << v.class
75
+ end
76
+ fail("Cannot handle multi typed arrays") if types.count > 1
77
+ struct.add_value(name, Array, types.to_a.first)
78
+ struct
79
+ end
80
+
81
+ def parse_value(name, value, struct)
82
+ struct.add_value(name, value.class)
83
+ struct
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,86 @@
1
+ module RustyJson
2
+ class RustStruct
3
+ attr_reader :name, :values
4
+
5
+ @@types = {
6
+ String => 'String',
7
+ Fixnum => 'i64',
8
+ Float => 'f64',
9
+ Array => 'Vec',
10
+ }
11
+
12
+ def initialize(name)
13
+ @printed = false
14
+ @name = name
15
+ @values = {}
16
+ @structs = Set.new
17
+ end
18
+
19
+ def reset
20
+ @printed = false
21
+ end
22
+
23
+ def add_value(name, type, subtype = nil)
24
+ if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct
25
+ struct = if type.class == RustyJson::RustStruct
26
+ t = type
27
+ type = type.name
28
+ t
29
+ elsif subtype.class == RustyJson::RustStruct
30
+ s = subtype
31
+ subtype = subtype.name
32
+ s
33
+ end
34
+ @structs << struct
35
+ RustStruct.add_type(struct.name, struct.name)
36
+ @values[name] = [type, subtype]
37
+ else
38
+ @values[name] = [type, subtype]
39
+ end
40
+ end
41
+
42
+ def required_structs
43
+ struct = ""
44
+ # binding.pry
45
+ @structs.to_a.each do |nested_struct|
46
+ struct << nested_struct.to_s + "\n"
47
+ end
48
+ struct
49
+ end
50
+
51
+ def == other
52
+ self.values == other.values
53
+ end
54
+
55
+ def to_s
56
+ return "" if @printed
57
+ @printed = true
58
+ struct = required_structs
59
+ # binding.pry
60
+ struct << <<-RUST
61
+ struct #{@name} {
62
+ RUST
63
+ @values.each do |attr_name, type|
64
+ if type[1] == nil
65
+ struct += " #{attr_name}: #{RustStruct.type_name(type[0])}"
66
+ else
67
+ struct += " #{attr_name}: #{RustStruct.type_name(type[0])}<#{RustStruct.type_name(type[1])}>"
68
+ end
69
+ struct += ",\n"
70
+ end
71
+ struct << <<-RUST
72
+ }
73
+ RUST
74
+ end
75
+
76
+ private
77
+
78
+ def self.add_type(name, value)
79
+ @@types[name] = value
80
+ end
81
+
82
+ def self.type_name(type)
83
+ @@types[type]
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module RustyJson
2
+ VERSION = "1.0.1"
3
+ end
data/lib/rusty_json.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "rusty_json/version"
2
+ require "rusty_json/parser"
3
+ require "rusty_json/rust_struct"
4
+
5
+ module RustyJson
6
+ def self.parse(json, name = 'JSON')
7
+ parser = Parser.new(name, json)
8
+ parser.parse
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rusty_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rusty_json"
8
+ spec.version = RustyJson::VERSION
9
+ spec.authors = ["Chris MacNaughton"]
10
+ spec.email = ["chmacnaughton@gmail.com"]
11
+
12
+ spec.summary = "RustyJson is a parser for JSON that takes in a JSON string and outputs Rust structs"
13
+ spec.homepage = "https://github.com/ChrisMacNaughton/rusty_json"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_dependency "json"
26
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rusty_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris MacNaughton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: pry
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: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - chmacnaughton@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/rusty_json.rb
100
+ - lib/rusty_json/parser.rb
101
+ - lib/rusty_json/rust_struct.rb
102
+ - lib/rusty_json/version.rb
103
+ - rusty_json.gemspec
104
+ homepage: https://github.com/ChrisMacNaughton/rusty_json
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: RustyJson is a parser for JSON that takes in a JSON string and outputs Rust
128
+ structs
129
+ test_files: []