Skip to content

Commit a8181c3

Browse files
committed
add emoji methods to cffi
1 parent ead4ecf commit a8181c3

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

message_parser_ffi/message_parser.h

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ extern "C" {
1414
#endif
1515

1616

17+
#include <stddef.h>
18+
#include <stdint.h>
19+
20+
/** \brief
21+
* Count emojis in a message, if there are only emojis.
22+
*
23+
* This is used to display messages with only emojis in a larger font size.
24+
*/
25+
uint32_t
26+
mp_count_emojis_if_only_contains_emoji (
27+
char const * text);
28+
29+
/** \brief
30+
* frees a string managed by rust
31+
*/
32+
void
33+
mp_free_rust_string (
34+
char * string);
35+
36+
1737
#include <stdbool.h>
1838

1939
/** <No documentation available> */
@@ -40,12 +60,15 @@ typedef struct TextResultForQt {
4060
* frees the TextResultForQt
4161
*/
4262
void
43-
free_text_result_for_qt (
63+
mp_free_text_result_for_qt (
4464
TextResultForQt_t result);
4565

46-
47-
#include <stddef.h>
48-
#include <stdint.h>
66+
/** \brief
67+
* get_first_emoji of text, result needs to be freed with `mp_free_rust_string`
68+
*/
69+
char *
70+
mp_get_first_emoji (
71+
char const * text);
4972

5073
/** \brief
5174
* Modes of the parser, which element set to parse
@@ -84,15 +107,15 @@ ParsingMode_t;
84107
* Pretty-prints a TextResultForQt using Rust's formatting logic.
85108
*/
86109
TextResultForQt_t
87-
parse_to_text_result_for_qt (
110+
mp_parse_to_text_result_for_qt (
88111
char const * text,
89112
ParsingMode_t mode);
90113

91114
/** \brief
92115
* Pretty-prints a TextResultForQt using Rust's formatting logic.
93116
*/
94117
void
95-
print_text_result_for_qt (
118+
mp_print_text_result_for_qt (
96119
TextResultForQt_t const * result);
97120

98121

message_parser_ffi/smoke_test/main.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33

44
#include "../message_parser.h"
55

6-
int
7-
main (int argc, char const * const argv[])
8-
{
9-
char text[] = "hello **world**. go to delta.chat";
10-
struct TextResultForQt result = parse_to_text_result_for_qt(text, PARSING_MODE_TEXT);
11-
print_text_result_for_qt(&result);
12-
printf("result: %s", result.html);
6+
int main(int argc, char const *const argv[]) {
7+
char text[] = "hello **world**. go to delta.chat";
8+
struct TextResultForQt result =
9+
mp_parse_to_text_result_for_qt(text, PARSING_MODE_TEXT);
10+
mp_print_text_result_for_qt(&result);
11+
printf("\nresult: %s", result.html);
12+
mp_free_text_result_for_qt(result);
13+
14+
int emoji_count = mp_count_emojis_if_only_contains_emoji(
15+
"🇩🇪😅🧑‍🎨👨‍👩‍👧👩🏽‍🌾");
16+
printf("\nemoji count: %d, should be 5", emoji_count);
17+
18+
char *first_emoji = mp_get_first_emoji("👩🏽‍🌾 Farmers");
19+
printf("\nfirst emoji of the string: \n%s\n", first_emoji);
20+
mp_free_rust_string(first_emoji);
1321
}

message_parser_ffi/src/lib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ pub struct TextResultForQt {
5151

5252
/// Pretty-prints a TextResultForQt using Rust's formatting logic.
5353
#[ffi_export]
54-
pub fn print_text_result_for_qt(result: &TextResultForQt) {
54+
pub fn mp_print_text_result_for_qt(result: &TextResultForQt) {
5555
println!("{:?}", result);
5656
}
5757

5858
/// frees the TextResultForQt
5959
#[ffi_export]
60-
pub fn free_text_result_for_qt(result: TextResultForQt) {
60+
pub fn mp_free_text_result_for_qt(result: TextResultForQt) {
6161
drop(result);
6262
}
6363

@@ -124,7 +124,7 @@ fn element_to_qt_html(input: &Element) -> String {
124124

125125
/// Pretty-prints a TextResultForQt using Rust's formatting logic.
126126
#[ffi_export]
127-
pub fn parse_to_text_result_for_qt(text: char_p_ref<'_>, mode: ParsingMode) -> TextResultForQt {
127+
pub fn mp_parse_to_text_result_for_qt(text: char_p_ref<'_>, mode: ParsingMode) -> TextResultForQt {
128128
let input = text.to_str();
129129
let elements = match mode {
130130
ParsingMode::Text => deltachat_message_parser::parser::parse_only_text(input),
@@ -146,6 +146,31 @@ pub fn parse_to_text_result_for_qt(text: char_p_ref<'_>, mode: ParsingMode) -> T
146146
}
147147
}
148148

149+
/// get_first_emoji of text, result needs to be freed with `mp_free_rust_string`
150+
#[ffi_export]
151+
pub fn mp_get_first_emoji(text: char_p_ref<'_>) -> char_p_boxed {
152+
let input = text.to_str();
153+
154+
match deltachat_message_parser::parser::is_emoji::get_first_emoji(input) {
155+
Some(emoji) => char_p_boxed::from(CString::new(emoji).unwrap()),
156+
None => char_p_boxed::from(CString::new("").unwrap()),
157+
}
158+
}
159+
160+
/// Count emojis in a message, if there are only emojis.
161+
///
162+
/// This is used to display messages with only emojis in a larger font size.
163+
#[ffi_export]
164+
pub fn mp_count_emojis_if_only_contains_emoji(text: char_p_ref<'_>) -> u32 {
165+
deltachat_message_parser::parser::is_emoji::count_emojis_if_only_contains_emoji(text.to_str()).unwrap_or(0)
166+
}
167+
168+
/// frees a string managed by rust
169+
#[ffi_export]
170+
pub fn mp_free_rust_string(string: char_p_boxed) {
171+
drop(string);
172+
}
173+
149174
// The following function is only necessary for the header generation.
150175
#[cfg(feature = "headers")] // c.f. the `Cargo.toml` section
151176
pub fn generate_headers() -> ::std::io::Result<()> {

0 commit comments

Comments
 (0)