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, PdfRenderWithWarnings, RustBuffer, TextRenderWithWarnings, ValidationError

Class Method Summary collapse

Class Method Details

.chord_diagram_svg(chord, instrument) ⇒ Object



885
886
887
888
889
890
891
892
893
894
# File 'lib/chordsketch_uniffi.rb', line 885

def self.chord_diagram_svg(chord, instrument)
    chord = Chordsketch::uniffi_utf8(chord)
    
    
    instrument = Chordsketch::uniffi_utf8(instrument)
    
    
  result = Chordsketch.rust_call_with_error(ChordSketchError,:uniffi_chordsketch_ffi_fn_func_chord_diagram_svg,RustBuffer.allocFromString(chord),RustBuffer.allocFromString(instrument))
  return result.consumeIntoOptionalstring
end

.parse_and_render_html(input, config_json, transpose) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/chordsketch_uniffi.rb', line 900

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_html_with_warnings(input, config_json, transpose) ⇒ Object



918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/chordsketch_uniffi.rb', line 918

def self.parse_and_render_html_with_warnings(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_with_warnings,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoTypeTextRenderWithWarnings
end

.parse_and_render_pdf(input, config_json, transpose) ⇒ Object



936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/chordsketch_uniffi.rb', line 936

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_pdf_with_warnings(input, config_json, transpose) ⇒ Object



954
955
956
957
958
959
960
961
962
963
964
965
966
# File 'lib/chordsketch_uniffi.rb', line 954

def self.parse_and_render_pdf_with_warnings(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_with_warnings,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoTypePdfRenderWithWarnings
end

.parse_and_render_text(input, config_json, transpose) ⇒ Object



972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/chordsketch_uniffi.rb', line 972

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

.parse_and_render_text_with_warnings(input, config_json, transpose) ⇒ Object



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/chordsketch_uniffi.rb', line 990

def self.parse_and_render_text_with_warnings(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_with_warnings,RustBuffer.allocFromString(input),RustBuffer.alloc_from_Optionalstring(config_json),RustBuffer.alloc_from_Optionali8(transpose))
  return result.consumeIntoTypeTextRenderWithWarnings
end

.rust_call(fn_name, *args) ⇒ Object



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

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



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/chordsketch_uniffi.rb', line 689

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



1008
1009
1010
1011
1012
1013
1014
# File 'lib/chordsketch_uniffi.rb', line 1008

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

.versionObject



1020
1021
1022
1023
# File 'lib/chordsketch_uniffi.rb', line 1020

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