googlescrape 0.3.1

a library to scrape google results
Documentation
#![cfg(feature = "ffi")]

use super::*;
use libc::{c_char, c_int, c_uchar, size_t};
use std::ffi::CStr;
use std::mem;


#[no_mangle]
#[repr(C)]
pub struct returned {
    val: *mut c_uchar,
    len: c_int,
    cap: size_t,
}

impl Drop for returned {
    fn drop(&mut self) {
        unsafe { Vec::<c_uchar>::from_raw_parts(self.val, self.len as usize, self.cap) };
    }
}

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn google(query: *const c_char) -> *mut returned {
    match unsafe {
        CStr::from_ptr(query)
            .to_str()
            .map_err(Box::from)
            .and_then(native::google)
    } {
        Ok(x) => {
            let mut v = to_vec_named(&x).unwrap();
            let ret = Box::into_raw(
                returned {
                    val: v.as_mut_ptr(),
                    len: v.len() as c_int,
                    cap: v.capacity(),
                }
                .into(),
            );
            mem::forget(v); // I thought this wouldnt happen at all but apparently as_mut_ptr DOESNT take ownershiP???
            ret
        }
        Err(x) => {
            let mut v = to_vec(&x.to_string()).unwrap();
            let ret = Box::into_raw(
                returned {
                    val: v.as_mut_ptr(),
                    len: v.len() as c_int,
                    cap: v.capacity(),
                }
                .into(),
            );
            mem::forget(v);
            ret
        }
    }
}

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn freeGResults(ret: *mut returned) {
    unsafe { Box::from_raw(ret) };
}