#![deny(
warnings,
nonstandard_style,
unused,
future_incompatible,
rust_2018_idioms
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![doc(
html_logo_url = "https://senpy.club/favicon.png",
html_favicon_url = "https://senpy.club/favicon.png"
)]
use std::ffi::{CStr, CString};
use libc::c_char;
#[no_mangle]
pub static SENPY_CLUB_API_BASE_URL: &str = senpy::SENPY_CLUB_API_BASE_URL;
#[no_mangle]
pub static SENPY_CLUB_API_CURRENT_VERSION: u32 =
senpy::SENPY_CLUB_API_CURRENT_VERSION;
#[no_mangle]
pub static SENPY_CLUB_API_URL: &str = senpy::SENPY_CLUB_API_URL;
#[repr(C)]
#[derive(Default)]
pub struct Random {
language: String,
image: String,
}
impl Random {
#[must_use]
pub fn new() -> Self { Self::default() }
pub fn populate(&mut self) {
if let Ok(image) = senpy::random() {
self.language = image.language;
self.image = image.image;
} else {
self.language = "".to_string();
self.image = "".to_string();
}
}
#[must_use]
pub fn get(&self, key: &str) -> String {
match key {
"language" => self.language.clone(),
"image" => self.image.clone(),
_ => "".to_string(),
}
}
}
#[no_mangle]
pub unsafe extern "C" fn language(language: *const c_char) -> *mut *mut c_char {
match senpy::language(CStr::from_ptr(language).to_str().unwrap()) {
Ok(images) => {
let mut images_c =
vec![CString::new(images.len().to_string()).unwrap().into_raw()];
for image in images {
images_c.push(CString::new(image).unwrap().into_raw());
}
images_c.as_mut_ptr()
}
Err(_) => vec![CString::new("-1").unwrap().into_raw()].as_mut_ptr(),
}
}
#[no_mangle]
pub extern "C" fn languages() -> *mut *mut c_char {
match senpy::languages() {
Ok(languages) => {
let mut languages_c = vec![CString::new(languages.len().to_string())
.unwrap()
.into_raw()];
for language in languages {
languages_c.push(CString::new(language).unwrap().into_raw());
}
languages_c.as_mut_ptr()
}
Err(_) => vec![CString::new("-1").unwrap().into_raw()].as_mut_ptr(),
}
}
#[no_mangle]
pub extern "C" fn random_new() -> *mut Random {
Box::into_raw(Box::new(Random::new()))
}
#[no_mangle]
pub unsafe extern "C" fn random_populate(random: *mut Random) {
(&mut *random).populate();
}
#[no_mangle]
pub unsafe extern "C" fn random_get(
random: *const Random,
key: *const c_char,
) -> *mut c_char {
CString::new((&*random).get(CStr::from_ptr(key).to_str().unwrap()))
.unwrap()
.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn random_free(random: *mut Random) {
if random.is_null() {
return;
}
drop(Box::from_raw(random));
}
#[no_mangle]
pub extern "C" fn status() -> i32 {
match senpy::status() {
Ok(status) =>
if status {
1
} else {
0
},
Err(_) => -1,
}
}