codify.rb 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +1 -1
- data/VERSION +1 -1
- data/lib/codify/rust.rb +284 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9506cfef26a10d61af066ebc60f754e826e2d84ba5650e7775299c59977bce4
|
4
|
+
data.tar.gz: 6be8bbfae264b901dcefb186a94c28ecfbe0329566195222544f17227922eece
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd3f75887543dc439b40a3c40b3d839a742573bdde1ffeefc0b3714cd68baf75bfe77ee9702afb8bd80aae52eb0cecebbc5111b02aa9cd59b11f7c1db001253d
|
7
|
+
data.tar.gz: 439a9352e3dc039ab923cfa194ea68fc9c66695ae0edeb988353de2b2d9619c89825516537b78a52af91ab172c8b1d6d128db56468db298b68b8a406abf50d14
|
data/CHANGES.md
CHANGED
@@ -5,4 +5,4 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
-
## 0.
|
8
|
+
## 0.1.0 - 2025-05-12
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/lib/codify/rust.rb
CHANGED
@@ -1 +1,285 @@
|
|
1
1
|
# This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
module Codify; end
|
4
|
+
module Codify::Rust; end
|
5
|
+
|
6
|
+
module Codify::Rust::Type
|
7
|
+
def definition?
|
8
|
+
Codify::Rust::Definition === self
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# @return [Boolean]
|
13
|
+
def primitive?() true end
|
14
|
+
|
15
|
+
##
|
16
|
+
# @return [Array<Type>]
|
17
|
+
def types() [] end
|
18
|
+
|
19
|
+
##
|
20
|
+
# @return [void]
|
21
|
+
def each_subtype(&block)
|
22
|
+
self.types.each do |subtype|
|
23
|
+
raise RuntimeError, subtype.inspect unless subtype.is_a?(Codify::Rust::Type)
|
24
|
+
block.call(subtype) if block_given?
|
25
|
+
subtype.each_subtype(&block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end # Codify::Rust::Type
|
29
|
+
|
30
|
+
module Codify::Rust::Types
|
31
|
+
Named = ::Struct.new('Named', :t) do |type|
|
32
|
+
include Codify::Rust::Type
|
33
|
+
type.define_method(:types) { [] } # NB
|
34
|
+
type.define_method(:to_s) { t.to_s }
|
35
|
+
end
|
36
|
+
|
37
|
+
Val = ::Struct.new('Val', :t) do |type|
|
38
|
+
include Codify::Rust::Type
|
39
|
+
type.define_method(:types) { [] }
|
40
|
+
type.define_method(:to_s) { t.to_s }
|
41
|
+
end
|
42
|
+
|
43
|
+
Bool = Val.new(:bool).freeze
|
44
|
+
I64 = Val.new(:i64).freeze
|
45
|
+
F64 = Val.new(:f64).freeze
|
46
|
+
String = Val.new(:String).freeze
|
47
|
+
|
48
|
+
Ref = ::Struct.new('Ref', :t) do |type|
|
49
|
+
include Codify::Rust::Type
|
50
|
+
type.define_method(:types) { [t] }
|
51
|
+
type.define_method(:to_s) { "&#{t}" }
|
52
|
+
end
|
53
|
+
|
54
|
+
Unit = ::Struct.new('Unit', :m) do |type|
|
55
|
+
include Codify::Rust::Type
|
56
|
+
type.define_method(:types) { [] }
|
57
|
+
type.define_method(:to_s) { m ? "(/*#{m}*/)" : "()" }
|
58
|
+
end
|
59
|
+
|
60
|
+
Vec = ::Struct.new('Vec', :t) do |type|
|
61
|
+
include Codify::Rust::Type
|
62
|
+
type.define_method(:types) { [t] }
|
63
|
+
type.define_method(:to_s) { "Vec<#{t}>" }
|
64
|
+
end
|
65
|
+
|
66
|
+
Option = ::Struct.new('Option', :t) do |type|
|
67
|
+
include Codify::Rust::Type
|
68
|
+
type.define_method(:types) { [t] }
|
69
|
+
type.define_method(:to_s) { "Option<#{t}>" }
|
70
|
+
end
|
71
|
+
|
72
|
+
Result = ::Struct.new('Result', :t, :e) do |type|
|
73
|
+
include Codify::Rust::Type
|
74
|
+
type.define_method(:types) { [t, e] }
|
75
|
+
type.define_method(:to_s) { "Result<#{t}, #{e}>" }
|
76
|
+
end
|
77
|
+
end # Codify::Rust::Types
|
78
|
+
|
79
|
+
module Codify::Rust
|
80
|
+
class Definition
|
81
|
+
include Codify::Rust::Type
|
82
|
+
|
83
|
+
attr_reader :name
|
84
|
+
attr_reader :derives
|
85
|
+
attr_accessor :comment
|
86
|
+
|
87
|
+
##
|
88
|
+
# @param [String, #to_s] name
|
89
|
+
# @param [Array<Symbol, #to_sym>, #to_a] derives
|
90
|
+
# @param [String, #to_s] comment
|
91
|
+
def initialize(name, derives: nil, comment: nil)
|
92
|
+
@name = name.to_s
|
93
|
+
@derives = (derives || []).to_a.dup.uniq.map!(&:to_sym).sort
|
94
|
+
@comment = comment ? comment.to_s.strip : nil
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# @return [Boolean]
|
99
|
+
def comment?() self.comment && !self.comment.empty? end
|
100
|
+
|
101
|
+
##
|
102
|
+
# @return [Array<Type>]
|
103
|
+
def types() [] end
|
104
|
+
|
105
|
+
##
|
106
|
+
# @return [Boolean]
|
107
|
+
def primitive?() false end
|
108
|
+
|
109
|
+
##
|
110
|
+
# @return [String]
|
111
|
+
def to_s() @name end
|
112
|
+
|
113
|
+
##
|
114
|
+
# @param [IO] out
|
115
|
+
# @return [void]
|
116
|
+
def write(out)
|
117
|
+
out.puts wrap_text(self.comment, 80-4).map { |s| s.prepend("/// ") }.join("\n") if self.comment?
|
118
|
+
out.puts "#[derive(#{@derives.sort.join(", ")})]" unless @derives.empty?
|
119
|
+
out.puts "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]"
|
120
|
+
end
|
121
|
+
end # Definition
|
122
|
+
|
123
|
+
class Newtype < Definition
|
124
|
+
##
|
125
|
+
# @param [String, #to_s] name
|
126
|
+
# @param [Type] type
|
127
|
+
# @param [Array<Symbol, #to_sym>, #to_a] derives
|
128
|
+
# @param [String, #to_s] comment
|
129
|
+
# @param [Proc] block
|
130
|
+
def initialize(name, type, derives: %i(Clone Debug), comment: nil, &block)
|
131
|
+
super(name, derives:, comment:)
|
132
|
+
raise ArgumentError, "#{type.inspect}" unless type.is_a?(Rust::Type)
|
133
|
+
@type = type
|
134
|
+
block.call(self) if block_given?
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# @return [Array<Type>]
|
139
|
+
def types() [@type] end
|
140
|
+
|
141
|
+
##
|
142
|
+
# @param [IO] out
|
143
|
+
# @return [void]
|
144
|
+
def write(out)
|
145
|
+
super(out)
|
146
|
+
out.puts "pub struct #{@name}(pub #{@type});"
|
147
|
+
end
|
148
|
+
end # Struct
|
149
|
+
|
150
|
+
class Struct < Definition
|
151
|
+
attr_reader :fields
|
152
|
+
|
153
|
+
##
|
154
|
+
# @param [String, #to_s] name
|
155
|
+
# @param [Array<Field>, #to_a] fields
|
156
|
+
# @param [Array<Symbol, #to_sym>, #to_a] derives
|
157
|
+
# @param [String, #to_s] comment
|
158
|
+
# @param [Proc] block
|
159
|
+
def initialize(name, fields: nil, derives: nil, comment: nil, &block)
|
160
|
+
super(name, derives:, comment:)
|
161
|
+
@fields = (fields || []).to_a.dup
|
162
|
+
block.call(self) if block_given?
|
163
|
+
end
|
164
|
+
|
165
|
+
##
|
166
|
+
# @return [Array<Type>]
|
167
|
+
def types() @fields.map(&:type).uniq.to_a end
|
168
|
+
|
169
|
+
##
|
170
|
+
# @param [IO] out
|
171
|
+
# @return [void]
|
172
|
+
def write(out)
|
173
|
+
super(out)
|
174
|
+
if self.fields.empty?
|
175
|
+
out.puts "pub struct #{@name};"
|
176
|
+
else
|
177
|
+
out.puts "pub struct #{@name} {"
|
178
|
+
@fields.each_with_index do |field, i|
|
179
|
+
out.puts if i > 0
|
180
|
+
out.puts wrap_text(field.comment, 80-8).map { |s| s.prepend(" /// ") }.join("\n") if field.comment?
|
181
|
+
out.puts " #[cfg_attr(feature = \"serde\", serde(rename = \"#{k.id}\"))]" if false
|
182
|
+
field.write(out)
|
183
|
+
end
|
184
|
+
out.puts "}"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end # Struct
|
188
|
+
|
189
|
+
class Enum < Definition
|
190
|
+
attr_reader :variants
|
191
|
+
|
192
|
+
##
|
193
|
+
# @param [String, #to_s] name
|
194
|
+
# @param [Array<Variant>, #to_a] variants
|
195
|
+
# @param [Array<Symbol, #to_sym>, #to_a] derives
|
196
|
+
# @param [String, #to_s] comment
|
197
|
+
# @param [Proc] block
|
198
|
+
def initialize(name, variants: nil, derives: nil, comment: nil, &block)
|
199
|
+
super(name, derives:, comment:)
|
200
|
+
@variants = (variants || []).to_a.dup
|
201
|
+
block.call(self) if block_given?
|
202
|
+
end
|
203
|
+
|
204
|
+
##
|
205
|
+
# @return [Array<Type>]
|
206
|
+
def types() @variants.map(&:type).compact.uniq.to_a end
|
207
|
+
|
208
|
+
##
|
209
|
+
# @param [IO] out
|
210
|
+
# @return [void]
|
211
|
+
def write(out)
|
212
|
+
super(out)
|
213
|
+
if self.variants.empty?
|
214
|
+
out.puts "pub struct #{@name};"
|
215
|
+
else
|
216
|
+
out.puts "pub enum #{@name} {"
|
217
|
+
@variants.each_with_index do |variant, i|
|
218
|
+
out.puts if i > 0
|
219
|
+
out.puts wrap_text(variant.comment, 80-8).map { |s| s.prepend(" /// ") }.join("\n") if variant.comment?
|
220
|
+
variant.write(out)
|
221
|
+
end
|
222
|
+
out.puts "}"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end # Enum
|
226
|
+
|
227
|
+
class Field
|
228
|
+
attr_reader :name, :type, :summary
|
229
|
+
attr_accessor :comment, :rename
|
230
|
+
|
231
|
+
def initialize(name, type, &block)
|
232
|
+
@name = name.to_s.gsub('[]', '').gsub('.', '_')
|
233
|
+
@type = type
|
234
|
+
raise ArgumentError, "#{type.inspect}" unless type.is_a?(Rust::Type)
|
235
|
+
block.call(self) if block_given?
|
236
|
+
end
|
237
|
+
|
238
|
+
##
|
239
|
+
# @return [Boolean]
|
240
|
+
def comment?() self.comment && !self.comment.empty? end
|
241
|
+
|
242
|
+
##
|
243
|
+
# @return [Array<Type>]
|
244
|
+
def types() [@type] end
|
245
|
+
|
246
|
+
##
|
247
|
+
# @param [IO] out
|
248
|
+
# @return [void]
|
249
|
+
def write(out)
|
250
|
+
out.puts " #[cfg_attr(feature = \"serde\", serde(rename = \"#{self.rename}\"))]" if self.rename
|
251
|
+
out.puts " pub r##{@name}: #{@type},"
|
252
|
+
end
|
253
|
+
end # Field
|
254
|
+
|
255
|
+
class Variant
|
256
|
+
attr_reader :name, :type, :summary
|
257
|
+
attr_accessor :comment
|
258
|
+
|
259
|
+
def initialize(name, type = nil, &block)
|
260
|
+
@name = name.to_sym
|
261
|
+
@type = type
|
262
|
+
raise ArgumentError, "#{type.inspect}" unless type.nil? || type.is_a?(Rust::Type)
|
263
|
+
block.call(self) if block_given?
|
264
|
+
end
|
265
|
+
|
266
|
+
##
|
267
|
+
# @return [Boolean]
|
268
|
+
def comment?() self.comment && !self.comment.empty? end
|
269
|
+
|
270
|
+
##
|
271
|
+
# @return [Array<Type>]
|
272
|
+
def types() [@type].compact end
|
273
|
+
|
274
|
+
##
|
275
|
+
# @param [IO] out
|
276
|
+
# @return [void]
|
277
|
+
def write(out)
|
278
|
+
if !@type
|
279
|
+
out.puts " #{@name},"
|
280
|
+
else
|
281
|
+
out.puts " #{@name}(#{@type}),"
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end # Variant
|
285
|
+
end # Codify::Rust
|