Module: Chordsketch

Defined in:
lib/chordsketch.rb,
lib/chordsketch_uniffi.rb

Overview

Detect the platform and pre-load the correct native library before requiring the UniFFI-generated bindings.

Defined Under Namespace

Modules: ChordSketchError, NativeLoader Classes: InternalError, RustBuffer

Class Method Summary collapse

Class Method Details

.parse_and_render_html(input, config_json, transpose) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/chordsketch_uniffi.rb', line 627

def self.parse_and_render_html(input, config_json, transpose)
    input = Chordsketch::uniffi_utf8(input)
    
    
    config_json = (config_json ? Chordsketch::uniffi_utf8(config_json) : nil)
    RustBuffer.check_lower_Optionalstring(config_json)
    
    transpose = (transpose ? Chordsketch::uniffi_in_range(transpose, "i8", -2**7, 2**7) : nil)
    RustBuffer.check_lower_Optionali8(transpose)
    
  result = Chordsketch.rust_call_with_error(ChordSketchError,:uniffi_chordsketch_ffi_fn_func_parse_and_render_html,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoString
end

.parse_and_render_pdf(input, config_json, transpose) ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/chordsketch_uniffi.rb', line 645

def self.parse_and_render_pdf(input, config_json, transpose)
    input = Chordsketch::uniffi_utf8(input)
    
    
    config_json = (config_json ? Chordsketch::uniffi_utf8(config_json) : nil)
    RustBuffer.check_lower_Optionalstring(config_json)
    
    transpose = (transpose ? Chordsketch::uniffi_in_range(transpose, "i8", -2**7, 2**7) : nil)
    RustBuffer.check_lower_Optionali8(transpose)
    
  result = Chordsketch.rust_call_with_error(ChordSketchError,:uniffi_chordsketch_ffi_fn_func_parse_and_render_pdf,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoBytes
end

.parse_and_render_text(input, config_json, transpose) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/chordsketch_uniffi.rb', line 663

def self.parse_and_render_text(input, config_json, transpose)
    input = Chordsketch::uniffi_utf8(input)
    
    
    config_json = (config_json ? Chordsketch::uniffi_utf8(config_json) : nil)
    RustBuffer.check_lower_Optionalstring(config_json)
    
    transpose = (transpose ? Chordsketch::uniffi_in_range(transpose, "i8", -2**7, 2**7) : nil)
    RustBuffer.check_lower_Optionali8(transpose)
    
  result = Chordsketch.rust_call_with_error(ChordSketchError,:uniffi_chordsketch_ffi_fn_func_parse_and_render_text,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoString
end

.rust_call(fn_name, *args) ⇒ Object



517
518
519
520
# File 'lib/chordsketch_uniffi.rb', line 517

def self.rust_call(fn_name, *args)
  # Call a rust function
  rust_call_with_error(nil, fn_name, *args)
end

.rust_call_with_error(error_module, fn_name, *args) ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/chordsketch_uniffi.rb', line 522

def self.rust_call_with_error(error_module, fn_name, *args)
  # Call a rust function and handle errors
  #
  # Use this when the rust function returns a Result<>.  error_module must be the error_module that corresponds to that Result.


  # Note: RustCallStatus.new zeroes out the struct, which is exactly what we
  # want to pass to Rust (code=0, error_buf=RustBuffer(len=0, capacity=0,
  # data=NULL))
  status = RustCallStatus.new
  args << status

  result = UniFFILib.public_send(fn_name, *args)

  case status.code
  when CALL_SUCCESS
    result
  when CALL_ERROR
    if error_module.nil?
      status.error_buf.free
      raise InternalError, "CALL_ERROR with no error_module set"
    else
      raise consume_buffer_into_error(error_module, status.error_buf)
    end
  when CALL_PANIC
    # When the rust code sees a panic, it tries to construct a RustBuffer
    # with the message.  But if that code panics, then it just sends back
    # an empty buffer.
    if status.error_buf.len > 0
      raise InternalError, status.error_buf.consumeIntoString()
    else
      raise InternalError, "Rust panic"
    end
  else
    raise InternalError, "Unknown call status: #{status.code}"
  end
end

.uniffi_bytes(v) ⇒ Object

Raises:

  • (TypeError)


34
35
36
37
# File 'lib/chordsketch_uniffi.rb', line 34

def self.uniffi_bytes(v)
  raise TypeError, "no implicit conversion of #{v} into String" unless v.respond_to?(:to_str)
  v.to_str
end

.uniffi_in_range(i, type_name, min, max) ⇒ Object

Raises:

  • (TypeError)


20
21
22
23
24
25
# File 'lib/chordsketch_uniffi.rb', line 20

def self.uniffi_in_range(i, type_name, min, max)
  raise TypeError, "no implicit conversion of #{i} into Integer" unless i.respond_to?(:to_int)
  i = i.to_int
  raise RangeError, "#{type_name} requires #{min} <= value < #{max}" unless (min <= i && i < max)
  i
end

.uniffi_utf8(v) ⇒ Object

Raises:

  • (TypeError)


27
28
29
30
31
32
# File 'lib/chordsketch_uniffi.rb', line 27

def self.uniffi_utf8(v)
  raise TypeError, "no implicit conversion of #{v} into String" unless v.respond_to?(:to_str)
  v = v.to_str.encode(Encoding::UTF_8)
  raise Encoding::InvalidByteSequenceError, "not a valid UTF-8 encoded string" unless v.valid_encoding?
  v
end

.validate(input) ⇒ Object



681
682
683
684
685
686
687
# File 'lib/chordsketch_uniffi.rb', line 681

def self.validate(input)
    input = Chordsketch::uniffi_utf8(input)
    
    
  result = Chordsketch.rust_call(:uniffi_chordsketch_ffi_fn_func_validate,RustBuffer.allocFromString(input))
  return result.consumeIntoSequencestring
end

.versionObject



693
694
695
696
# File 'lib/chordsketch_uniffi.rb', line 693

def self.version()
  result = Chordsketch.rust_call(:uniffi_chordsketch_ffi_fn_func_version,)
  return result.consumeIntoString
end