Class: Oso::Polar::FFI::Query

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/oso/polar/ffi.rb,
lib/oso/polar/ffi/query.rb

Overview

Wrapper class for Query FFI pointer + operations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enrich_messageObject

Returns the value of attribute enrich_message.



10
11
12
# File 'lib/oso/polar/ffi/query.rb', line 10

def enrich_message
  @enrich_message
end

Class Method Details

.release(ptr) ⇒ Object



41
42
43
# File 'lib/oso/polar/ffi.rb', line 41

def self.release(ptr)
  Rust.free(ptr) unless ptr.null?
end

Instance Method Details

#application_error(message) ⇒ Object

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



56
57
58
59
# File 'lib/oso/polar/ffi/query.rb', line 56

def application_error(message)
  res = Rust.application_error(self, message)
  check_result res
end

#bind(name, value) ⇒ Object



71
72
73
74
# File 'lib/oso/polar/ffi/query.rb', line 71

def bind(name, value)
  res = Rust.bind(self, name, JSON.dump(value))
  check_result res
end

#call_result(value, call_id:) ⇒ Object

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



40
41
42
43
# File 'lib/oso/polar/ffi/query.rb', line 40

def call_result(value, call_id:)
  res = Rust.call_result(self, call_id, JSON.dump(value))
  check_result res
end

#check_result(res) ⇒ Object

Unwrap the result by (a) extracting the pointers for result and error, (b) freeing the result pointers, and then (c) either returning the result pointer, or constructing and raising the error.



116
117
118
119
120
121
122
123
124
125
# File 'lib/oso/polar/ffi/query.rb', line 116

def check_result(res)
  result = res[:result]
  error = res[:error]
  Rust.result_free(res)

  raise 'internal error: both result and error pointers are not null' if !error.null? && !result.zero?
  raise FFI::Error.get(error, enrich_message) unless error.null?

  result
end

#debug_command(cmd) ⇒ Object

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



31
32
33
34
35
# File 'lib/oso/polar/ffi/query.rb', line 31

def debug_command(cmd)
  res = Rust.debug_command(self, cmd)
  process_messages
  check_result res
end

#next_event::Oso::Polar::QueryEvent

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



63
64
65
66
67
68
69
# File 'lib/oso/polar/ffi/query.rb', line 63

def next_event
  event = Rust.next_event(self)
  process_messages
  event = check_result event

  ::Oso::Polar::QueryEvent.new(JSON.parse(event.to_s))
end

#next_messageObject



76
77
78
# File 'lib/oso/polar/ffi/query.rb', line 76

def next_message
  check_result Rust.next_message(self)
end

#process_message(message, enrich_message) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/oso/polar/ffi/query.rb', line 80

def process_message(message, enrich_message)
  message = JSON.parse(message.to_s)
  kind = message['kind']
  msg = message['msg']
  msg = enrich_message.call(msg)

  case kind
  when 'Print'
    puts(msg)
  when 'Warning'
    warn(format('[warning] %<msg>s', msg: msg))
  end
end

#process_messagesObject



94
95
96
97
98
99
100
101
# File 'lib/oso/polar/ffi/query.rb', line 94

def process_messages
  loop do
    message = next_message
    break if message.null?

    process_message(message, enrich_message)
  end
end

#question_result(result, call_id:) ⇒ Object

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



48
49
50
51
52
# File 'lib/oso/polar/ffi/query.rb', line 48

def question_result(result, call_id:)
  result = result ? 1 : 0
  res = Rust.question_result(self, call_id, result)
  check_result res
end

#sourceString

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



105
106
107
108
109
110
# File 'lib/oso/polar/ffi/query.rb', line 105

def source
  res = Rust.source(self)
  res = check_result res

  res.to_s
end

#zero?Boolean



37
38
39
# File 'lib/oso/polar/ffi.rb', line 37

def zero?
  null?
end