|
| 1 | +extern crate serde; |
| 2 | +extern crate serde_json; |
| 3 | +extern crate libc; |
| 4 | +use std::slice; |
| 5 | + |
| 6 | +use libc::{c_char, size_t}; |
| 7 | +use serde::{Serialize, Deserialize}; |
| 8 | + |
| 9 | +/******************************************************/ |
| 10 | +/******************************************************/ |
| 11 | +/** |
| 12 | + * Warning: the C++ code may not generate the same JSON. |
| 13 | + */ |
| 14 | + /******************************************************/ |
| 15 | + /******************************************************/ |
| 16 | + |
| 17 | +// This has no equivalent in C++: |
| 18 | +#[derive(Serialize, Deserialize)] |
| 19 | +pub struct Metadata { |
| 20 | + result_type: String, |
| 21 | + iso_language_code: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Serialize, Deserialize)] |
| 25 | +pub struct User { |
| 26 | + id: i64, |
| 27 | + id_str: String, |
| 28 | + name: String, |
| 29 | + screen_name: String, |
| 30 | + location: String, |
| 31 | + description: String, |
| 32 | + // C++ does not have those: |
| 33 | + // url: Option<String>, |
| 34 | + //protected: bool, |
| 35 | + //listed_count: i64, |
| 36 | + //created_at: String, |
| 37 | + //favourites_count: i64, |
| 38 | + //utc_offset: Option<i64>, |
| 39 | + //time_zone: Option<String>, |
| 40 | + //geo_enabled: bool, |
| 41 | + verified: bool, |
| 42 | + followers_count: i64, |
| 43 | + friends_count: i64, |
| 44 | + statuses_count: i64, |
| 45 | + // C++ does not have those: |
| 46 | + //lang: String, |
| 47 | + //profile_background_color: String, |
| 48 | + //profile_background_image_url: String, |
| 49 | + //profile_background_image_url_https: String, |
| 50 | + //profile_background_tile: bool, |
| 51 | + //profile_image_url: String, |
| 52 | + //profile_image_url_https: String, |
| 53 | + //profile_banner_url: Option<String>, |
| 54 | + //profile_link_color: String, |
| 55 | + //profile_sidebar_border_color: String, |
| 56 | + //profile_sidebar_fill_color: String, |
| 57 | + //profile_text_color: String, |
| 58 | + //profile_use_background_image: bool, |
| 59 | + //default_profile: bool, |
| 60 | + //default_profile_image: bool, |
| 61 | + //following: bool, |
| 62 | + //follow_request_sent: bool, |
| 63 | + //notifications: bool, |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Serialize, Deserialize)] |
| 67 | +pub struct Hashtag { |
| 68 | + text: String, |
| 69 | + |
| 70 | + // C++ has those but D. Lemire does not know what they are, they don't appear in the JSON: |
| 71 | + // int64_t indices_start; |
| 72 | + // int64_t indices_end; |
| 73 | +} |
| 74 | + |
| 75 | +#[derive(Serialize, Deserialize)] |
| 76 | +pub struct Url { |
| 77 | + url: String, |
| 78 | + expanded_url: String, |
| 79 | + display_url: String, |
| 80 | + // C++ has those but D. Lemire does not know what they are, they don't appear in the JSON: |
| 81 | + // int64_t indices_start; |
| 82 | + // int64_t indices_end; |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Serialize, Deserialize)] |
| 86 | +pub struct UserMention { |
| 87 | + id: i64, |
| 88 | + name: String, |
| 89 | + screen_name: String, |
| 90 | + // Not in the C++ equivalent: |
| 91 | + //id_str: String, |
| 92 | + //indices: Vec<i64>, |
| 93 | + // C++ has those but D. Lemire does not know what they are, they don't appear in the JSON: |
| 94 | + // int64_t indices_start; |
| 95 | + // int64_t indices_end; |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Serialize, Deserialize)] |
| 99 | +pub struct Entities { |
| 100 | + hashtags: Vec<Hashtag>, |
| 101 | + urls: Vec<Url>, |
| 102 | + user_mentions: Vec<UserMention>, |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Serialize, Deserialize)] |
| 106 | +pub struct Status { |
| 107 | + created_at: String, |
| 108 | + id: i64, |
| 109 | + text: String, |
| 110 | + user: User, |
| 111 | + entities: Entities, |
| 112 | + retweet_count: i64, |
| 113 | + favorite_count: i64, |
| 114 | + favorited: bool, |
| 115 | + retweeted: bool, |
| 116 | + // None of these are in the C++ equivalent: |
| 117 | + /* |
| 118 | + metadata: Metadata, |
| 119 | + id_str: String, |
| 120 | + source: String, |
| 121 | + truncated: bool, |
| 122 | + in_reply_to_status_id: Option<i64>, |
| 123 | + in_reply_to_status_id_str: Option<String>, |
| 124 | + in_reply_to_user_id: Option<i64>, |
| 125 | + in_reply_to_user_id_str: Option<String>, |
| 126 | + in_reply_to_screen_name: Option<String>, |
| 127 | + geo: Option<String>, |
| 128 | + coordinates: Option<String>, |
| 129 | + place: Option<String>, |
| 130 | + contributors: Option<String>, |
| 131 | + lang: String, |
| 132 | + */ |
| 133 | +} |
| 134 | + |
| 135 | +#[derive(Serialize, Deserialize)] |
| 136 | +pub struct TwitterData { |
| 137 | + statuses: Vec<Status>, |
| 138 | +} |
| 139 | + |
| 140 | +#[no_mangle] |
| 141 | +pub unsafe extern "C" fn twitter_from_str(raw_input: *const c_char, raw_input_length: size_t) -> *mut TwitterData { |
| 142 | + let input = std::str::from_utf8_unchecked(slice::from_raw_parts(raw_input as *const u8, raw_input_length)); |
| 143 | + match serde_json::from_str(&input) { |
| 144 | + Ok(result) => Box::into_raw(Box::new(result)), |
| 145 | + Err(_) => std::ptr::null_mut(), |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +#[no_mangle] |
| 150 | +pub unsafe extern "C" fn str_from_twitter(raw: *mut TwitterData) -> *const c_char { |
| 151 | + let twitter_thing = { &*raw }; |
| 152 | + let serialized = serde_json::to_string(&twitter_thing).unwrap(); |
| 153 | + return std::ffi::CString::new(serialized.as_str()).unwrap().into_raw() |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +#[no_mangle] |
| 158 | +pub unsafe extern "C" fn free_twitter(raw: *mut TwitterData) { |
| 159 | + if raw.is_null() { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + drop(Box::from_raw(raw)) |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +#[no_mangle] |
| 168 | +pub unsafe extern fn free_string(ptr: *const c_char) { |
| 169 | + let _ = std::ffi::CString::from_raw(ptr as *mut _); |
| 170 | +} |
0 commit comments