codify.rb 0.1.1 → 0.1.3

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: 4a2fc0cfdae5034fbbd99aff7da00a1b8d985c751c902a81c109fefdb1b60955
4
- data.tar.gz: fc8b11f52952a04cb4c672656c881df181eb5d854990a627bfc50ee544f75f36
3
+ metadata.gz: 84aae30b3df6e33c57174ae898b99e048e0b0a9df82297d296f1161afc8a2509
4
+ data.tar.gz: e7544e5d9a769837fee18b2baeb79647e6eaf874c4f937680632dee5659b7790
5
5
  SHA512:
6
- metadata.gz: 5ccea702e716c28ebcda7051bcfdb296c9d16feb0f0b1b045f6472c2e5b7355b89e711d6683c58c927e9b15bee717053f0de1b902d75240b3e372116878fda88
7
- data.tar.gz: cc5573697695713655bf15db4f8e65032b9f8ca912475734e3c8a3d864cc9c827c4b5d8b3bbeb2d658f8c22e474a63d3b4da573ccf2a3060d20d101f8d46d528
6
+ metadata.gz: f3dd64bc0fe9cfa1963159e21eb6abffbb29be32968bc776385d7549f6a5efe3fdc80d3b6bf725abd263bb422ec78ffa01d3d622fbbf20b02709185884d7be58
7
+ data.tar.gz: 7e60a3f096be2c2dbc3d5fccab31515416e09bfb7ad9022c4d0a9f15482dbe781f9258ac3851d8de020b9100ef79a74733fd2216f8f9b4464c14d000eade58bc
data/CHANGES.md CHANGED
@@ -5,6 +5,10 @@ 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.1.3 - 2025-05-17
9
+
10
+ ## 0.1.2 - 2025-05-14
11
+
8
12
  ## 0.1.1 - 2025-05-13
9
13
 
