Class: Codify::Rust::EnumVariant

Inherits:
Object
  • Object
show all
Includes:
Codify::Rust
Defined in:
lib/codify/rust/enum_variant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, default: nil, serde: nil, &block) ⇒ EnumVariant

Returns a new instance of EnumVariant.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/codify/rust/enum_variant.rb', line 11

def initialize(name, type = nil, default: nil, serde: nil, &block)
  @name = name.to_sym
  @type = type
  raise ArgumentError, "#{type.inspect}" unless type.nil? || type.is_a?(Type)
  @default = default
  @serde = serde || {} # :untagged, :other, :rename
  block.call(self) if block_given?
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



9
10
11
# File 'lib/codify/rust/enum_variant.rb', line 9

def comment
  @comment
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/codify/rust/enum_variant.rb', line 8

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/codify/rust/enum_variant.rb', line 8

def name
  @name
end

#summaryObject (readonly)

Returns the value of attribute summary.



8
9
10
# File 'lib/codify/rust/enum_variant.rb', line 8

def summary
  @summary
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/codify/rust/enum_variant.rb', line 8

def type
  @type
end

Instance Method Details

#comment?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/codify/rust/enum_variant.rb', line 28

def comment?
  self.comment && !self.comment.empty?
end

#defaultible?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/codify/rust/enum_variant.rb', line 22

def defaultible?
  self.default
end

#typesArray<Type>

Returns:



34
# File 'lib/codify/rust/enum_variant.rb', line 34

def types() [@type].compact end

#write(out) ⇒ void

This method returns an undefined value.

Parameters:

  • out (IO)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/codify/rust/enum_variant.rb', line 39

def write(out)
  out.puts "    #[default]" if @default
  if @serde
    serde = []
    serde << 'untagged' if @serde[:untagged]
    serde << 'other' if @serde[:other]
    serde << 'rename = "' + @serde[:rename].to_s + '"' if @serde[:rename]
    out.puts "    #[cfg_attr(feature = \"serde\", serde(#{serde.join(', ')}))]" unless serde.empty?
  end
  if !@type
    out.puts "    #{@name},"
  else
    out.puts "    #{@name}(#{@type}),"
  end
end