convert-serialization 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +11 -0
  3. data/README.md +3 -0
  4. data/bin/convert-serialization +109 -0
  5. metadata +93 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73503bd0fdb0d1578e3a1c2cb46ca0ecb3e479e867a37ffd858e219c104bfaa3
4
+ data.tar.gz: e72e78b2f314ce010f1dbdfea5aca9cd4e0a479be956cfaeaf7ca19f2cab2fbd
5
+ SHA512:
6
+ metadata.gz: b762692cb670950c9325b7cfb2dfb6bb3634e55c2e83c3f43b33151f6ce0ab41c8e5e931603bba2fc4939fd41679c025ffee283d2eb927bfc60ff407978dbb19
7
+ data.tar.gz: a9d4f04cb660010a4d3139f26bdb920655c79fe56698d380806ee186b912fa16fa85a1467d9e77d93330cf0b38c2852b675cde76a7d4aa9827ba1c8ea26f240c
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
7
+
8
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
9
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
10
+
11
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # convert-serialization
2
+
3
+ A simple Ruby script that can convert between different formats (msgpack / json / yaml / toml).
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ # sharable_constant_value: literal
4
+
5
+ begin
6
+ require 'optparse'
7
+
8
+ input_format = nil
9
+ output_format = nil
10
+ OptionParser.new do |opts|
11
+ opts.banner = 'Usage: convert-serialization [options] input_file output_file'
12
+
13
+ opts.on('-i', '--in INPUT_FORMAT', 'Specifies the input format (msgpack / json / yaml / toml).') do |format|
14
+ input_format = format
15
+ end
16
+
17
+ opts.on('-o', '--out OUTPUT_FORMAT', 'Specifies the output format (msgpack / json / yaml / toml).') do |format|
18
+ output_format = format
19
+ end
20
+ end.parse!
21
+
22
+ raise ArgumentError, 'Input format missing.' unless input_format
23
+ raise ArgumentError, 'Output format missing.' unless output_format
24
+
25
+ input_file = ARGV.shift
26
+ output_file = ARGV.shift
27
+
28
+ raise ArgumentError, 'Input file is missing.' unless input_file
29
+ raise ArgumentError, 'Output file is missing.' unless output_file
30
+
31
+ input_format.strip!
32
+ input_format.downcase!
33
+
34
+ output_format.strip!
35
+ output_format.downcase!
36
+
37
+ in_handler = case input_format
38
+ when 'msgpack'
39
+ lambda do |filename|
40
+ require 'msgpack'
41
+ MessagePack.load filename
42
+ end
43
+ when 'json'
44
+ lambda do |filename|
45
+ require 'psych'
46
+ Psych.load_file filename
47
+ end
48
+ when 'yaml'
49
+ lambda do |filename|
50
+ require 'psych'
51
+ Psych.load_file filename
52
+ end
53
+ when 'toml'
54
+ lambda do |filename|
55
+ require 'tomlib'
56
+ file = File.new filename, 'r'
57
+ result = Tomlib.load(file.read)
58
+ file.close
59
+
60
+ result
61
+ end
62
+ else
63
+ raise ArgumentError, 'Unknown input format.'
64
+ end
65
+
66
+ out_handler = case output_format
67
+ when 'msgpack'
68
+ lambda do |object, filename|
69
+ require 'msgpack'
70
+
71
+ file = File.new filename, 'wb'
72
+ MessagePack.pack object, file
73
+ file.close
74
+ end
75
+ when 'json'
76
+ lambda do |object, filename|
77
+ require 'json'
78
+
79
+ file = File.new filename, 'w'
80
+ JSON.dump object, file
81
+ file.close
82
+ end
83
+ when 'yaml'
84
+ lambda do |object, filename|
85
+ require 'psych'
86
+
87
+ file = File.new filename, 'w'
88
+ Psych.dump object, file
89
+ file.close
90
+ end
91
+ when 'toml'
92
+ lambda do |object, filename|
93
+ require 'tomlib'
94
+ file = File.new filename, 'w'
95
+ file.write Tomlib.dump(object)
96
+ file.close
97
+ end
98
+ else
99
+ raise ArgumentError, 'Unknown output format.'
100
+ end
101
+
102
+ input_object = in_handler.call(input_file)
103
+ out_handler.call(input_object, output_file)
104
+
105
+ warn 'Conversion successful!'
106
+ rescue StandardError => e
107
+ warn 'Conversion failed!'
108
+ warn "Error: #{e.message}"
109
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: convert-serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Küthe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tomlib
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: msgpack
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.7.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.7'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.7.2
53
+ description: A simple Ruby script that can convert between different formats (msgpack
54
+ / json / yaml / toml).
55
+ email: m.k@mk16.de
56
+ executables:
57
+ - convert-serialization
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - LICENSE
61
+ - README.md
62
+ files:
63
+ - LICENSE
64
+ - README.md
65
+ - bin/convert-serialization
66
+ homepage: https://codeberg.org/mark22k/convert-serialization
67
+ licenses:
68
+ - WTFPL
69
+ metadata:
70
+ source_code_uri: https://codeberg.org/mark22k/convert-serialization
71
+ bug_tracker_uri: https://codeberg.org/mark22k/convert-serialization/issues
72
+ rubygems_mfa_required: 'true'
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '3.1'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.4.22
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A simple Ruby script that can convert between different formats (msgpack
92
+ / json / yaml / toml).
93
+ test_files: []