aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 4347966eddf413b6c1c45b67fca96d6d12ed2a84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// This file is part of senpy-ffi <https://github.com/senpy-club/senpy-ffi>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

//! FFI bindings for [`senpy-rs`](https://github.com/senpy-club/senpy-rs)

#![feature(trivial_bounds)]
#![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;

/// The base URL to The Senpy Club API
///
/// FFI binding to `senpy::SENPY_CLUB_API_BASE_URL`
#[no_mangle]
pub static SENPY_CLUB_API_BASE_URL: &str = senpy::SENPY_CLUB_API_BASE_URL;
/// The current API version of The Senpy Club API
///
/// FFI binding to `senpy::SENPY_CLUB_API_CURRENT_VERSION`
#[no_mangle]
pub static SENPY_CLUB_API_CURRENT_VERSION: u32 =
  senpy::SENPY_CLUB_API_CURRENT_VERSION;
/// The API URL to The Senpy Club API
///
/// FFI binding to `senpy::SENPY_CLUB_API_URL`
#[no_mangle]
pub static SENPY_CLUB_API_URL: &str = senpy::SENPY_CLUB_API_URL;

/// The response of the <https://api.senpy.club/v2/random> route
///
/// Part of the FFI binding to `senpy::random`
#[repr(C)]
#[derive(Default)]
pub struct Random {
  language: String,
  image:    String,
}
impl Random {
  /// Initializes a new `Random`
  ///
  /// Part of the FFI binding to `senpy::random`
  #[must_use]
  pub fn new() -> Self { Self::default() }

  /// Populates a `Random` from a `senpy::random` call
  ///
  /// Part of the FFI binding to `senpy::random`
  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();
    }
  }

  /// Frees a `Random`
  ///
  /// Part of the FFI binding to `senpy::random`
  #[must_use]
  pub fn get(&self, key: &str) -> String {
    match key {
      "language" => self.language.clone(),
      "image" => self.image.clone(),
      _ => "".to_string(),
    }
  }
}

/// Returns an array where the first element is the size of the array and the
/// remaining elements are the images.
///
/// If the first element (size) is `-1`; the
/// request failed for any reason.
///
/// # Safety
/// This is an *unsafe* FFI binding to `senpy::language`.
///
/// # Panics
/// if a `String` cannot be converted into a `CString`
#[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(),
  }
}

/// Returns an array where the first element is the size of the array and the
/// remaining elements are the languages.
///
/// If the first element (size) is `-1`; the request failed for
/// any reason.
///
/// FFI binding to `senpy::languages`
///
/// # Panics
/// if a `String` cannot be converted into a `CString`
#[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(),
  }
}

/// Initializes a new `Random`
///
/// Part of the FFI binding to `senpy::random`
#[no_mangle]
pub extern "C" fn random_new() -> *mut Random {
  Box::into_raw(Box::new(Random::new()))
}

/// Populates a `Random` from a `senpy::random` call
///
/// # Safety
/// This is part of an *unsafe* FFI binding to `senpy::random`.
#[no_mangle]
pub unsafe extern "C" fn random_populate(random: *mut Random) {
  (&mut *random).populate();
}

/// Gets a member from a `Random`, valid members are `language` and `image`.
///
/// # Safety
/// This is part of an *unsafe* FFI binding to `senpy::random`.
///
/// # Panics
/// if the `key` cannot be wrapped as a safe `CStr`
#[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()
}

/// Frees a `Random`
///
/// # Safety
/// This is part of an *unsafe* FFI binding to `senpy::random`.
#[no_mangle]
pub unsafe extern "C" fn random_free(random: *mut Random) {
  if random.is_null() {
    return;
  }

  Box::from_raw(random);
}

/// Returns `1` if up, returns `0` if down, and returns `-1` if the request
/// failed for any reason.
///
/// FFI binding to `senpy::status`
#[no_mangle]
pub extern "C" fn status() -> i32 {
  match senpy::status() {
    Ok(status) =>
      if status {
        1
      } else {
        0
      },
    Err(_) => -1,
  }
}