mrml 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d24641d7935465df48cb84cf4ec54134e028ccc6939c5f5353d587cc08a83a1
4
- data.tar.gz: fbdb527a1fb152b2f18965f9685c61e97f2c53b4004547016909f5b025b061d6
3
+ metadata.gz: cb8417eb9099012242823d444d1168502a1aba8276e3917760b8c4b7957e4cf0
4
+ data.tar.gz: 6a6cba475b50378d2e06c184a4a8ec90b56b5db72348e7e1fb352fd9364bb8bf
5
5
  SHA512:
6
- metadata.gz: 24421c93616103cf85ec54ac19388928a72950722d379bb9c467f17dd2b320568772bab722b39d1c2dfd25e9eb73bda93442df4db083397a3e4dc132fc27f377
7
- data.tar.gz: 9103f9892daffb15306ed66439a8948f2ff33ce702b67e6ca65cfd48fb85d7f005fd94ed5aeed5a46a7149813cd7d42062359931a669a6ab8ad6502b3202de18
6
+ metadata.gz: 8d8487f6ce51e9945ed284ba01dafaf0a0741e1138f31fdb5e81d648302c68781850e8d73879c99653da06167c987727dd77dfd8ee37eb9be4915b38c398a4bb
7
+ data.tar.gz: 010ee0e3c32f11ca6f2cd526646c8429b9ad22b937a70ba02c2c59e4eef335d4ca67f489b682c0431d51639b52b90738a9b7f3fb382583fb619276470eff09de
data/Cargo.toml CHANGED
@@ -6,7 +6,8 @@ edition = "2018"
6
6
 
7
7
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
8
  [dependencies]
9
- mrml = "0.5.0"
9
+ mrml = "1.2.6"
10
+ serde = { version = "1.0", features = ["derive"] }
10
11
 
11
12
  [lib]
12
13
  name = "mrml"
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/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
+ Native.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.6.0'.freeze
3
3
  end
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/src/lib.rs CHANGED
@@ -3,36 +3,64 @@ 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 parse_result(result: Result<String, mrml::prelude::render::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
+ fn parse_option(option: Option<String>) -> *mut c_char {
23
+ to_char(option.unwrap_or("".to_string()))
14
24
  }
15
25
 
16
26
  #[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)),
27
+ pub extern "C" fn to_title(input: *const c_char) -> *mut c_char {
28
+ let root = mrml::parse(&to_string(input));
29
+
30
+ if root.is_err() {
31
+ to_char(format!("MRML::Error {:?}", root.unwrap_err()))
32
+ } else {
33
+ parse_option(root.unwrap().get_title())
21
34
  }
22
35
  }
23
36
 
24
37
  #[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)),
38
+ pub extern "C" fn to_preview(input: *const c_char) -> *mut c_char {
39
+ let root = mrml::parse(&to_string(input));
40
+
41
+ if root.is_err() {
42
+ to_char(format!("MRML::Error {:?}", root.unwrap_err()))
43
+ } else {
44
+ parse_option(root.unwrap().get_preview())
29
45
  }
30
46
  }
31
47
 
32
48
  #[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)),
49
+ pub extern "C" fn to_html(input: *const c_char) -> *mut c_char {
50
+ let root = mrml::parse(&to_string(input));
51
+ let opts = mrml::prelude::render::Options::default();
52
+
53
+ if root.is_err() {
54
+ to_char(format!("MRML::Error {:?}", root.unwrap_err()))
55
+ } else {
56
+ parse_result(root.unwrap().render(&opts))
37
57
  }
38
58
  }
59
+
60
+ #[no_mangle]
61
+ pub extern "C" fn free_result(string: *mut c_char) {
62
+ unsafe {
63
+ if string.is_null() { return }
64
+ CString::from_raw(string)
65
+ };
66
+ }
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.6.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-11-11 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
@@ -122,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
125
  - !ruby/object:Gem::Version
123
126
  version: '0'
124
127
  requirements: []
125
- rubygems_version: 3.1.4
128
+ rubygems_version: 3.1.6
126
129
  signing_key:
127
130
  specification_version: 4
128
131
  summary: Ruby wrapper for MRML Rust