codify.rb 0.0.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7682af0469ac965e6eaabdf5f7c5dfa364117ad1a2943a22784f81a58206cc7f
4
- data.tar.gz: 62bb7c7af1a464fed837c0a2056c1c7a34118526d162d6a39997c6c9728f4ce6
3
+ metadata.gz: 4a2fc0cfdae5034fbbd99aff7da00a1b8d985c751c902a81c109fefdb1b60955
4
+ data.tar.gz: fc8b11f52952a04cb4c672656c881df181eb5d854990a627bfc50ee544f75f36
5
5
  SHA512:
6
- metadata.gz: f3ef22cd1cef955b5c763d6b77fe310e00511462516680b5c90c17bd1fca53a42842a38843e68f7adaa4e7eef3806a2cfc1bd55c410a229fe5f34b6f7e09409b
7
- data.tar.gz: 4a9d8c8f26d2adb822bac2346828e1101415911871677c3577518a0449da03f9e057e4d622716e954c844db4ea2dc3ed4b35c49602453c3682123e1a863fba06
6
+ metadata.gz: 5ccea702e716c28ebcda7051bcfdb296c9d16feb0f0b1b045f6472c2e5b7355b89e711d6683c58c927e9b15bee717053f0de1b902d75240b3e372116878fda88
7
+ data.tar.gz: cc5573697695713655bf15db4f8e65032b9f8ca912475734e3c8a3d864cc9c827c4b5d8b3bbeb2d658f8c22e474a63d3b4da573ccf2a3060d20d101f8d46d528
data/CHANGES.md CHANGED
@@ -5,4 +5,6 @@ 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.0.0 - 2025-05-12
8
+ ## 0.1.1 - 2025-05-13
9
+
10
+ ## 0.1.0 - 2025-05-12
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.1
@@ -0,0 +1,62 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'types'
4
+
5
+ class Codify::Rust::Definition
6
+ include Codify::Rust
7
+ include Type
8
+
9
+ attr_reader :name
10
+ attr_reader :derives
11
+ attr_reader :cfg_derives
12
+ attr_accessor :comment
13
+
14
+ ##
15
+ # @param [String, #to_s] name
16
+ # @param [Array<Symbol, #to_sym>, #to_a] derives
17
+ # @param [Array<Symbol, #to_sym>, #to_a] cfg_derives
18
+ # @param [String, #to_s] comment
19
+ def initialize(name, derives: %i(Debug), cfg_derives: nil, comment: nil)
20
+ @name = name.to_s
21
+ @derives = (derives || []).to_a.dup.uniq.map!(&:to_sym).sort
22
+ @cfg_derives = (cfg_derives || []).to_a.dup.uniq.map!(&:to_sym).sort
23
+ @comment = comment ? comment.to_s.strip : nil
24
+ end
25
+
26
+ ##
27
+ # @return [Boolean]
28
+ def comment?
29
+ self.comment && !self.comment.empty?
30
+ end
31
+
32
+ ##
33
+ # @return [Array<Type>]
34
+ def types() [] end
35
+
36
+ ##
37
+ # @return [Boolean]
38
+ def primitive?() false end
39
+
40
+ ##
41
+ # @return [String]
42
+ def to_s() @name end
43
+
44
+ ##
45
+ # @return [String]
46
+ def to_rust_code
47
+ out = StringIO.new
48
+ write(out)
49
+ out.string
50
+ end
51
+
52
+ ##
53
+ # @param [IO] out
54
+ # @return [void]
55
+ def write(out)
56
+ out.puts wrap_text(self.comment, 80-4).map { |s| s.prepend("/// ") }.join("\n") if self.comment?
57
+ out.puts "#[derive(#{@derives.sort.join(", ")})]" unless @derives.empty?
58
+ if @cfg_derives.include?(:serde)
59
+ out.puts "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]"
60
+ end
61
+ end
62
+ end # Codify::Rust::Definition
@@ -0,0 +1,41 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::Enum < Codify::Rust::Definition
6
+ attr_reader :variants
7
+
8
+ ##
9
+ # @param [String, #to_s] name
10
+ # @param [Array<EnumVariant>, #to_a] variants
11
+ # @param [Array<Symbol, #to_sym>, #to_a] derives
12
+ # @param [String, #to_s] comment
13
+ # @param [Proc] block
14
+ def initialize(name, variants: nil, **kwargs, &block)
15
+ super(name, **kwargs)
16
+ @variants = (variants || []).to_a.dup
17
+ block.call(self) if block_given?
18
+ end
19
+
20
+ ##
21
+ # @return [Array<Type>]
22
+ def types() @variants.map(&:type).compact.uniq.to_a end
23
+
24
+ ##
25
+ # @param [IO] out
26
+ # @return [void]
27
+ def write(out)
28
+ super(out)
29
+ if self.variants.empty?
30
+ out.puts "pub struct #{@name};"
31
+ else
32
+ out.puts "pub enum #{@name} {"
33
+ @variants.each_with_index do |variant, i|
34
+ out.puts if i > 0
35
+ out.puts wrap_text(variant.comment, 80-8).map { |s| s.prepend(" /// ") }.join("\n") if variant.comment?
36
+ variant.write(out)
37
+ end
38
+ out.puts "}"
39
+ end
40
+ end
41
+ end # Codify::Rust::Enum
@@ -0,0 +1,38 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::EnumVariant
6
+ include Codify::Rust
7
+
8
+ attr_reader :name, :type, :summary
9
+ attr_accessor :comment
10
+
11
+ def initialize(name, type = nil, &block)
12
+ @name = name.to_sym
13
+ @type = type
14
+ raise ArgumentError, "#{type.inspect}" unless type.nil? || type.is_a?(Type)
15
+ block.call(self) if block_given?
16
+ end
17
+
18
+ ##
19
+ # @return [Boolean]
20
+ def comment?
21
+ self.comment && !self.comment.empty?
22
+ end
23
+
24
+ ##
25
+ # @return [Array<Type>]
26
+ def types() [@type].compact end
27
+
28
+ ##
29
+ # @param [IO] out
30
+ # @return [void]
31
+ def write(out)
32
+ if !@type
33
+ out.puts " #{@name},"
34
+ else
35
+ out.puts " #{@name}(#{@type}),"
36
+ end
37
+ end
38
+ end # Codify::Rust::EnumVariant
@@ -0,0 +1,30 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::Newtype < Codify::Rust::Definition
6
+ ##
7
+ # @param [String, #to_s] name
8
+ # @param [Type] type
9
+ # @param [Array<Symbol, #to_sym>, #to_a] derives
10
+ # @param [String, #to_s] comment
11
+ # @param [Proc] block
12
+ def initialize(name, type, **kwargs, &block)
13
+ super(name, **kwargs)
14
+ raise ArgumentError, "#{type.inspect}" unless type.is_a?(Type)
15
+ @type = type
16
+ block.call(self) if block_given?
17
+ end
18
+
19
+ ##
20
+ # @return [Array<Type>]
21
+ def types() [@type] end
22
+
23
+ ##
24
+ # @param [IO] out
25
+ # @return [void]
26
+ def write(out)
27
+ super(out)
28
+ out.puts "pub struct #{@name}(pub #{@type});"
29
+ end
30
+ end # Codify::Rust::Newtype
@@ -0,0 +1,42 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::Struct < Codify::Rust::Definition
6
+ attr_reader :fields
7
+
8
+ ##
9
+ # @param [String, #to_s] name
10
+ # @param [Array<StructField>, #to_a] fields
11
+ # @param [Array<Symbol, #to_sym>, #to_a] derives
12
+ # @param [String, #to_s] comment
13
+ # @param [Proc] block
14
+ def initialize(name, fields: nil, **kwargs, &block)
15
+ super(name, **kwargs)
16
+ @fields = (fields || []).to_a.dup
17
+ block.call(self) if block_given?
18
+ end
19
+
20
+ ##
21
+ # @return [Array<Type>]
22
+ def types() @fields.map(&:type).uniq.to_a end
23
+
24
+ ##
25
+ # @param [IO] out
26
+ # @return [void]
27
+ def write(out)
28
+ super(out)
29
+ if self.fields.empty?
30
+ out.puts "pub struct #{@name};"
31
+ else
32
+ out.puts "pub struct #{@name} {"
33
+ @fields.each_with_index do |field, i|
34
+ out.puts if i > 0
35
+ out.puts wrap_text(field.comment, 80-8).map { |s| s.prepend(" /// ") }.join("\n") if field.comment?
36
+ out.puts " #[cfg_attr(feature = \"serde\", serde(rename = \"#{k.id}\"))]" if false
37
+ field.write(out)
38
+ end
39
+ out.puts "}"
40
+ end
41
+ end
42
+ end # Codify::Rust::Struct
@@ -0,0 +1,33 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::StructField
6
+ include Codify::Rust
7
+
8
+ attr_reader :name, :type, :summary
9
+ attr_accessor :comment, :rename
10
+
11
+ def initialize(name, type, &block)
12
+ @name = name.to_s.gsub('[]', '').gsub('.', '_')
13
+ @type = type
14
+ raise ArgumentError, "#{type.inspect}" unless type.is_a?(Type)
15
+ block.call(self) if block_given?
16
+ end
17
+
18
+ ##
19
+ # @return [Boolean]
20
+ def comment?() self.comment && !self.comment.empty? end
21
+
22
+ ##
23
+ # @return [Array<Type>]
24
+ def types() [@type] end
25
+
26
+ ##
27
+ # @param [IO] out
28
+ # @return [void]
29
+ def write(out)
30
+ out.puts " #[cfg_attr(feature = \"serde\", serde(rename = \"#{self.rename}\"))]" if self.rename
31
+ out.puts " pub r##{@name}: #{@type},"
32
+ end
33
+ end # Codify::Rust::StructField
@@ -0,0 +1,36 @@
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 to_rust
8
+ self
9
+ end
10
+
11
+ ##
12
+ # @return [Boolean]
13
+ def primitive?()
14
+ true
15
+ end
16
+
17
+ def definition?
18
+ Codify::Rust::Definition === self
19
+ end
20
+
21
+ ##
22
+ # @return [Array<Type>]
23
+ def types()
24
+ []
25
+ end
26
+
27
+ ##
28
+ # @return [void]
29
+ def each_subtype(&block)
30
+ self.types.each do |subtype|
31
+ raise RuntimeError, subtype.inspect unless subtype.is_a?(Codify::Rust::Type)
32
+ block.call(subtype) if block_given?
33
+ subtype.each_subtype(&block)
34
+ end
35
+ end
36
+ end # Codify::Rust::Type
@@ -0,0 +1,76 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'type'
4
+
5
+ module Codify::Rust::Types
6
+ Named = ::Struct.new('Named', :t) do
7
+ include Codify::Rust::Type
8
+
9
+ def self.to_s() 'Codify::Rust::Types::Named' end
10
+
11
+ def types() [] end # NB
12
+ def to_s() t.to_s end
13
+ end
14
+
15
+ Val = ::Struct.new('Val', :t) do
16
+ include Codify::Rust::Type
17
+
18
+ def self.to_s() 'Codify::Rust::Types::Val' end
19
+
20
+ def types() [] end
21
+ def to_s() t.to_s end
22
+ def inspect() t.to_s end
23
+ end
24
+
25
+ Bool = Val.new(:bool).freeze
26
+ I64 = Val.new(:i64).freeze
27
+ F64 = Val.new(:f64).freeze
28
+ String = Val.new(:String).freeze
29
+
30
+ Ref = ::Struct.new('Ref', :t) do
31
+ include Codify::Rust::Type
32
+
33
+ def self.to_s() 'Codify::Rust::Types::Ref' end
34
+
35
+ def types() [t] end
36
+ def to_s() "&#{t}" end
37
+ end
38
+
39
+ Tuple0 = ::Struct.new('Unit', :m) do
40
+ include Codify::Rust::Type
41
+
42
+ def self.to_s() 'Codify::Rust::Types::Unit' end
43
+
44
+ def types() [] end
45
+ def to_s() m ? "(/*#{m}*/)" : '()' end
46
+ end
47
+
48
+ Unit = Tuple0.new.freeze
49
+
50
+ Vec = ::Struct.new('Vec', :t) do
51
+ include Codify::Rust::Type
52
+
53
+ def self.to_s() 'Codify::Rust::Types::Vec' end
54
+
55
+ def types() [t] end
56
+ def to_s() "Vec<#{t || '_'}>" end
57
+ end
58
+
59
+ Option = ::Struct.new('Option', :t) do
60
+ include Codify::Rust::Type
61
+
62
+ def self.to_s() 'Codify::Rust::Types::Option' end
63
+
64
+ def types() [t] end
65
+ def to_s() "Option<#{t}>" end
66
+ end
67
+
68
+ Result = ::Struct.new('Result', :t, :e) do
69
+ include Codify::Rust::Type
70
+
71
+ def self.to_s() 'Codify::Rust::Types::Result' end
72
+
73
+ def types() [t, e] end
74
+ def to_s() "Result<#{t}, #{e}>" end
75
+ end
76
+ end # Codify::Rust::Types
data/lib/codify/rust.rb CHANGED
@@ -1 +1,13 @@
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
+ require_relative 'rust/definition'
7
+ require_relative 'rust/enum'
8
+ require_relative 'rust/enum_variant'
9
+ require_relative 'rust/newtype'
10
+ require_relative 'rust/struct'
11
+ require_relative 'rust/struct_field'
12
+ require_relative 'rust/type'
13
+ require_relative 'rust/types'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codify.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-12 00:00:00.000000000 Z
10
+ date: 2025-05-13 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rspec
@@ -50,6 +50,14 @@ files:
50
50
  - VERSION
51
51
  - lib/codify.rb
52
52
  - lib/codify/rust.rb
53
+ - lib/codify/rust/definition.rb
54
+ - lib/codify/rust/enum.rb
55
+ - lib/codify/rust/enum_variant.rb
56
+ - lib/codify/rust/newtype.rb
57
+ - lib/codify/rust/struct.rb
58
+ - lib/codify/rust/struct_field.rb
59
+ - lib/codify/rust/type.rb
60
+ - lib/codify/rust/types.rb
53
61
  - lib/codify/version.rb
54
62
  homepage: https://github.com/dryruby/codify.rb
55
63
  licenses: