2022-11-25 03:01:34 +00:00
|
|
|
require "colorize"
|
|
|
|
|
2019-04-13 18:16:59 +00:00
|
|
|
module Ameba
|
|
|
|
enum Severity
|
|
|
|
Error
|
|
|
|
Warning
|
2019-05-11 18:17:18 +00:00
|
|
|
Convention
|
2019-04-13 18:16:59 +00:00
|
|
|
|
2019-04-14 13:45:31 +00:00
|
|
|
# Returns a symbol uniquely indicating severity.
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# Severity::Warning.symbol # => 'W'
|
|
|
|
# ```
|
2021-01-11 18:13:58 +00:00
|
|
|
def symbol : Char
|
|
|
|
case self
|
|
|
|
in Error then 'E'
|
|
|
|
in Warning then 'W'
|
|
|
|
in Convention then 'C'
|
|
|
|
end
|
2019-04-14 13:45:31 +00:00
|
|
|
end
|
|
|
|
|
2022-11-25 03:01:34 +00:00
|
|
|
# Returns a color uniquely indicating severity.
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# Severity::Warning.color # => Colorize::ColorANSI::Red
|
|
|
|
# ```
|
|
|
|
def color : Colorize::Color
|
|
|
|
case self
|
|
|
|
in Error then Colorize::ColorANSI::Red
|
|
|
|
in Warning then Colorize::ColorANSI::Red
|
|
|
|
in Convention then Colorize::ColorANSI::Blue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-14 13:45:31 +00:00
|
|
|
# Creates Severity by the name.
|
|
|
|
#
|
|
|
|
# ```
|
2019-05-11 18:17:18 +00:00
|
|
|
# Severity.parse("convention") # => Severity::Convention
|
|
|
|
# Severity.parse("foo-bar") # => Exception: Incorrect severity name
|
2019-04-14 13:45:31 +00:00
|
|
|
# ```
|
2019-04-26 15:15:36 +00:00
|
|
|
def self.parse(name : String)
|
|
|
|
super name
|
2019-04-14 16:31:50 +00:00
|
|
|
rescue ArgumentError
|
2023-02-19 15:39:25 +00:00
|
|
|
raise "Incorrect severity name #{name}. Try one of: #{values.map(&.to_s).join(", ")}"
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-14 13:45:31 +00:00
|
|
|
# Converter for `YAML.mapping` which converts severity enum to and from YAML.
|
2019-04-13 18:16:59 +00:00
|
|
|
class SeverityYamlConverter
|
|
|
|
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
|
|
|
|
unless node.is_a?(YAML::Nodes::Scalar)
|
|
|
|
raise "Severity must be a scalar, not #{node.class}"
|
|
|
|
end
|
|
|
|
|
|
|
|
case value = node.value
|
2019-04-26 15:15:36 +00:00
|
|
|
when String then Severity.parse(value)
|
2023-05-09 11:02:10 +00:00
|
|
|
when Nil then raise "Missing severity"
|
2019-04-13 18:16:59 +00:00
|
|
|
else
|
|
|
|
raise "Incorrect severity: #{value}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_yaml(value : Severity, yaml : YAML::Nodes::Builder)
|
2019-04-13 19:38:37 +00:00
|
|
|
yaml.scalar value
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|