Module: Fusion::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/fusion/cli.rb,
lib/fusion/cli/repl.rb,
lib/fusion/cli/parser.rb,
lib/fusion/cli/decoder.rb,
lib/fusion/cli/encoder.rb,
lib/fusion/cli/options.rb,
lib/fusion/cli/serializer.rb

Defined Under Namespace

Modules: Decoder, Encoder, Parser, Serializer Classes: Options, Repl

Instance Method Summary collapse

Instance Method Details

#apply(input, function) ⇒ Object

input | function -> output



69
70
71
# File 'lib/fusion/cli.rb', line 69

def apply(input, function)
  Interpreter.safe_apply(function, input)
end

#decode(string, mode:) ⇒ Object

String -> WirePair Doesn’t support mode ‘:unix`



59
60
61
# File 'lib/fusion/cli.rb', line 59

def decode(string, mode:)
  Decoder.decode(string, mode:)
end

#encode(wire_pair, mode:) ⇒ Object

WirePair -> String Doesn’t support mode ‘:unix`



86
87
88
# File 'lib/fusion/cli.rb', line 86

def encode(wire_pair, mode:)
  Encoder.encode(wire_pair, mode:)
end

#evaluate(expression, environment) ⇒ Object

expression -> runtime value Mutates environment (REPL variable binding)



75
76
77
# File 'lib/fusion/cli.rb', line 75

def evaluate(expression, environment)
  Interpreter.safe_evaluate(expression, environment)
end

#parse(wire_pair) ⇒ Object

WirePair -> runtime value



64
65
66
# File 'lib/fusion/cli.rb', line 64

def parse(wire_pair)
  Parser.parse(wire_pair)
end

#run(options) ⇒ Object

Run the use case selected on the command line.



25
26
27
28
29
30
31
32
# File 'lib/fusion/cli.rb', line 25

def run(options)
  case options.use_case
  when :pipe then run_pipe(options)
  when :stream then run_stream(options)
  when :repl then Repl.new.run
  else raise Unreachable, "Unknown use case #{options.use_case}"
  end
end

#run_pipe(options) ⇒ Object

pipe: load the program, pipe one input through it, emit one output.



35
36
37
38
39
40
# File 'lib/fusion/cli.rb', line 35

def run_pipe(options)
  function = load_program(options)
  input    = parse(load_input(options))
  output   = apply(input, function)
  emit_output(serialize(output), output_mode: options.output_mode)
end

#run_stream(options) ⇒ Object

stream: load the program once, then treat stdin/stdout as NDJSON streams —one input per line, one output line per input. Errors stay in-band (the unix mode is unavailable here), so the exit code is always 0.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fusion/cli.rb', line 45

def run_stream(options)
  function = load_program(options)
  $stdout.sync = true
  $stdin.each_line do |line|
    next if line.strip.empty?

    input  = parse(decode(line, mode: options.input_mode))
    output = apply(input, function)
    $stdout.puts(encode(serialize(output), mode: options.output_mode))
  end
end

#serialize(runtime_value) ⇒ Object

runtime value -> WirePair



80
81
82
# File 'lib/fusion/cli.rb', line 80

def serialize(runtime_value)
  Serializer.serialize(runtime_value)
end