123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #!/usr/bin/env ruby
- # frozen_string_literal: true
- # sharable_constant_value: literal
- begin
- require 'optparse'
- input_format = nil
- output_format = nil
- input_file = nil
- output_file = nil
- OptionParser.new do |opts|
- opts.banner = 'Usage: convert-serialization [options] input_file output_file'
- opts.on('-s', '--inf INPUT_FORMAT', 'Specifies the input format (msgpack / json / yaml / toml).') do |format|
- input_format = format
- end
- opts.on('-d', '--outf OUTPUT_FORMAT', 'Specifies the output format (msgpack / json / yaml / toml).') do |format|
- output_format = format
- end
- opts.on('-i', '--in INPUT_FILE', 'Specifies the input file.') do |file|
- input_file = file
- end
- opts.on('-o', '--out OUTPUT_FILE', 'Specifies the output file.') do |file|
- output_file = file
- end
- end.parse!
- raise ArgumentError, 'Input format missing.' unless input_format
- raise ArgumentError, 'Output format missing.' unless output_format
- raise ArgumentError, 'Input file is missing.' unless input_file
- raise ArgumentError, 'Output file is missing.' unless output_file
- input_format.strip!
- input_format.downcase!
- output_format.strip!
- output_format.downcase!
- input_file.strip!
- input_file.downcase!
- output_file.strip!
- output_file.downcase!
- in_handler = case input_format
- when 'msgpack'
- lambda do |filename|
- require 'msgpack'
- file = File.new filename, 'rb'
- result = MessagePack.unpack file
- file.close
- result
- end
- when 'json'
- lambda do |filename|
- require 'psych'
- Psych.load_file filename
- end
- when 'yaml'
- lambda do |filename|
- require 'psych'
- Psych.load_file filename
- end
- when 'toml'
- lambda do |filename|
- require 'toml-rb'
- TomlRB.load_file filename
- end
- else
- raise ArgumentError, 'Unknown input format.'
- end
- out_handler = case output_format
- when 'msgpack'
- lambda do |object, filename|
- require 'msgpack'
- file = File.new filename, 'wb'
- MessagePack.pack object, file
- file.close
- end
- when 'json'
- lambda do |object, filename|
- require 'json'
- file = File.new filename, 'w'
- JSON.dump object, file
- file.close
- end
- when 'yaml'
- lambda do |object, filename|
- require 'psych'
- file = File.new filename, 'w'
- Psych.dump object, file
- file.close
- end
- when 'toml'
- lambda do |object, filename|
- raise 'The primary toml block must consist of key-value pairs.' unless object.is_a? Hash
- require 'toml-rb'
- file = File.new filename, 'w'
- file.write TomlRB.dump(object)
- file.close
- end
- else
- raise ArgumentError, 'Unknown output format.'
- end
- input_object = in_handler.call(input_file)
- out_handler.call(input_object, output_file)
- warn 'Conversion successful!'
- rescue StandardError => e
- warn 'Conversion failed!'
- warn "Error: #{e.message}"
- end
|