Class: Codify::Rust::Enum
- Inherits:
-
Definition
- Object
- Definition
- Codify::Rust::Enum
- Defined in:
- lib/codify/rust/enum.rb
Instance Attribute Summary collapse
-
#variants ⇒ Object
readonly
Returns the value of attribute variants.
Attributes inherited from Definition
#cfg_derives, #comment, #derives, #name
Instance Method Summary collapse
- #defaultible? ⇒ Boolean (also: #has_default?)
-
#initialize(name, variants: nil, serde: nil, **kwargs, &block) ⇒ Enum
constructor
A new instance of Enum.
- #types ⇒ Array<Type>
- #write(out) ⇒ void
Methods inherited from Definition
#comment?, #primitive?, #to_rust_code, #to_s
Methods included from Type
#definition?, #each_subtype, #primitive?, #to_rust
Constructor Details
#initialize(name, variants: nil, serde: nil, **kwargs, &block) ⇒ Enum
Returns a new instance of Enum.
14 15 16 17 18 19 |
# File 'lib/codify/rust/enum.rb', line 14 def initialize(name, variants: nil, serde: nil, **kwargs, &block) super(name, **kwargs) @variants = (variants || []).to_a.dup @serde = serde || {} # :untagged, :rename_all block.call(self) if block_given? end |
Instance Attribute Details
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
6 7 8 |
# File 'lib/codify/rust/enum.rb', line 6 def variants @variants end |
Instance Method Details
#defaultible? ⇒ Boolean Also known as: has_default?
29 30 31 |
# File 'lib/codify/rust/enum.rb', line 29 def defaultible? @variants.empty? || @variants.any?(&:defaultible?) end |
#types ⇒ Array<Type>
23 24 25 |
# File 'lib/codify/rust/enum.rb', line 23 def types @variants.map(&:type).compact.uniq.to_a end |
#write(out) ⇒ void
This method returns an undefined value.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/codify/rust/enum.rb', line 37 def write(out) super(out, extra_derives: self.defaultible? ? %i(Default) : []) if self.variants.empty? out.puts "pub struct #{@name};" else if @serde serde = [] serde << 'untagged' if @serde[:untagged] serde << 'rename_all = "' + @serde[:rename_all].to_s + '"' if @serde[:rename_all] out.puts "#[cfg_attr(feature = \"serde\", serde(#{serde.join(', ')}))]" unless serde.empty? end out.puts "pub enum #{@name} {" @variants.each_with_index do |variant, i| out.puts if i > 0 out.puts wrap_text(variant.comment, 80-8).map { |s| s.prepend(" /// ") }.join("\n") if variant.comment? variant.write(out) end out.puts "}" end end |