Module: IRust

Defined in:
lib/irust.rb,
lib/irust/version.rb,
lib/irust/template_renderer.rb

Defined Under Namespace

Classes: TemplateRenderer

Constant Summary collapse

VERSION =
"1.1.2"

Class Method Summary collapse

Class Method Details

.compile(tmpdir, src) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/irust.rb', line 28

def compile(tmpdir, src)
  Dir.chdir(tmpdir) do
    stdin, stderr, status = Open3.capture3("rustc irust.rs")
    return true if status.success?

    error = "--- BEGIN FAILED PROGRAM ---\n"
    src.split("\n").each_with_index do |line, i|
        error << "#{i + 1}: #{line}\n"
    end
    error << "---- END FAILED PROGRAM ----"

    STDERR.puts error.red
    STDERR.puts stderr
    false
  end
end

.detect_rustObject



11
12
13
14
15
16
17
18
# File 'lib/irust.rb', line 11

def detect_rust
  rust_version = `rustc -v`
  puts "Using #{rust_version.split("\n").first}"
rescue Errno::ENOENT
  STDERR.puts "No rustc detected! Please install Rust first!"
  STDERR.puts "See the Quick Start at: https://github.com/mozilla/rust/blob/master/README.md"
  exit 1
end

.eval(line, history = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/irust.rb', line 45

def eval(line, history = nil)
  exit 0 if line.nil?

  Dir.mktmpdir do |tmpdir|
    src     = rust_program(line, history)
    srcfile = File.join(tmpdir, "irust.rs")
    File.write(srcfile, src)

    if compile(tmpdir, src)
      system srcfile.sub(/\.rs$/, '')
      history + ";\n" + line
    else
      history
    end
  end
end

.readObject



20
21
22
# File 'lib/irust.rb', line 20

def read
  Readline.readline("irust> ", true)
end

.runObject



62
63
64
65
66
# File 'lib/irust.rb', line 62

def run
  detect_rust
  history = ""
  loop { history = eval read, history }
end

.rust_program(line, history) ⇒ Object



24
25
26
# File 'lib/irust.rb', line 24

def rust_program(line, history)
  IRust::TemplateRenderer.new(line, history).render
end