10
14
  ## 0.1.0 - 2025-05-12
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![License](https://img.shields.io/badge/license-Public%20Domain-blue.svg)](https://unlicense.org)
4
4
  [![Compatibility](https://img.shields.io/badge/ruby-3.0%2B-blue)](https://rubyreferences.github.io/rubychanges/3.0.html)
5
5
  [![Package](https://img.shields.io/gem/v/codify.rb)](https://rubygems.org/gems/codify.rb)
6
- [![Documentation](https://img.shields.io/badge/rubydoc-latest-blue)](https://rubydoc.info/gems/codify.rb)
6
+ [![Documentation](https://img.shields.io/badge/rubydoc-latest-blue)](https://rubydoc.info/github/dryruby/codify.rb)
7
7
 
8
8
  **Codify.rb** is a [Ruby] library for code generation.
9
9
 
@@ -33,7 +33,7 @@ require 'codify'
33
33
 
34
34
  ## 📚 Reference
35
35
 
36
- https://rubydoc.info/gems/codify.rb
36
+ https://rubydoc.info/github/dryruby/codify.rb
37
37
 
38
38
  ## 👨‍💻 Development
39
39
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.3
@@ -35,7 +35,9 @@ class Codify::Rust::Definition
35
35
 
36
36
  ##
37
37
  # @return [Boolean]
38
- def primitive?() false end
38
+ def primitive?
39
+ false
40
+ end
39
41
 
40
42
  ##
41
43
  # @return [String]
@@ -52,9 +54,9 @@ class Codify::Rust::Definition
52
54
  ##
53
55
  # @param [IO] out
54
56
  # @return [void]
55
- def write(out)
57
+ def write(out, extra_derives: [])
56
58
  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?
59
+ out.puts "#[derive(#{(@derives + (extra_derives || [])).sort.uniq.join(", ")})]" unless (@derives + extra_derives).empty?
58
60
  if @cfg_derives.include?(:serde)
59
61
  out.puts "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]"
60
62
  end
@@ -11,24 +11,41 @@ class Codify::Rust::Enum < Codify::Rust::Definition
11
11
  # @param [Array<Symbol, #to_sym>, #to_a] derives
12
12
  # @param [String, #to_s] comment
13
13
  # @param [Proc] block
14
- def initialize(name, variants: nil, **kwargs, &block)
14
+ def initialize(name, variants: nil, rename_all: nil, **kwargs, &block)
15
15
  super(name, **kwargs)
16
16
  @variants = (variants || []).to_a.dup
17
+ @rename_all = rename_all&.to_s
17
18
  block.call(self) if block_given?
18
19
  end
19
20
 
20
21
  ##
21
22
  # @return [Array<Type>]
22
- def types() @variants.map(&:type).compact.uniq.to_a end
23
+ def types
24
+ @variants.map(&:type).compact.uniq.to_a
25
+ end
26
+
27
+ ##
28
+ # @return [Boolean]
29
+ def defaultible?
30
+ @variants.empty? || @variants.any?(&:defaultible?)
31
+ end
32
+ alias_method :has_default?, :defaultible?
23
33
 
24
34
  ##
25
35
  # @param [IO] out
26
36
  # @return [void]
27
37
  def write(out)
28
- super(out)
38
+ super(out, extra_derives: self.defaultible? ? %i(Default) : [])
29
39
  if self.variants.empty?
30
40
  out.puts "pub struct #{@name};"
31
41
  else
42
+ if @cfg_derives.include?(:serde)
43
+ if @rename_all
44
+ out.puts "#[cfg_attr(feature = \"serde\", serde(untagged, rename_all = \"lowercase\"))]"
45
+ else
46
+ out.puts "#[cfg_attr(feature = \"serde\", serde(untagged))]"
47
+ end
48
+ end
32
49
  out.puts "pub enum #{@name} {"
33
50
  @variants.each_with_index do |variant, i|
34
51
  out.puts if i > 0
@@ -5,16 +5,25 @@ require_relative 'definition'
5
5
  class Codify::Rust::EnumVariant
6
6
  include Codify::Rust
7
7
 
8
- attr_reader :name, :type, :summary
8
+ attr_reader :name, :type, :default, :summary
9
9
  attr_accessor :comment
10
10
 
11
- def initialize(name, type = nil, &block)
11
+ def initialize(name, type = nil, default: nil, rename: nil, other: nil, &block)
12
12
  @name = name.to_sym
13
13
  @type = type
14
14
  raise ArgumentError, "#{type.inspect}" unless type.nil? || type.is_a?(Type)
15
+ @default = default
16
+ @rename = rename
17
+ @other = other
15
18
  block.call(self) if block_given?
16
19
  end
17
20
 
21
+ ##
22
+ # @return [Boolean]
23
+ def defaultible?
24
+ self.default
25
+ end
26
+
18
27
  ##
19
28
  # @return [Boolean]
20
29
  def comment?
@@ -29,6 +38,9 @@ class Codify::Rust::EnumVariant
29
38
  # @param [IO] out
30
39
  # @return [void]
31
40
  def write(out)
41
+ out.puts " #[default]" if @default
42
+ out.puts " #[cfg_attr(feature = \"serde\", serde(rename = \"#{@rename}\"))]" if @rename
43
+ out.puts " #[cfg_attr(feature = \"serde\", serde(other))]" if @other
32
44
  if !@type
33
45
  out.puts " #{@name},"
34
46
  else
@@ -16,6 +16,12 @@ class Codify::Rust::Newtype < Codify::Rust::Definition
16
16
  block.call(self) if block_given?
17
17
  end
18
18
 
19
+ ##
20
+ # @return [Boolean]
21
+ def defaultible?
22
+ @type.defaultible?
23
+ end
24
+
19
25
  ##
20
26
  # @return [Array<Type>]
21
27
  def types() [@type] end
@@ -17,9 +17,17 @@ class Codify::Rust::Struct < Codify::Rust::Definition
17
17
  block.call(self) if block_given?
18
18
  end
19
19
 
20
+ ##
21
+ # @return [Boolean]
22
+ def defaultible?
23
+ @fields.all?(&:defaultible?)
24
+ end
25
+
20
26
  ##
21
27
  # @return [Array<Type>]
22
- def types() @fields.map(&:type).uniq.to_a end
28
+ def types
29
+ @fields.map(&:type).uniq.to_a
30
+ end
23
31
 
24
32
  ##
25
33
  # @param [IO] out
@@ -17,11 +17,21 @@ class Codify::Rust::StructField
17
17
 
18
18
  ##
19
19
  # @return [Boolean]
20
- def comment?() self.comment && !self.comment.empty? end
20
+ def defaultible?
21
+ @type.defaultible?
22
+ end
23
+
24
+ ##
25
+ # @return [Boolean]
26
+ def comment?
27
+ self.comment && !self.comment.empty?
28
+ end
21
29
 
22
30
  ##
23
31
  # @return [Array<Type>]
24
- def types() [@type] end
32
+ def types
33
+ [@type]
34
+ end
25
35
 
26
36
  ##
27
37
  # @param [IO] out
@@ -10,10 +10,16 @@ module Codify::Rust::Type
10
10
 
11
11
  ##
12
12
  # @return [Boolean]
13
- def primitive?()
13
+ def primitive?
14
14
  true
15
15
  end
16
16
 
17
+ ##
18
+ # @return [Boolean]
19
+ def defaultible?
20
+ self.primitive?
21
+ end
22
+
17
23
  def definition?
18
24
  Codify::Rust::Definition === self
19
25
  end
@@ -0,0 +1,35 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'definition'
4
+
5
+ class Codify::Rust::TypeAlias < Codify::Rust::Definition
6
+ ##
7
+ # @param [String, #to_s] name
8
+ # @param [Type] type
9
+ # @param [String, #to_s] comment
10
+ # @param [Proc] block
11
+ def initialize(name, type, **kwargs, &block)
12
+ super(name, **kwargs, derives: [], cfg_derives: [])
13
+ raise ArgumentError, "#{type.inspect}" unless type.is_a?(Type)
14
+ @type = type
15
+ block.call(self) if block_given?
16
+ end
17
+
18
+ ##
19
+ # @return [Boolean]
20
+ def defaultible?
21
+ @type.defaultible?
22
+ end
23
+
24
+ ##
25
+ # @return [Array<Type>]
26
+ def types() [@type] end
27
+
28
+ ##
29
+ # @param [IO] out
30
+ # @return [void]
31
+ def write(out)
32
+ super(out)
33
+ out.puts "pub type #{@name} = #{@type};"
34
+ end
35
+ end # Codify::Rust::TypeAlias
@@ -8,6 +8,7 @@ module Codify::Rust::Types
8
8
 
9
9
  def self.to_s() 'Codify::Rust::Types::Named' end
10
10
 
11
+ def primitive?() false end
11
12
  def types() [] end # NB
12
13
  def to_s() t.to_s end
13
14
  end
@@ -63,6 +64,10 @@ module Codify::Rust::Types
63
64
 
64
65
  def types() [t] end
65
66
  def to_s() "Option<#{t}>" end
67
+
68
+ def flatten
69
+ t.is_a?(self.class) ? t : self
70
+ end
66
71
  end
67
72
 
68
73
  Result = ::Struct.new('Result', :t, :e) do
data/lib/codify/rust.rb CHANGED
@@ -10,4 +10,5 @@ require_relative 'rust/newtype'
10
10
  require_relative 'rust/struct'
11
11
  require_relative 'rust/struct_field'
12
12
  require_relative 'rust/type'
13
+ require_relative 'rust/type_alias'
13
14
  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.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-13 00:00:00.000000000 Z
10
+ date: 2025-05-17 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rspec
@@ -57,6 +57,7 @@ files:
57
57
  - lib/codify/rust/struct.rb
58
58
  - lib/codify/rust/struct_field.rb
59
59
  - lib/codify/rust/type.rb
60
+ - lib/codify/rust/type_alias.rb
60
61
  - lib/codify/rust/types.rb
61
62
  - lib/codify/version.rb
62
63
  homepage: https://github.com/dryruby/codify.rb
@@ -82,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  requirements: []
85
- rubygems_version: 3.6.3
86
+ rubygems_version: 3.6.9
86
87
  specification_version: 4
87
88
  summary: Codify.rb
88
89
  test_files: []