Class: ScaleRb::OldRegistry
- Inherits:
-
Object
- Object
- ScaleRb::OldRegistry
- Includes:
- Types
- Defined in:
- lib/scale_rb/old_registry.rb,
lib/scale_rb/old_registry.rb
Defined Under Namespace
Modules: TypeExp
Constant Summary
Constants included from Types
Types::DecodeResult, Types::HashMap, Types::Hex, Types::PortableType, Types::Primitive, Types::Registry, Types::Ti, Types::TypedArray, Types::U8, Types::U8Array, Types::UnsignedInteger, Types::VariantKind
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
- % keys
-
Integer -> String.
-
#lookup ⇒ Object
readonly
- Map name to index of type in ‘types` array % lookup
-
String -> Integer.
-
#old_types ⇒ Object
readonly
Returns the value of attribute old_types.
-
#types ⇒ Object
readonly
- % types
-
Array<PortableType>.
Instance Method Summary collapse
- #[](identifier) ⇒ Object
-
#as_primitive(name) ⇒ Object
- % as_primitive
-
String -> PrimitiveType | nil.
- #build ⇒ Object
-
#build_from_definition(name, definition) ⇒ Object
- % build_from_definition
-
String -> OldTypeDefinition -> PortableType | TypeAlias.
-
#build_portable_type(ast_type) ⇒ Object
- % build_portable_type
-
NamedType | ArrayType | TupleType -> PortableType __ :build_portable_type, { ast_type: TypedArray[TypeExp::ArrayType | TypeExp::TupleType | TypeExp::NamedType] } => PortableType.
-
#build_portable_type_from_named_type(named_type) ⇒ Object
- % build_portable_type_from_named_type
-
NamedType -> PortableType.
-
#initialize(old_types) ⇒ OldRegistry
constructor
- % initialize
-
Hash<String, Any> -> void.
- #inspect ⇒ Object
- #to_s ⇒ Object
-
#use(old_type_exp) ⇒ Object
- % use
-
String -> Integer.
Constructor Details
#initialize(old_types) ⇒ OldRegistry
- % initialize
-
Hash<String, Any> -> void
23 24 25 26 27 28 29 30 |
# File 'lib/scale_rb/old_registry.rb', line 23 def initialize(old_types) @old_types = old_types @lookup = {} @keys = {} @types = [] build() end |
Instance Attribute Details
#keys ⇒ Object (readonly)
- % keys
-
Integer -> String
15 16 17 |
# File 'lib/scale_rb/old_registry.rb', line 15 def keys @keys end |
#lookup ⇒ Object (readonly)
Map name to index of type in ‘types` array
- % lookup
-
String -> Integer
12 13 14 |
# File 'lib/scale_rb/old_registry.rb', line 12 def lookup @lookup end |
#old_types ⇒ Object (readonly)
Returns the value of attribute old_types.
20 21 22 |
# File 'lib/scale_rb/old_registry.rb', line 20 def old_types @old_types end |
#types ⇒ Object (readonly)
- % types
-
Array<PortableType>
18 19 20 |
# File 'lib/scale_rb/old_registry.rb', line 18 def types @types end |
Instance Method Details
#[](identifier) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/scale_rb/old_registry.rb', line 38 def [](identifier) if identifier.is_a?(::Integer) @types[identifier] elsif identifier.is_a?(::String) @types[use(identifier)] else raise "Unknown identifier type: #{identifier.class}" end end |
#as_primitive(name) ⇒ Object
- % as_primitive
-
String -> PrimitiveType | nil
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/scale_rb/old_registry.rb', line 126 def as_primitive(name) case name.downcase when /^i\d+$/ PrimitiveType.new(primitive: "I#{name[1..]}".to_sym) when /^u\d+$/ PrimitiveType.new(primitive: "U#{name[1..]}".to_sym) when /^bool$/ PrimitiveType.new(primitive: :Bool) when /^str$/, /^text$/ PrimitiveType.new(primitive: :Str) else nil end end |
#build ⇒ Object
32 33 34 35 36 |
# File 'lib/scale_rb/old_registry.rb', line 32 def build() @old_types.keys.each do |name| use(name) end end |
#build_from_definition(name, definition) ⇒ Object
- % build_from_definition
-
String -> OldTypeDefinition -> PortableType | TypeAlias
type OldTypeDefinition = String | OldEnumDefinition | OldStructDefinition type OldEnumDefinition =
_enum: String[] | Hash<Symbol, Any>,
type OldStructDefinition =
_struct: Hash<Symbol, Any>
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/scale_rb/old_registry.rb', line 150 def build_from_definition(name, definition) # rubocop:disable Metrics/MethodLength case definition when String # TypeAlias.new(name, use(definition)) alias_type_id = use(definition) # p "alias_type_id: #{alias_type_id}" types[alias_type_id] when Hash if definition['_enum'] _build_portable_type_from_enum_definition(definition) elsif definition['_set'] raise 'Sets are not supported' else _build_portable_type_from_struct_definition(definition) end end end |
#build_portable_type(ast_type) ⇒ Object
- % build_portable_type
-
NamedType | ArrayType | TupleType -> PortableType
__ :build_portable_type, { ast_type: TypedArray[TypeExp::ArrayType | TypeExp::TupleType | TypeExp::NamedType] } => PortableType
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/scale_rb/old_registry.rb', line 79 def build_portable_type(ast_type) case ast_type when TypeExp::ArrayType ArrayType.new(use(ast_type.item), ast_type.len, registry: self) when TypeExp::TupleType TupleType.new(ast_type.params.map { |param| use(param) }) when TypeExp::NamedType build_portable_type_from_named_type(ast_type) else raise "Unknown type: #{ast_type.class}" end end |
#build_portable_type_from_named_type(named_type) ⇒ Object
- % build_portable_type_from_named_type
-
NamedType -> PortableType
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/scale_rb/old_registry.rb', line 93 def build_portable_type_from_named_type(named_type) name = named_type.name params = named_type.params definition = @old_types[name] return build_from_definition(name, definition) if definition primitive = as_primitive(name) return primitive if primitive case name when 'Vec' item_index = use(params[0].to_s) SequenceType.new(type: item_index, registry: self) when 'Option' item_index = use(params[0].to_s) VariantType.option(item_index, self) when 'Result' ok_index = use(params[0].to_s) err_index = use(params[1].to_s) VariantType.result(ok_index, err_index, self) when 'Compact' # item_index = use(params[0].to_s) # CompactType.new(type: item_index, registry: self) CompactType.new when 'Null' UnitType.new else raise "Unknown type: #{name}" end end |
#inspect ⇒ Object
48 49 50 |
# File 'lib/scale_rb/old_registry.rb', line 48 def inspect "registry(#{@types.length} types)" end |
#to_s ⇒ Object
52 53 54 55 56 |
# File 'lib/scale_rb/old_registry.rb', line 52 def to_s @types.map.with_index do |type, index| "#{@keys[index]} => #{type.to_s}" end.join("\n") end |
#use(old_type_exp) ⇒ Object
- % use
-
String -> Integer
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scale_rb/old_registry.rb', line 59 def use(old_type_exp) raise "Empty old_type_exp: #{old_type_exp}" if old_type_exp.nil? || old_type_exp.strip == '' ast_type = TypeExp.parse(old_type_exp) raise "No AST type for #{old_type_exp}" if ast_type.nil? key = ast_type.to_s ti = lookup[key] return ti if ti ti = @types.length @types[ti] = "Placeholder" @lookup[key] = ti @keys[ti] = key @types[ti] = build_portable_type(ast_type) ti end |