libkernaux/pkgs/ruby/lib/kernaux.rb

67 lines
1.3 KiB
Ruby
Raw Normal View History

2022-01-18 10:20:37 +00:00
# frozen_string_literal: true
require_relative 'kernaux/version'
2022-01-21 21:23:57 +00:00
# Native extensions
require_relative 'kernaux/default'
2022-01-18 10:20:37 +00:00
##
# Auxiliary library for kernel development.
#
module KernAux
2022-01-21 14:38:22 +00:00
# Default callback for assertions ({.assert_cb})
2022-01-29 21:17:04 +00:00
DEFAULT_ASSERT_CB = @assert_cb = lambda { |file, line, msg|
raise AssertError, "#{file}:#{line}:#{msg}"
2022-01-20 19:36:36 +00:00
}
2022-01-21 14:38:22 +00:00
# Buffer size for {.sprintf1}
# @todo make it dynamic
SPRINTF1_BUFFER_SIZE = 10_000
2022-01-20 19:36:36 +00:00
def self.panic(msg)
file, line = caller(1..1).first.split(':')[0..1]
assert_do file, Integer(line), msg
end
2022-01-23 21:52:42 +00:00
if singleton_class.method_defined? :snprintf1
def self.sprintf(*args)
args.map do |arg|
if arg.is_a? Array
sprintf1(*arg)
else
arg
end
end.join.freeze
end
def self.sprintf1(...) = snprintf1(SPRINTF1_BUFFER_SIZE, ...).first
2022-01-21 14:52:46 +00:00
end
2022-01-20 19:36:36 +00:00
##
2022-01-20 19:42:48 +00:00
# Our base class for runtime errors.
2022-01-20 19:36:36 +00:00
#
2022-01-20 21:52:43 +00:00
class Error < RuntimeError; end
2022-01-20 19:36:36 +00:00
##
# Raised when assertion has failed or panic has been called.
#
2022-01-21 21:11:28 +00:00
# @see .panic
#
2022-01-20 21:52:43 +00:00
class AssertError < Error; end
2022-01-21 21:23:40 +00:00
##
2022-01-29 22:29:04 +00:00
# Raised when command line parsing goes wrong.
2022-01-21 21:23:40 +00:00
#
# @see .cmdline
#
class CmdlineError < Error; end
2022-01-29 22:12:09 +00:00
##
2022-01-29 22:29:04 +00:00
# Raised when integer base is invalid.
2022-01-29 22:12:09 +00:00
#
# @see .utoa
# @see .itoa
#
class InvalidNtoaBaseError < Error; end
2022-01-18 10:20:37 +00:00
end