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.1"

Class Method Summary collapse

Class Method Details

.compile(tmpdir) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/irust.rb', line 27

def compile(tmpdir)
  Dir.chdir(tmpdir) do
    stdin, stderr, status = Open3.capture3("rustc irust.rs")

    if status.success?
      true
    else
      STDERR.puts stderr
      false
    end
  end
end

.detect_rustObject



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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/irust.rb', line 40

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

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

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

.readObject



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

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

.runObject



56
57
58
59
60
# File 'lib/irust.rb', line 56

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

.rust_program(line, history) ⇒ Object



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

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