mrml 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d24641d7935465df48cb84cf4ec54134e028ccc6939c5f5353d587cc08a83a1
4
- data.tar.gz: fbdb527a1fb152b2f18965f9685c61e97f2c53b4004547016909f5b025b061d6
3
+ metadata.gz: 695828a62cfc39187d156c7744d583d1809df22f4c41fe903c7d56eb2b509f37
4
+ data.tar.gz: 918b732a91c2067a0f94747c1ff2a08d740e4c5a5e2c9db05b3ce8b63fee4dfb
5
5
  SHA512:
6
- metadata.gz: 24421c93616103cf85ec54ac19388928a72950722d379bb9c467f17dd2b320568772bab722b39d1c2dfd25e9eb73bda93442df4db083397a3e4dc132fc27f377
7
- data.tar.gz: 9103f9892daffb15306ed66439a8948f2ff33ce702b67e6ca65cfd48fb85d7f005fd94ed5aeed5a46a7149813cd7d42062359931a669a6ab8ad6502b3202de18
6
+ metadata.gz: 8eb9fd6fbc6598ddc81d446ef85110501641012f2da38db55b1278e0563a616c54beb87409bdee5074cb6bb4abba509ee6850ca22a0a1f1508249bf37acf3f55
7
+ data.tar.gz: 224d11acaf11a084058fd7b86887280aaaa0944017fa0f872b72bb567a25b75839828de807adf22204421409e47b6605cb00b341cd36c4bd233bf53e4ca7ebe9
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mrml.gemspec
4
4
  gemspec
5
-
6
- gem 'rake', '~> 12.0'
7
- gem 'minitest', '~> 5.0'
data/lib/mrml.rb CHANGED
@@ -1,33 +1,21 @@
1
1
  require 'ffi'
2
+ require 'mrml/result'
3
+ require 'mrml/native'
4
+ require 'mrml/error'
2
5
  require 'mrml/version'
3
6
 
4
7
  module MRML
5
- class Error < StandardError
6
- end
7
-
8
8
  class << self
9
- extend FFI::Library
10
-
11
- ffi_lib File.expand_path("mrml.#{FFI::Platform::LIBSUFFIX}", __dir__)
12
-
13
- attach_function :__to_title, :to_title, [:string], :string
14
- attach_function :__to_preview, :to_preview, [:string], :string
15
- attach_function :__to_html, :to_html, [:string], :string
16
-
17
- private :__to_title
18
- private :__to_preview
19
- private :__to_html
20
-
21
9
  def to_title(template)
22
- call(:__to_title, template)
10
+ call(:to_title, template)
23
11
  end
24
12
 
25
13
  def to_preview(template)
26
- call(:__to_preview, template)
14
+ call(:to_preview, template)
27
15
  end
28
16
 
29
17
  def to_html(template)
30
- call(:__to_html, template)
18
+ call(:to_html, template)
31
19
  end
32
20
 
33
21
  private
@@ -35,8 +23,10 @@ module MRML
35
23
  def call(method, template)
36
24
  return if template.nil?
37
25
 
38
- result = send(method, template)
39
- raise Error, result if result.start_with?('Error')
26
+ result = Native.send(method, template)
27
+ result = result.read_string.force_encoding('UTF-8')
28
+
29
+ raise Error, result if result.start_with?(Error.name)
40
30
 
41
31
  result
42
32
  end
data/lib/mrml/error.rb ADDED
@@ -0,0 +1,4 @@
1
+ module MRML
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ module MRML
2
+ module Native
3
+ extend FFI::Library
4
+
5
+ LIBNAME = "mrml.#{FFI::Platform::LIBSUFFIX}".freeze
6
+ LIBPATH = File.expand_path('..', __dir__).freeze
7
+
8
+ ffi_lib File.expand_path(LIBNAME, LIBPATH)
9
+
10
+ attach_function :to_title, :to_title, [:string], Result
11
+ attach_function :to_preview, :to_preview, [:string], Result
12
+ attach_function :to_html, :to_html, [:string], Result
13
+ attach_function :free, :free_result, [Result], :void
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module MRML
2
+ class Result < FFI::AutoPointer
3
+ class << self
4
+ def release(ptr)
5
+ Binding.free(ptr)
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/mrml/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MRML
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
data/src/lib.rs CHANGED
@@ -3,36 +3,41 @@ use std::ffi::{CStr, CString};
3
3
 
4
4
  use mrml;
5
5
 
6
- fn to_string(input: *const c_char) -> String {
7
- unsafe {
8
- CStr::from_ptr(input).to_string_lossy().into_owned()
6
+ fn to_string(unsafe_string: *const c_char) -> String {
7
+ unsafe { CStr::from_ptr(unsafe_string) }.to_str().unwrap().to_string()
8
+ }
9
+
10
+ fn to_char(string: String) -> *mut c_char {
11
+ CString::new(string).unwrap().into_raw()
12
+ }
13
+
14
+ fn to_result(result: Result<String, mrml::Error>) -> *mut c_char {
15
+ if result.is_err() {
16
+ to_char(format!("MRML::Error {:?}", result.unwrap_err()))
17
+ } else {
18
+ to_char(result.unwrap())
9
19
  }
10
20
  }
11
21
 
12
- fn to_char(str: &str) -> *const c_char {
13
- CString::new(str).unwrap().into_raw()
22
+ #[no_mangle]
23
+ pub extern "C" fn to_title(input: *const c_char) -> *mut c_char {
24
+ to_result(mrml::to_title(&to_string(input), mrml::Options::default()))
14
25
  }
15
26
 
16
27
  #[no_mangle]
17
- pub extern "C" fn to_title(input: *const c_char) -> *const c_char {
18
- match mrml::to_title(&to_string(input), mrml::Options::default()) {
19
- Ok(html) => return to_char(&html),
20
- Err(err) => return to_char(&format!("Error: {:?}", err)),
21
- }
28
+ pub extern "C" fn to_preview(input: *const c_char) -> *mut c_char {
29
+ to_result(mrml::to_preview(&to_string(input), mrml::Options::default()))
22
30
  }
23
31
 
24
32
  #[no_mangle]
25
- pub extern "C" fn to_preview(input: *const c_char) -> *const c_char {
26
- match mrml::to_preview(&to_string(input), mrml::Options::default()) {
27
- Ok(html) => return to_char(&html),
28
- Err(err) => return to_char(&format!("Error: {:?}", err)),
29
- }
33
+ pub extern "C" fn to_html(input: *const c_char) -> *mut c_char {
34
+ to_result(mrml::to_html(&to_string(input), mrml::Options::default()))
30
35
  }
31
36
 
32
37
  #[no_mangle]
33
- pub extern "C" fn to_html(input: *const c_char) -> *const c_char {
34
- match mrml::to_html(&to_string(input), mrml::Options::default()) {
35
- Ok(html) => return to_char(&html),
36
- Err(err) => return to_char(&format!("Error: {:?}", err)),
37
- }
38
+ pub extern "C" fn free_result(string: *mut c_char) {
39
+ unsafe {
40
+ if string.is_null() { return }
41
+ CString::from_raw(string)
42
+ };
38
43
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -100,6 +100,9 @@ files:
100
100
  - bin/setup
101
101
  - ext/Rakefile
102
102
  - lib/mrml.rb
103
+ - lib/mrml/error.rb
104
+ - lib/mrml/native.rb
105
+ - lib/mrml/result.rb
103
106
  - lib/mrml/version.rb
104
107
  - mrml.gemspec
105
108
  - src/lib.rs