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

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

Overview

Wrapper class for Polar FFI pointer + operations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enrich_messageObject

rubocop:disable Metrics/ClassLength



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

def enrich_message
  @enrich_message
end

Class Method Details

.createFFI::Polar

Returns:

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



45
46
47
# File 'lib/oso/polar/ffi/polar.rb', line 45

def self.create
  Rust.new
end

.release(ptr) ⇒ Object



26
27
28
# File 'lib/oso/polar/ffi.rb', line 26

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

Instance Method Details

#build_data_filter(types, partials, variable, class_tag) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/oso/polar/ffi/polar.rb', line 59

def build_data_filter(types, partials, variable, class_tag)
  types = JSON.dump(types)
  partials = JSON.dump(partials)
  plan = Rust.build_data_filter(self, types, partials, variable, class_tag)
  process_messages
  plan = check_result plan
  # TODO(gw) more error checking?
  JSON.parse plan.to_s
end

#build_filter_plan(types, partials, variable, class_tag) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/oso/polar/ffi/polar.rb', line 49

def build_filter_plan(types, partials, variable, class_tag)
  types = JSON.dump(types)
  partials = JSON.dump(partials)
  plan = Rust.build_filter_plan(self, types, partials, variable, class_tag)
  process_messages
  plan = check_result plan
  # TODO(gw) more error checking?
  JSON.parse plan.to_s
end

#check_result(res) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/oso/polar/ffi/polar.rb', line 159

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

#clear_rulesObject

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



78
79
80
81
82
# File 'lib/oso/polar/ffi/polar.rb', line 78

def clear_rules
  cleared = Rust.clear_rules(self)
  process_messages
  check_result cleared
end

#load(sources) ⇒ Object

Parameters:

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



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

def load(sources)
  loaded = Rust.load(self, JSON.dump(sources))
  process_messages
  check_result loaded
end

#new_idInteger

Returns:

  • (Integer)

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



95
96
97
# File 'lib/oso/polar/ffi/polar.rb', line 95

def new_id
  Rust.new_id(self)
end

#new_query_from_str(str) ⇒ FFI::Query

Parameters:

  • str (String)

    Query string.

Returns:

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



102
103
104
105
106
# File 'lib/oso/polar/ffi/polar.rb', line 102

def new_query_from_str(str)
  query = Rust.new_query_from_str(self, str, 0)
  process_messages
  check_result query
end

#new_query_from_term(term) ⇒ FFI::Query

Parameters:

  • term (Hash<String, Object>)

Returns:

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



111
112
113
114
115
# File 'lib/oso/polar/ffi/polar.rb', line 111

def new_query_from_term(term)
  query = Rust.new_query_from_term(self, JSON.dump(term), 0)
  process_messages
  check_result query
end

#next_inline_queryFFI::Query?

Returns:

  • (FFI::Query)

    if there are remaining inline queries.

  • (nil)

    if there are no remaining inline queries.

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



87
88
89
90
91
# File 'lib/oso/polar/ffi/polar.rb', line 87

def next_inline_query
  query = Rust.next_inline_query(self, 0)
  process_messages
  query.null? ? nil : query
end

#next_messageObject



133
134
135
# File 'lib/oso/polar/ffi/polar.rb', line 133

def next_message
  check_result Rust.next_message(self)
end

#process_message(message, enrich_message) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/oso/polar/ffi/polar.rb', line 137

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

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

#process_messagesObject



150
151
152
153
154
155
156
157
# File 'lib/oso/polar/ffi/polar.rb', line 150

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

    process_message(message, enrich_message)
  end
end

#register_constant(value, name:) ⇒ Object

Parameters:

  • name (String)
  • value (Hash<String, Object>)

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



120
121
122
123
# File 'lib/oso/polar/ffi/polar.rb', line 120

def register_constant(value, name:)
  registered = Rust.register_constant(self, name, JSON.dump(value))
  check_result registered
end

#register_mro(name, mro) ⇒ Object

Parameters:

  • name (String)
  • mro (Array<Integer>)

Raises:

  • (FFI::Error)

    if the FFI call returns an error.



128
129
130
131
# File 'lib/oso/polar/ffi/polar.rb', line 128

def register_mro(name, mro)
  registered = Rust.register_mro(self, name, JSON.dump(mro))
  check_result registered
end

#zero?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/oso/polar/ffi.rb', line 22

def zero?
  null?
end