Skip to content

Commit

Permalink
Change output_format to typed enum
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jul 22, 2023
1 parent 03287ce commit a46f426
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ class Crystal::Command
hierarchy_exp : String?,
cursor_location : String?,
output_format : String?,
dependency_output_format : DependencyPrinter::Format,
combine_rpath : Bool,
includes : Array(String),
excludes : Array(String),
Expand Down Expand Up @@ -370,6 +371,7 @@ class Crystal::Command
hierarchy_exp = nil
cursor_location = nil
output_format = nil
dependency_output_format = nil
excludes = [] of String
includes = [] of String
verbose = false
Expand Down Expand Up @@ -418,7 +420,8 @@ class Crystal::Command

if dependencies
opts.on("-f tree|flat|dot|mermaid", "--format tree|flat|dot|mermaid", "Output format tree (default), flat, dot, or mermaid") do |f|
output_format = f
dependency_output_format = DependencyPrinter::Format.parse?(f)
error "Invalid format: #{f}. Options are: tree, flat, dot, or mermaid" unless dependency_output_format
end

opts.on("-i <path>", "--include <path>", "Include path") do |f|
Expand Down Expand Up @@ -571,16 +574,11 @@ class Crystal::Command
end
end

if dependencies
output_format ||= "tree"
unless output_format.in?("tree", "flat", "dot", "mermaid")
error "You have input an invalid format, only tree, flat, dot, and mermaid are supported"
end
else
output_format ||= "text"
unless output_format.in?("text", "json")
error "You have input an invalid format, only text and JSON are supported"
end
dependency_output_format ||= DependencyPrinter::Format::Tree

output_format ||= "text"
unless output_format.in?("text", "json")
error "You have input an invalid format, only text and JSON are supported"
end

error "maximum number of threads cannot be lower than 1" if compiler.n_threads < 1
Expand All @@ -594,7 +592,9 @@ class Crystal::Command
end

combine_rpath = run && !no_codegen
@config = CompilerConfig.new compiler, sources, output_filename, emit_base_filename, arguments, specified_output, hierarchy_exp, cursor_location, output_format, combine_rpath, includes, excludes, verbose
@config = CompilerConfig.new compiler, sources, output_filename, emit_base_filename,
arguments, specified_output, hierarchy_exp, cursor_location, output_format,
dependency_output_format.not_nil!, combine_rpath, includes, excludes, verbose
end

private def gather_sources(filenames)
Expand Down
9 changes: 2 additions & 7 deletions src/compiler/crystal/tools/dependencies.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Crystal::Command
private def dependencies
config = create_compiler "tool dependencies", no_codegen: true, dependencies: true

dependency_printer = DependencyPrinter.new(STDOUT, format: config.output_format, verbose: config.verbose)
dependency_printer = DependencyPrinter.new(STDOUT, format: config.dependency_output_format, verbose: config.verbose)

dependency_printer.includes.concat config.includes.map { |path| ::Path[path].expand.to_s }
dependency_printer.excludes.concat config.excludes.map { |path| ::Path[path].expand.to_s }
Expand Down Expand Up @@ -38,12 +38,7 @@ module Crystal

getter default_paths : Array(::Path) = CrystalPath.default_paths.map { |path| ::Path[path].expand }

def initialize(@io : IO, format : Format | String? = Format::Tree, @verbose : Bool = false)
case format
in Format then @format = format
in String then @format = Format.parse(format)
in Nil then @format = Format::Tree
end
def initialize(@io : IO, @format : Format = Format::Tree, @verbose : Bool = false)
end

def enter_file(filename : String, unseen : Bool)
Expand Down

0 comments on commit a46f426

Please sign in to comment.