Class: RustyJson::RustStruct
- Inherits:
-
Object
- Object
- RustyJson::RustStruct
- Defined in:
- lib/rusty_json/rust_struct.rb
Overview
RustyJson:L:RustStruct is the object that will be translated into a Rust struct.
Constant Summary collapse
- @@types =
{ String => 'String', Fixnum => 'i64', Float => 'f64', TrueClass => 'bool', FalseClass => 'bool', Array => 'Vec', NilClass => 'Option<?>' }
Instance Attribute Summary collapse
-
#name ⇒ String
Name of the struct.
-
#root ⇒ Object
Is this the root level JSON object?.
-
#values ⇒ Hash
Sub objects in the struct.
Instance Method Summary collapse
-
#==(other) ⇒ Object
two Rust structs are equal if all of their keys / value types are the same.
-
#add_value(name, type, subtype = nil) ⇒ Object
Add Value is how we add keys to the resulting Struct We need a name and a type, and potentially a subtype.
-
#initialize(name, root = false) ⇒ RustStruct
constructor
Root is used so that we can reset child structs when the to_s is finished.
-
#reset ⇒ Object
reset must be called to print the struct again, as we don’t want to repeatedly print the same struct when it occurs recursively in the input JSON.
-
#to_s ⇒ Object
to_s controlls how to display the RustStruct as a Rust Struct.
Constructor Details
#initialize(name, root = false) ⇒ RustStruct
Root is used so that we can reset child structs when the to_s is finished
28 29 30 31 32 33 34 |
# File 'lib/rusty_json/rust_struct.rb', line 28 def initialize(name, root = false) @root = root @printed = false @name = name @values = {} @structs = Set.new end |
Instance Attribute Details
#name ⇒ String
Returns name of the struct.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rusty_json/rust_struct.rb', line 12 class RustStruct attr_reader :name, :values, :root @@types = { String => 'String', Fixnum => 'i64', Float => 'f64', TrueClass => 'bool', FalseClass => 'bool', Array => 'Vec', NilClass => 'Option<?>' } # @param name [String] the name of the returned struct # @param root [Boolean] is this the root struct # # Root is used so that we can reset child structs when the to_s is finished def initialize(name, root = false) @root = root @printed = false @name = name @values = {} @structs = Set.new end # reset must be called to print the struct again, as we don't want to # repeatedly print the same struct when it occurs recursively in the # input JSON def reset @printed = false @structs.each(&:reset) end # Add Value is how we add keys to the resulting Struct # We need a name and a type, and potentially a subtype # # For example: # types could be String, Fixnum, or Array # If the tyoe is Array, then we need the subtype of the array, # is it an array of integers or strings? # # @param name [String] what is this key in the struct # @param type [Class] What typs are we # @param subtype [Class] What typs are we # # @return true def add_value(name, type, subtype = nil) if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct if type.class == RustyJson::RustStruct t = type type = type.name struct = t elsif subtype.class == RustyJson::RustStruct s = subtype subtype = subtype.name struct = s end @structs << struct RustStruct.add_type(struct.name, struct.name) end @values[name] = [type, subtype] true end # two Rust structs are equal if all of their keys / value types are the same def ==(other) values == other.values end # to_s controlls how to display the RustStruct as a Rust Struct def to_s return '' if @printed @printed = true struct = required_structs members = @values.map do |key, value| type = RustStruct.type_name(value[0]) subtype = RustStruct.type_name(value[1]) member = " #{key}: #{type}" member << "<#{subtype}>" unless value[1].nil? member end struct << "struct #{@name} {\n" + members.join(",\n") + ",\n}\n\n" struct = struct.gsub("\n\n\n", "\n\n") reset if @root struct end private def required_structs s = @structs.map(&:to_s).join("\n") return "" if s == "\n" s end def self.add_type(name, value) @@types[name] = value end def self.type_name(type) @@types[type] end end |
#root ⇒ Object
Returns is this the root level JSON object?.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rusty_json/rust_struct.rb', line 12 class RustStruct attr_reader :name, :values, :root @@types = { String => 'String', Fixnum => 'i64', Float => 'f64', TrueClass => 'bool', FalseClass => 'bool', Array => 'Vec', NilClass => 'Option<?>' } # @param name [String] the name of the returned struct # @param root [Boolean] is this the root struct # # Root is used so that we can reset child structs when the to_s is finished def initialize(name, root = false) @root = root @printed = false @name = name @values = {} @structs = Set.new end # reset must be called to print the struct again, as we don't want to # repeatedly print the same struct when it occurs recursively in the # input JSON def reset @printed = false @structs.each(&:reset) end # Add Value is how we add keys to the resulting Struct # We need a name and a type, and potentially a subtype # # For example: # types could be String, Fixnum, or Array # If the tyoe is Array, then we need the subtype of the array, # is it an array of integers or strings? # # @param name [String] what is this key in the struct # @param type [Class] What typs are we # @param subtype [Class] What typs are we # # @return true def add_value(name, type, subtype = nil) if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct if type.class == RustyJson::RustStruct t = type type = type.name struct = t elsif subtype.class == RustyJson::RustStruct s = subtype subtype = subtype.name struct = s end @structs << struct RustStruct.add_type(struct.name, struct.name) end @values[name] = [type, subtype] true end # two Rust structs are equal if all of their keys / value types are the same def ==(other) values == other.values end # to_s controlls how to display the RustStruct as a Rust Struct def to_s return '' if @printed @printed = true struct = required_structs members = @values.map do |key, value| type = RustStruct.type_name(value[0]) subtype = RustStruct.type_name(value[1]) member = " #{key}: #{type}" member << "<#{subtype}>" unless value[1].nil? member end struct << "struct #{@name} {\n" + members.join(",\n") + ",\n}\n\n" struct = struct.gsub("\n\n\n", "\n\n") reset if @root struct end private def required_structs s = @structs.map(&:to_s).join("\n") return "" if s == "\n" s end def self.add_type(name, value) @@types[name] = value end def self.type_name(type) @@types[type] end end |
#values ⇒ Hash
Returns sub objects in the struct.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rusty_json/rust_struct.rb', line 12 class RustStruct attr_reader :name, :values, :root @@types = { String => 'String', Fixnum => 'i64', Float => 'f64', TrueClass => 'bool', FalseClass => 'bool', Array => 'Vec', NilClass => 'Option<?>' } # @param name [String] the name of the returned struct # @param root [Boolean] is this the root struct # # Root is used so that we can reset child structs when the to_s is finished def initialize(name, root = false) @root = root @printed = false @name = name @values = {} @structs = Set.new end # reset must be called to print the struct again, as we don't want to # repeatedly print the same struct when it occurs recursively in the # input JSON def reset @printed = false @structs.each(&:reset) end # Add Value is how we add keys to the resulting Struct # We need a name and a type, and potentially a subtype # # For example: # types could be String, Fixnum, or Array # If the tyoe is Array, then we need the subtype of the array, # is it an array of integers or strings? # # @param name [String] what is this key in the struct # @param type [Class] What typs are we # @param subtype [Class] What typs are we # # @return true def add_value(name, type, subtype = nil) if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct if type.class == RustyJson::RustStruct t = type type = type.name struct = t elsif subtype.class == RustyJson::RustStruct s = subtype subtype = subtype.name struct = s end @structs << struct RustStruct.add_type(struct.name, struct.name) end @values[name] = [type, subtype] true end # two Rust structs are equal if all of their keys / value types are the same def ==(other) values == other.values end # to_s controlls how to display the RustStruct as a Rust Struct def to_s return '' if @printed @printed = true struct = required_structs members = @values.map do |key, value| type = RustStruct.type_name(value[0]) subtype = RustStruct.type_name(value[1]) member = " #{key}: #{type}" member << "<#{subtype}>" unless value[1].nil? member end struct << "struct #{@name} {\n" + members.join(",\n") + ",\n}\n\n" struct = struct.gsub("\n\n\n", "\n\n") reset if @root struct end private def required_structs s = @structs.map(&:to_s).join("\n") return "" if s == "\n" s end def self.add_type(name, value) @@types[name] = value end def self.type_name(type) @@types[type] end end |
Instance Method Details
#==(other) ⇒ Object
two Rust structs are equal if all of their keys / value types are the same
77 78 79 |
# File 'lib/rusty_json/rust_struct.rb', line 77 def ==(other) values == other.values end |
#add_value(name, type, subtype = nil) ⇒ Object
Add Value is how we add keys to the resulting Struct We need a name and a type, and potentially a subtype
For example: types could be String, Fixnum, or Array If the tyoe is Array, then we need the subtype of the array, is it an array of integers or strings?
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rusty_json/rust_struct.rb', line 58 def add_value(name, type, subtype = nil) if type.class == RustyJson::RustStruct || subtype.class == RustyJson::RustStruct if type.class == RustyJson::RustStruct t = type type = type.name struct = t elsif subtype.class == RustyJson::RustStruct s = subtype subtype = subtype.name struct = s end @structs << struct RustStruct.add_type(struct.name, struct.name) end @values[name] = [type, subtype] true end |
#reset ⇒ Object
reset must be called to print the struct again, as we don’t want to repeatedly print the same struct when it occurs recursively in the input JSON
39 40 41 42 |
# File 'lib/rusty_json/rust_struct.rb', line 39 def reset @printed = false @structs.each(&:reset) end |
#to_s ⇒ Object
to_s controlls how to display the RustStruct as a Rust Struct
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rusty_json/rust_struct.rb', line 82 def to_s return '' if @printed @printed = true struct = required_structs members = @values.map do |key, value| type = RustStruct.type_name(value[0]) subtype = RustStruct.type_name(value[1]) member = " #{key}: #{type}" member << "<#{subtype}>" unless value[1].nil? member end struct << "struct #{@name} {\n" + members.join(",\n") + ",\n}\n\n" struct = struct.gsub("\n\n\n", "\n\n") reset if @root struct end |