use cassandra::data_type::ConstDataType;
use cassandra::iterator::FieldIterator;
use cassandra::util::Protected;
use cassandra::value::Value;
use cassandra::error::*;
use cassandra_sys::CASS_OK;
use cassandra_sys::CassFunctionMeta as _CassFunctionMeta;
use cassandra_sys::cass_function_meta_argument;
use cassandra_sys::cass_function_meta_argument_count;
use cassandra_sys::cass_function_meta_argument_type_by_name;
use cassandra_sys::cass_function_meta_body;
use cassandra_sys::cass_function_meta_called_on_null_input;
use cassandra_sys::cass_function_meta_field_by_name;
use cassandra_sys::cass_function_meta_full_name;
use cassandra_sys::cass_function_meta_language;
use cassandra_sys::cass_function_meta_name;
use cassandra_sys::cass_function_meta_return_type;
use cassandra_sys::cass_iterator_fields_from_function_meta;
use cassandra_sys::cass_true;
use std::{mem, slice, str};
use std::ffi::CString;
#[derive(Debug)]
pub struct FunctionMeta(*const _CassFunctionMeta);
impl Protected<*const _CassFunctionMeta> for FunctionMeta {
fn inner(&self) -> *const _CassFunctionMeta { self.0 }
fn build(inner: *const _CassFunctionMeta) -> Self { if inner.is_null() { panic!("Unexpected null pointer") }; FunctionMeta(inner) }
}
impl FunctionMeta {
pub fn fields_iter(&self) -> FieldIterator {
unsafe { FieldIterator::build(cass_iterator_fields_from_function_meta(self.0)) }
}
#[allow(cast_possible_truncation)]
pub fn get_name(&self) -> String {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_function_meta_name(self.0, &mut name, &mut name_length);
str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
.expect("must be utf8")
.to_owned()
}
}
#[allow(cast_possible_truncation)]
pub fn full_name(&self) -> String {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_function_meta_full_name(self.0, &mut name, &mut name_length);
str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
.expect("must be utf8")
.to_owned()
}
}
#[allow(cast_possible_truncation)]
pub fn body(&self) -> String {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_function_meta_body(self.0, &mut name, &mut name_length);
str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
.expect("must be utf8")
.to_owned()
}
}
#[allow(cast_possible_truncation)]
pub fn language(&self) -> String {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_function_meta_language(self.0, &mut name, &mut name_length);
str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
.expect("must be utf8")
.to_owned()
}
}
pub fn called_on_null_input(&self) -> bool {
unsafe { cass_function_meta_called_on_null_input(self.0) == cass_true }
}
pub fn argument_count(&self) -> usize { unsafe { cass_function_meta_argument_count(self.0) } }
pub fn argument(&self, index: usize) -> Result<()> {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
let mut data_type = mem::zeroed();
cass_function_meta_argument(self.0, index, &mut name, &mut name_length, &mut data_type).to_result(())
}
}
pub fn argument_type_by_name(&self, name: &str) -> ConstDataType {
unsafe {
let name_cstr = CString::new(name).expect("must be utf8");
ConstDataType::build(cass_function_meta_argument_type_by_name(self.0, name_cstr.as_ptr()))
}
}
pub fn return_type(&self) -> ConstDataType { unsafe { ConstDataType::build(cass_function_meta_return_type(self.0)) } }
pub fn field_by_name(&self, name: &str) -> Value {
unsafe {
let name_cstr = CString::new(name).expect("must be utf8");
Value::build(cass_function_meta_field_by_name(self.0, name_cstr.as_ptr()))
}
}
}