Class: ScaleRb::Types::VariantType
- Defined in:
- lib/scale_rb/types.rb
Constant Summary
Constants inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Base
Class Method Details
.option(type, registry) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/scale_rb/types.rb', line 192 def self.option(type, registry) VariantType.new( variants: [ SimpleVariant.new(name: :None, index: 0), TupleVariant.new(name: :Some, index: 1, tuple: TupleType.new(tuple: [type], registry:)) ], registry: ) end |
.result(ok_type, err_type, registry) ⇒ Object
208 209 210 211 212 213 214 215 216 |
# File 'lib/scale_rb/types.rb', line 208 def self.result(ok_type, err_type, registry) VariantType.new( variants: [ TupleVariant.new(name: :Ok, index: 0, tuple: TupleType.new(tuple: [ok_type], registry:)), TupleVariant.new(name: :Err, index: 1, tuple: TupleType.new(tuple: [err_type], registry:)) ], registry: ) end |
Instance Method Details
#option? ⇒ Boolean
202 203 204 205 206 |
# File 'lib/scale_rb/types.rb', line 202 def option? variants.length == 2 && variants.any? { |v| v.is_a?(SimpleVariant) && v.name == :None && v.index == 0 } && variants.any? { |v| v.is_a?(TupleVariant) && v.name == :Some && v.index == 1 } end |
#to_string(depth = 0) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/scale_rb/types.rb', line 175 def to_string(depth = 0) if depth > MAX_DEPTH variants.sort_by(&:index).map { |v| v.name.to_s }.join(' | ') else variants.sort_by(&:index).map do |v| case v when SimpleVariant v.name.to_s when TupleVariant "#{v.name}#{v.tuple.to_string(depth + 1)}" when StructVariant "#{v.name} #{v.struct.to_string(depth + 1)}" end end.join(' | ') end end |