rbon 0.0.210120

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.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +93 -0
  3. data/lib/rbon.rb +18 -0
  4. data/lib/rbon/dump.rb +77 -0
  5. data/lib/rbon/load.rb +151 -0
  6. metadata +56 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1da49718259949517ccc8117bed23b712fc28eabcd4a7a61b6fa4fbcc5d5f5e
4
+ data.tar.gz: 0ff78b0a378228a8d3853fd1c29b0e28196f571ae7cdeb2801e5ec974ac769d8
5
+ SHA512:
6
+ metadata.gz: c6f1bcffd0bd56eaf7a940f9b785598d04a7e409b9653b554967fe327202558f1a4b689f397eae1a52943e415496f45d6b49eacc9c61bb15f6cbc0439c7f695d
7
+ data.tar.gz: e2abefd3d9dd734215a64e90e6cb1a9a8e921700ac88759dfb843757eddf99405473dd244ac95c38cac7463ed2428fae07e19bef976591356ba5b8e17bdccac4
@@ -0,0 +1,93 @@
1
+ # RBON
2
+
3
+ * [VERSION 0.0.210120](https://github.com/carlosjhr64/rbon/releases)
4
+ * [github](https://www.github.com/carlosjhr64/rbon)
5
+ * [rubygems](https://rubygems.org/gems/rbon)
6
+
7
+ ## INSTALL:
8
+
9
+ ```shell
10
+ $ gem install rbon
11
+ ```
12
+
13
+ ## DESCRIPTION:
14
+
15
+ RBON is not JSON!
16
+
17
+ Use RBON to store your configration "Items",
18
+ if by "Items" you mean that:
19
+ `Key = (Symbol=~/^\w+[?!]?$/)`
20
+ and
21
+ `Item = (Key | String | Integer | Float | nil | bool)`
22
+ and
23
+ `Items = (Item | Array[Items] | Hash[Key, Items])`.
24
+
25
+ ## SYNOPSIS:
26
+
27
+ ```ruby
28
+ require 'rbon'
29
+
30
+ ITEMS = {
31
+ author: "CarlosJHR64",
32
+ year: 2021,
33
+ Key_List: [
34
+ :yes!,
35
+ :wut?,
36
+ :no
37
+ ],
38
+ }
39
+
40
+ # RBON::Dump.new(String tab:' ')
41
+ dumper = RBON::Dump.new
42
+
43
+ # Key = (Symbol=~/^\w+[?!]?$/)
44
+ # Item = (Key | String | Integer | Float | nil | bool)
45
+ # Items = (Item | Array[Items] | Hash[Key, Items])
46
+ # RBON#dump(Items item, IO io: StringIO.new) => String?
47
+ dump = dumper.dump ITEMS
48
+
49
+ dump.class #=> String
50
+ dump #~> ^\{\s*author: "CarlosJHR64"
51
+
52
+ # The dump is just a very restricted strict ruby code for Items:
53
+ unsafe = eval dump
54
+ ITEMS == unsafe #=> true
55
+
56
+ # But don't eval an untrusted RBON dump, load it in RBON instead:
57
+ # RBON::Load.new
58
+ loader = RBON::Load.new
59
+
60
+ # RBON::Load#load((IO | String) io) => Items
61
+ safe = loader.load dump
62
+ ITEMS == safe #=> true
63
+
64
+ # For simple Items<=>String conversion, just use:
65
+ dump = RBON.dump ITEMS
66
+ ITEMS == RBON.load(dump) #=> true
67
+ ```
68
+
69
+ ## LICENSE:
70
+
71
+ Copyright 2021 CarlosJHR64
72
+
73
+ Permission is hereby granted, free of charge,
74
+ to any person obtaining a copy of this software and
75
+ associated documentation files (the "Software"),
76
+ to deal in the Software without restriction,
77
+ including without limitation the rights
78
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
79
+ copies of the Software, and
80
+ to permit persons to whom the Software is furnished to do so,
81
+ subject to the following conditions:
82
+
83
+ The above copyright notice and this permission notice
84
+ shall be included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED "AS IS",
87
+ WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
88
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
89
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
90
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
91
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
92
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
93
+ THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ require 'stringio'
2
+ module RBON
3
+ VERSION = '0.0.210120'
4
+ require 'rbon/dump'
5
+ require 'rbon/load'
6
+
7
+ # For simple String => Items conversions
8
+ def self.load(...)
9
+ Load.new.load(...)
10
+ end
11
+
12
+ # For simple Items => String conversions
13
+ def self.dump(...)
14
+ Dump.new.dump(...)
15
+ end
16
+ end
17
+ # Requires:
18
+ # `ruby`
@@ -0,0 +1,77 @@
1
+ module RBON
2
+ class Dump
3
+ class Error < StandardError
4
+ end
5
+
6
+ def initialize(tab:' ')
7
+ @tab, @io = tab, nil
8
+ end
9
+
10
+ def dump(object, io:StringIO.new)
11
+ @io = io
12
+ traverse(object,'')
13
+ @io.print "\n"
14
+ @io.respond_to?(:string)? @io.string : nil
15
+ end
16
+ alias :pretty_generate :dump
17
+
18
+ private
19
+
20
+ def traverse(object, indent)
21
+ case object
22
+ when Hash
23
+ hash(object, indent)
24
+ when Array
25
+ array(object, indent)
26
+ when Symbol, String, Integer, Float, NilClass, TrueClass, FalseClass
27
+ item(object, indent)
28
+ else
29
+ raise RBON::Dump::Error, "Unsupported class #{object.class}: #{object.inspect}"
30
+ end
31
+ end
32
+
33
+ def item(item, indent)
34
+ case item
35
+ when String
36
+ @io.print (item=='')? '""' : item.lines.map{_1.inspect}.join(" +\n"+indent)
37
+ else
38
+ @io.print item.inspect
39
+ end
40
+ end
41
+
42
+ def array(array, indent)
43
+ if array.empty?
44
+ @io.print '[]'
45
+ else
46
+ @io.print "[\n"+indent+@tab
47
+ traverse(array[0], indent+@tab)
48
+ array[1..-1].each do |object|
49
+ @io.print ",\n"+indent+@tab
50
+ traverse(object, indent+@tab)
51
+ end
52
+ @io.print "\n"+indent+']'
53
+ end
54
+ end
55
+
56
+ def hash(hash, indent)
57
+ if hash.empty?
58
+ @io.print '{}'
59
+ else
60
+ @io.print "{\n"+indent+@tab
61
+ array = hash.to_a
62
+ key_object(*array[0], indent+@tab)
63
+ array[1..-1].each do |key,object|
64
+ @io.print ",\n"+indent+@tab
65
+ key_object(key,object, indent+@tab)
66
+ end
67
+ @io.print "\n"+indent+'}'
68
+ end
69
+ end
70
+
71
+ def key_object(key, object, indent)
72
+ raise RBON::Dump::Error, "Bad Key #{key.class}: #{key.inspect}" unless key.is_a?(Symbol) and key.match?('^\w+[?!]?$')
73
+ @io.print "#{key}: "
74
+ traverse(object, indent)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,151 @@
1
+ module RBON
2
+ class Load
3
+ class Bug < Exception
4
+ end
5
+ class Error < StandardError
6
+ end
7
+
8
+ class CloseArray < Exception
9
+ end
10
+ class CloseHash < Exception
11
+ end
12
+ class EOL < Exception
13
+ # End Of List
14
+ end
15
+
16
+ X = '\\'
17
+ XX = X+X
18
+ XQ = X+'"'
19
+ XN = X+'n'
20
+
21
+ NILS = /^nil,?$/
22
+ FALSES = /^false,?$/
23
+ TRUES = /^true,?$/
24
+
25
+ INTEGER = /^\d+,?$/
26
+ FLOAT = /^\d+\.\d+,?$/
27
+
28
+ KEY = /^\w+[?!]?:/
29
+ SYMBOL = /^:\w+[?!]?,?$/
30
+
31
+ STRING = /^".*",?$/
32
+ STRINGS = /^".*"\s*[+]$/
33
+
34
+ EMPTY_ARRAY = /^\[\],?$/
35
+ OPEN_ARRAY = '['
36
+ CLOSE_ARRAY = /^\],?$/
37
+
38
+ EMPTY_HASH = /^\{\},?$/
39
+ OPEN_HASH = '{'
40
+ CLOSE_HASH = /^\},?$/
41
+
42
+ def initialize
43
+ @opened, @io = 0, nil
44
+ end
45
+
46
+ def load(io)
47
+ @io = io
48
+ readlines
49
+ build
50
+ end
51
+
52
+ private
53
+
54
+ def readlines
55
+ case @io
56
+ when String
57
+ @io = @io.lines.map(&:strip)
58
+ when IO
59
+ @io = @io.readlines.map(&:strip)
60
+ else
61
+ raise TypeError, "RBON#load: Need IO or String, got #{@io.class}."
62
+ end
63
+ end
64
+
65
+ def build(items=nil)
66
+ loop do
67
+ item = get_item
68
+ case items
69
+ when NilClass
70
+ items = item
71
+ when Array
72
+ items.push item
73
+ when Hash
74
+ items[item[0]]=item[1]
75
+ else
76
+ raise RBON::Load::Bug, "Unexpected Error"
77
+ end
78
+ end
79
+ return items
80
+ rescue CloseArray
81
+ raise RBON::Load::Error unless items.is_a? Array
82
+ return items
83
+ rescue CloseHash
84
+ raise RBON::Load::Error unless items.is_a? Hash
85
+ return items
86
+ rescue EOL
87
+ raise RBON::Load::Error unless @opened == 0
88
+ return items
89
+ end
90
+
91
+ def chomp(string)
92
+ string.chomp(',')[1..-2].gsub(XX,X).gsub(XQ,'"').gsub(XN,"\n")
93
+ end
94
+
95
+ def get_strings
96
+ strings = ''
97
+ while @io[0].match? STRINGS
98
+ string = @io.shift
99
+ strings << chomp(string.sub(/\s*[+]$/,''))
100
+ end
101
+ string = @io.shift
102
+ raise RBON::Load::Error unless string.match? STRING
103
+ strings << chomp(string)
104
+ return strings
105
+ end
106
+
107
+ def get_item
108
+ line = @io.shift or raise EOL
109
+ case line
110
+ when NILS
111
+ nil
112
+ when FALSES
113
+ false
114
+ when TRUES
115
+ true
116
+ when INTEGER
117
+ line.to_i
118
+ when FLOAT
119
+ line.to_f
120
+ when KEY
121
+ key, string = line.split(/:\s*/,2)
122
+ @io.unshift string
123
+ item = get_item
124
+ [key.to_sym, item]
125
+ when SYMBOL
126
+ then line[1..-1].chomp(',').to_sym
127
+ when STRING
128
+ chomp(line)
129
+ when STRINGS
130
+ @io.unshift line
131
+ get_strings
132
+ when EMPTY_ARRAY then []
133
+ when OPEN_ARRAY
134
+ @opened += 1
135
+ build(Array.new)
136
+ when CLOSE_ARRAY
137
+ @opened -= 1
138
+ raise CloseArray
139
+ when EMPTY_HASH then {}
140
+ when OPEN_HASH
141
+ @opened += 1
142
+ build(Hash.new)
143
+ when CLOSE_HASH
144
+ @opened -= 1
145
+ raise CloseHash
146
+ else
147
+ raise RBON::Load::Error, "Unsupported Item: '#{line}'"
148
+ end
149
+ end
150
+ end
151
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.210120
5
+ platform: ruby
6
+ authors:
7
+ - carlosjhr64
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ RBON is not JSON!
15
+
16
+ Use RBON to store your configration "Items",
17
+ if by "Items" you mean that:
18
+ `Key = (Symbol=~/^w+[?!]?$/)`
19
+ and
20
+ `Item = (Key | String | Integer | Float | nil | bool)`
21
+ and
22
+ `Items = (Item | Array[Items] | Hash[Key, Items])`.
23
+ email: carlosjhr64@gmail.com
24
+ executables: []
25
+ extensions: []
26
+ extra_rdoc_files: []
27
+ files:
28
+ - README.md
29
+ - lib/rbon.rb
30
+ - lib/rbon/dump.rb
31
+ - lib/rbon/load.rb
32
+ homepage: https://github.com/carlosjhr64/rbon
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements:
51
+ - 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
52
+ rubygems_version: 3.2.3
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: RBON is not JSON!
56
+ test_files: []