Class: Codify::Rust::Definition

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

Direct Known Subclasses

Enum, Newtype, Struct, TypeAlias

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (String, #to_s)
  • derives (Array<Symbol, #to_sym>, #to_a) (defaults to: %i(Debug),)
  • cfg_derives (Array<Symbol, #to_sym>, #to_a) (defaults to: nil)
  • comment (String, #to_s) (defaults to: nil)


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_derivesObject (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

#commentObject

Returns the value of attribute comment.



12
13
14
# File 'lib/codify/rust/definition.rb', line 12

def comment
  @comment
end

#derivesObject (readonly)

Returns the value of attribute derives.



10
11
12
# File 'lib/codify/rust/definition.rb', line 10

def derives
  @derives
end

#nameObject (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

Returns:

  • (Boolean)


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

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

#primitive?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/codify/rust/definition.rb', line 38

def primitive?
  false
end

#to_rust_codeString

Returns:

  • (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_sString

Returns:

  • (String)


44
# File 'lib/codify/rust/definition.rb', line 44

def to_s() @name end

#typesArray<Type>

Returns:



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

def types() [] end

#write(out, extra_derives: []) ⇒ void

This method returns an undefined value.

Parameters:

  • out (IO)


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