toon-rb 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 +7 -0
- data/CHANGELOG.md +13 -0
- data/LICENSE +23 -0
- data/README.md +96 -0
- data/lib/toon/codec_options.rb +35 -0
- data/lib/toon/decoder.rb +77 -0
- data/lib/toon/encoder.rb +50 -0
- data/lib/toon/errors.rb +14 -0
- data/lib/toon/version.rb +5 -0
- data/lib/toon.rb +21 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e1a18de600edbf8f9d7ad1869dccfd2c72ba5224b669ec715f4ee194b9d98c64
|
|
4
|
+
data.tar.gz: ba63932aa8ca6b7b5de8ed43435dd8ef95cbad8927c490317cc810625ba55c97
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 99971c6f635a94138d6661439f9d1bc9c65e8e783063fe84c1f94a8e5c1de8dbc3c100513aeb710b9ab914546ee5f00f81ac765fac197733109c296a12932182
|
|
7
|
+
data.tar.gz: 866fd064fb06e7c73fd78934850ea0a7d0442a9c6ecca1834af88cc68de4ac100be6ed342191198b2455251041efa5953b2d77e0d1cb04694f94b13a12b84e96
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.1.0] - 2025-11-16
|
|
6
|
+
|
|
7
|
+
- Initial release of the Ruby TOON gem.
|
|
8
|
+
- Provides `Toon.encode`, `Toon.decode`, and `Toon.decode_safe` APIs.
|
|
9
|
+
- Implements core encode/decode for typical Ruby types (strings, integers, floats, booleans, nil, arrays, hashes).
|
|
10
|
+
- Includes RSpec unit tests and basic integration test that mirrors README usage.
|
|
11
|
+
- Targets TOON specification baseline pinned conceptually to `v1.0.0`.
|
|
12
|
+
|
|
13
|
+
|
data/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Ruby TOON Gem
|
|
2
|
+
|
|
3
|
+
A Ruby gem providing TOON-style encode/decode functionality with simple `encode` / `decode` APIs and containerized test setup.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's `Gemfile`:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "toon"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### From GitHub
|
|
20
|
+
|
|
21
|
+
Until published to RubyGems you can depend on the GitHub repository (note the `gem` subdirectory):
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "toon", git: "https://github.com/dobryakov/ruby-toon-gem", subdir: "gem"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### From local path (for development)
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem "toon", path: "path/to/ruby-toon-gem/gem"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
require "toon"
|
|
37
|
+
|
|
38
|
+
data = {
|
|
39
|
+
"message" => "Hello, TOON!",
|
|
40
|
+
"answer" => 42,
|
|
41
|
+
"items" => [1, 2, 3],
|
|
42
|
+
"flags" => { "ok" => true, "admin" => false }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
encoded = Toon.encode(data)
|
|
46
|
+
decoded = Toon.decode(encoded)
|
|
47
|
+
|
|
48
|
+
puts encoded # TOON-encoded string (UTF-8 by default)
|
|
49
|
+
puts decoded.inspect # => original Ruby structure
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Safe decode
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
result = Toon.decode_safe("not-a-valid-payload")
|
|
56
|
+
|
|
57
|
+
result.value # => nil
|
|
58
|
+
result.errors # => ["...error message..."]
|
|
59
|
+
result.diagnostics # => []
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Development
|
|
63
|
+
|
|
64
|
+
This repository is designed to run tests via Docker and `docker-compose`.
|
|
65
|
+
|
|
66
|
+
### Quickstart (docker-compose)
|
|
67
|
+
|
|
68
|
+
From the repository root:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
cp env.example .env
|
|
72
|
+
|
|
73
|
+
docker-compose -f docker/compose/docker-compose.yml build
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Run gem unit specs:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
docker-compose -f docker/compose/docker-compose.yml run --rm gem sh -lc 'bundle install && bundle exec rake spec'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Run Rails-based specs:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
docker-compose -f docker/compose/docker-compose.yml run --rm rails_app sh -lc 'bundle install && bundle exec rspec'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Or run both via the smoke test script:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
./scripts/smoke_tests.sh
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For more details, see `specs/001-implement-toon-gem/quickstart.md`.
|
|
95
|
+
|
|
96
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Toon
|
|
2
|
+
class CodecOptions
|
|
3
|
+
attr_reader :binary_output,
|
|
4
|
+
:reject_special_floats,
|
|
5
|
+
:duplicate_key_policy,
|
|
6
|
+
:canonical_encode
|
|
7
|
+
|
|
8
|
+
def initialize(
|
|
9
|
+
binary_output: false,
|
|
10
|
+
reject_special_floats: false,
|
|
11
|
+
duplicate_key_policy: :raise,
|
|
12
|
+
canonical_encode: true
|
|
13
|
+
)
|
|
14
|
+
@binary_output = binary_output
|
|
15
|
+
@reject_special_floats = reject_special_floats
|
|
16
|
+
@duplicate_key_policy = duplicate_key_policy
|
|
17
|
+
@canonical_encode = canonical_encode
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.from(options)
|
|
21
|
+
case options
|
|
22
|
+
when nil
|
|
23
|
+
new
|
|
24
|
+
when CodecOptions
|
|
25
|
+
options
|
|
26
|
+
when Hash
|
|
27
|
+
new(**options.transform_keys(&:to_sym))
|
|
28
|
+
else
|
|
29
|
+
raise ArgumentError, "Unsupported options type: #{options.class}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
|
data/lib/toon/decoder.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Toon
|
|
4
|
+
DecodeResult = Struct.new(:value, :diagnostics, :errors, keyword_init: true)
|
|
5
|
+
|
|
6
|
+
class Decoder
|
|
7
|
+
def initialize(options = nil)
|
|
8
|
+
@options = CodecOptions.from(options)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def decode(input)
|
|
12
|
+
value = parse(input)
|
|
13
|
+
validate_decoded!(value)
|
|
14
|
+
value
|
|
15
|
+
rescue DecodingError => e
|
|
16
|
+
raise e
|
|
17
|
+
rescue StandardError => e
|
|
18
|
+
raise MalformedInputError, e.message
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def decode_safe(input)
|
|
22
|
+
diagnostics = []
|
|
23
|
+
errors = []
|
|
24
|
+
value = nil
|
|
25
|
+
|
|
26
|
+
begin
|
|
27
|
+
value = decode(input)
|
|
28
|
+
rescue DecodingError => e
|
|
29
|
+
errors << e.message
|
|
30
|
+
value = nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
DecodeResult.new(value: value, diagnostics: diagnostics, errors: errors)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def parse(input)
|
|
39
|
+
str = case input
|
|
40
|
+
when String
|
|
41
|
+
input.dup
|
|
42
|
+
else
|
|
43
|
+
raise MalformedInputError, "Input must be a String"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
JSON.parse(str)
|
|
47
|
+
rescue JSON::ParserError => e
|
|
48
|
+
raise MalformedInputError, e.message
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def validate_decoded!(value)
|
|
52
|
+
case value
|
|
53
|
+
when nil, true, false, String, Integer
|
|
54
|
+
true
|
|
55
|
+
when Float
|
|
56
|
+
if @options.reject_special_floats && !value.finite?
|
|
57
|
+
raise SpecialFloatError, "NaN and Infinity not allowed when reject_special_floats is true"
|
|
58
|
+
end
|
|
59
|
+
when Array
|
|
60
|
+
value.each { |v| validate_decoded!(v) }
|
|
61
|
+
when Hash
|
|
62
|
+
# Ruby's JSON parser already applies last-wins semantics for duplicate keys.
|
|
63
|
+
# Here we only validate key/value types.
|
|
64
|
+
value.each do |k, v|
|
|
65
|
+
unless k.is_a?(String)
|
|
66
|
+
raise MalformedInputError, "Decoded hash keys must be strings, got #{k.class}"
|
|
67
|
+
end
|
|
68
|
+
validate_decoded!(v)
|
|
69
|
+
end
|
|
70
|
+
else
|
|
71
|
+
raise MalformedInputError, "Unsupported decoded type: #{value.class}"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
|
data/lib/toon/encoder.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Toon
|
|
4
|
+
class Encoder
|
|
5
|
+
def initialize(options = nil)
|
|
6
|
+
@options = CodecOptions.from(options)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def encode(value)
|
|
10
|
+
normalized = normalize(value)
|
|
11
|
+
json = JSON.generate(normalized)
|
|
12
|
+
if @options.binary_output
|
|
13
|
+
json.force_encoding(Encoding::ASCII_8BIT)
|
|
14
|
+
else
|
|
15
|
+
json.force_encoding(Encoding::UTF_8)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def normalize(value)
|
|
22
|
+
case value
|
|
23
|
+
when nil, true, false, String, Integer
|
|
24
|
+
value
|
|
25
|
+
when Float
|
|
26
|
+
raise SpecialFloatError, "NaN and Infinity are not allowed in encode" unless value.finite?
|
|
27
|
+
value
|
|
28
|
+
when Array
|
|
29
|
+
value.map { |v| normalize(v) }
|
|
30
|
+
when Hash
|
|
31
|
+
# Canonicalize by key (string representation) if enabled
|
|
32
|
+
pairs = value.to_a
|
|
33
|
+
if @options.canonical_encode
|
|
34
|
+
pairs = pairs.sort_by { |k, _| k.to_s }
|
|
35
|
+
end
|
|
36
|
+
pairs.each_with_object({}) do |(k, v), acc|
|
|
37
|
+
key = k.is_a?(Symbol) ? k.to_s : k
|
|
38
|
+
unless key.is_a?(String)
|
|
39
|
+
raise UnsupportedTypeError, "Hash keys must be strings or symbols, got #{k.class}"
|
|
40
|
+
end
|
|
41
|
+
acc[key] = normalize(v)
|
|
42
|
+
end
|
|
43
|
+
else
|
|
44
|
+
raise UnsupportedTypeError, "Unsupported type for encode: #{value.class}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
|
data/lib/toon/errors.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Toon
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
class EncodingError < Error; end
|
|
5
|
+
class DecodingError < Error; end
|
|
6
|
+
|
|
7
|
+
class UnsupportedTypeError < EncodingError; end
|
|
8
|
+
class SpecialFloatError < EncodingError; end
|
|
9
|
+
|
|
10
|
+
class DuplicateKeyError < DecodingError; end
|
|
11
|
+
class MalformedInputError < DecodingError; end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
|
data/lib/toon/version.rb
ADDED
data/lib/toon.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require_relative "toon/version"
|
|
2
|
+
require_relative "toon/errors"
|
|
3
|
+
require_relative "toon/codec_options"
|
|
4
|
+
require_relative "toon/encoder"
|
|
5
|
+
require_relative "toon/decoder"
|
|
6
|
+
|
|
7
|
+
module Toon
|
|
8
|
+
def self.encode(value, options = nil)
|
|
9
|
+
Encoder.new(options).encode(value)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.decode(input, options = nil)
|
|
13
|
+
Decoder.new(options).decode(input)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.decode_safe(input, options = nil)
|
|
17
|
+
Decoder.new(options).decode_safe(input)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: toon-rb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ruby TOON Gem
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-11-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A Ruby gem providing TOON encode/decode functionality with spec-aligned
|
|
14
|
+
behavior.
|
|
15
|
+
email:
|
|
16
|
+
- grigoriydobryakov@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/toon.rb
|
|
25
|
+
- lib/toon/codec_options.rb
|
|
26
|
+
- lib/toon/decoder.rb
|
|
27
|
+
- lib/toon/encoder.rb
|
|
28
|
+
- lib/toon/errors.rb
|
|
29
|
+
- lib/toon/version.rb
|
|
30
|
+
homepage: https://github.com/dobryakov/ruby-toon-gem
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
source_code_uri: https://github.com/dobryakov/ruby-toon-gem
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options: []
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 3.2.0
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
requirements: []
|
|
50
|
+
rubygems_version: 3.5.22
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: TOON encoder/decoder for Ruby
|
|
54
|
+
test_files: []
|