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 +4 -4
- data/Cargo.toml +2 -1
- data/Gemfile +0 -3
- data/lib/mrml/error.rb +4 -0
- data/lib/mrml/native.rb +15 -0
- data/lib/mrml/result.rb +9 -0
- data/lib/mrml/version.rb +1 -1
- data/lib/mrml.rb +10 -20
- data/src/lib.rs +45 -17
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb8417eb9099012242823d444d1168502a1aba8276e3917760b8c4b7957e4cf0
|
4
|
+
data.tar.gz: 6a6cba475b50378d2e06c184a4a8ec90b56b5db72348e7e1fb352fd9364bb8bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d8487f6ce51e9945ed284ba01dafaf0a0741e1138f31fdb5e81d648302c68781850e8d73879c99653da06167c987727dd77dfd8ee37eb9be4915b38c398a4bb
|
7
|
+
data.tar.gz: 010ee0e3c32f11ca6f2cd526646c8429b9ad22b937a70ba02c2c59e4eef335d4ca67f489b682c0431d51639b52b90738a9b7f3fb382583fb619276470eff09de
|
data/Cargo.toml
CHANGED
data/Gemfile
CHANGED
data/lib/mrml/error.rb
ADDED
data/lib/mrml/native.rb
ADDED
@@ -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
|
data/lib/mrml/result.rb
ADDED
data/lib/mrml/version.rb
CHANGED
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(:
|
10
|
+
call(:to_title, template)
|
23
11
|
end
|
24
12
|
|
25
13
|
def to_preview(template)
|
26
|
-
call(:
|
14
|
+
call(:to_preview, template)
|
27
15
|
end
|
28
16
|
|
29
17
|
def to_html(template)
|
30
|
-
call(:
|
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
|
-
|
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(
|
7
|
-
unsafe {
|
8
|
-
|
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
|
13
|
-
|
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) -> *
|
18
|
-
|
19
|
-
|
20
|
-
|
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) -> *
|
26
|
-
|
27
|
-
|
28
|
-
|
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) -> *
|
34
|
-
|
35
|
-
|
36
|
-
|
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.
|
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-
|
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.
|
128
|
+
rubygems_version: 3.1.6
|
126
129
|
signing_key:
|
127
130
|
specification_version: 4
|
128
131
|
summary: Ruby wrapper for MRML Rust
|