Class: Codify::Rust::Definition
- Inherits:
-
Object
- Object
- Codify::Rust::Definition
- Includes:
- Codify::Rust, Type
- Defined in:
- lib/codify/rust/definition.rb
Instance Attribute Summary collapse
-
#cfg_derives ⇒ Object
readonly
Returns the value of attribute cfg_derives.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#derives ⇒ Object
readonly
Returns the value of attribute derives.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #comment? ⇒ Boolean
-
#initialize(name, derives: %i(Debug),, cfg_derives: nil, comment: nil) ⇒ Definition
constructor
A new instance of Definition.
- #primitive? ⇒ Boolean
- #to_rust_code ⇒ String
- #to_s ⇒ String
- #types ⇒ Array<Type>
- #write(out, extra_derives: []) ⇒ void
Methods included from Type
#defaultible?, #definition?, #each_subtype, #to_rust
Constructor Details
#initialize(name, derives: %i(Debug),, cfg_derives: nil, comment: nil) ⇒ Definition
Returns a new instance of Definition.
19 20 21 22 23 24 |
# File 'lib/codify/rust/definition.rb', line 19 def initialize(name, derives: i(Debug), cfg_derives: nil, comment: nil) @name = name.to_s @derives = (derives || []).to_a.dup.uniq.map!(&:to_sym).sort @cfg_derives = (cfg_derives || []).to_a.dup.uniq.map!(&:to_sym).sort @comment = comment ? comment.to_s.strip : nil end |
Instance Attribute Details
#cfg_derives ⇒ Object (readonly)
Returns the value of attribute cfg_derives.
11 12 13 |
# File 'lib/codify/rust/definition.rb', line 11 def cfg_derives @cfg_derives end |
#comment ⇒ Object
Returns the value of attribute comment.
12 13 14 |
# File 'lib/codify/rust/definition.rb', line 12 def comment @comment end |
#derives ⇒ Object (readonly)
Returns the value of attribute derives.
10 11 12 |
# File 'lib/codify/rust/definition.rb', line 10 def derives @derives end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/codify/rust/definition.rb', line 9 def name @name end |
Instance Method Details
#comment? ⇒ Boolean
28 29 30 |
# File 'lib/codify/rust/definition.rb', line 28 def comment? self.comment && !self.comment.empty? end |
#primitive? ⇒ Boolean
38 39 40 |
# File 'lib/codify/rust/definition.rb', line 38 def primitive? false end |
#to_rust_code ⇒ String
48 49 50 51 52 |
# File 'lib/codify/rust/definition.rb', line 48 def to_rust_code out = StringIO.new write(out) out.string end |
#to_s ⇒ String
44 |
# File 'lib/codify/rust/definition.rb', line 44 def to_s() @name end |
#write(out, extra_derives: []) ⇒ void
This method returns an undefined value.
57 58 59 60 61 62 63 |
# File 'lib/codify/rust/definition.rb', line 57 def write(out, extra_derives: []) out.puts wrap_text(self.comment, 80-4).map { |s| s.prepend("/// ") }.join("\n") if self.comment? out.puts "#[derive(#{(@derives + (extra_derives || [])).sort.uniq.join(", ")})]" unless (@derives + extra_derives).empty? if @cfg_derives.include?(:serde) out.puts "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]" end end |