use crate::error::Result;
use crate::metadata::{MetaData, MetaDataRef};
use mdflib_sys as ffi;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::ops::Deref;
use std::os::raw::c_char;
#[derive(Debug, Clone, Copy)]
pub struct SourceInformationRef<'a> {
pub(crate) inner: *const ffi::ISourceInformation,
_marker: PhantomData<&'a ()>,
}
impl std::fmt::Display for SourceInformationRef<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"SourceInformation {{ index: {}, name: {}, description: {}, path: {}, type: {}, bus: {}, flags: {} }}",
self.get_index(),
self.get_name(),
self.get_description(),
self.get_path(),
self.get_type(),
self.get_bus(),
self.get_flags()
)
}
}
impl<'a> SourceInformationRef<'a> {
#[allow(dead_code)]
pub(crate) fn new(inner: *const ffi::ISourceInformation) -> Self {
Self {
inner,
_marker: PhantomData,
}
}
pub fn get_index(&self) -> u64 {
unsafe { ffi::SourceInformationGetIndex(self.inner) }
}
pub fn get_name(&self) -> String {
unsafe {
let mut len = ffi::SourceInformationGetName(self.inner, std::ptr::null_mut(), 0);
if len == 0 {
return String::new();
}
len += 1; let mut buf = vec![0 as c_char; len as usize];
ffi::SourceInformationGetName(self.inner, buf.as_mut_ptr(), len);
CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
}
}
pub fn get_description(&self) -> String {
unsafe {
let mut len = ffi::SourceInformationGetDescription(self.inner, std::ptr::null_mut(), 0);
if len == 0 {
return String::new();
}
len += 1; let mut buf = vec![0 as c_char; len as usize];
ffi::SourceInformationGetDescription(self.inner, buf.as_mut_ptr(), len);
CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
}
}
pub fn get_path(&self) -> String {
unsafe {
let mut len = ffi::SourceInformationGetPath(self.inner, std::ptr::null_mut(), 0);
if len == 0 {
return String::new();
}
len += 1; let mut buf = vec![0 as c_char; len as usize];
ffi::SourceInformationGetPath(self.inner, buf.as_mut_ptr(), len);
CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
}
}
pub fn get_type(&self) -> u8 {
unsafe { ffi::SourceInformationGetType(self.inner) }
}
pub fn get_bus(&self) -> u8 {
unsafe { ffi::SourceInformationGetBus(self.inner) }
}
pub fn get_flags(&self) -> u8 {
unsafe { ffi::SourceInformationGetFlags(self.inner) }
}
pub fn get_metadata(&self) -> Option<MetaDataRef<'a>> {
unsafe {
let metadata = ffi::SourceInformationGetMetaData(self.inner);
if metadata.is_null() {
None
} else {
Some(MetaDataRef::new(metadata))
}
}
}
}
#[derive(Debug)]
pub struct SourceInformation<'a> {
pub(crate) inner: *mut ffi::ISourceInformation,
inner_ref: SourceInformationRef<'a>,
}
impl<'a> SourceInformation<'a> {
#[allow(dead_code)]
pub(crate) fn new(inner: *mut ffi::ISourceInformation) -> Self {
Self {
inner,
inner_ref: SourceInformationRef::new(inner),
}
}
pub fn set_name(&mut self, name: &str) -> Result<()> {
let c_name = CString::new(name)?;
unsafe {
ffi::SourceInformationSetName(self.inner, c_name.as_ptr());
}
Ok(())
}
pub fn set_description(&mut self, description: &str) -> Result<()> {
let c_description = CString::new(description)?;
unsafe {
ffi::SourceInformationSetDescription(self.inner, c_description.as_ptr());
}
Ok(())
}
pub fn set_path(&mut self, path: &str) -> Result<()> {
let c_path = CString::new(path)?;
unsafe {
ffi::SourceInformationSetPath(self.inner, c_path.as_ptr());
}
Ok(())
}
pub fn set_type(&mut self, source_type: u8) {
unsafe {
ffi::SourceInformationSetType(self.inner, source_type);
}
}
pub fn set_bus(&mut self, bus: u8) {
unsafe {
ffi::SourceInformationSetBus(self.inner, bus);
}
}
pub fn set_flags(&mut self, flags: u8) {
unsafe {
ffi::SourceInformationSetFlags(self.inner, flags);
}
}
pub fn create_metadata(&mut self) -> Option<MetaData<'a>> {
unsafe {
let metadata = ffi::SourceInformationCreateMetaData(self.inner);
if metadata.is_null() {
None
} else {
Some(MetaData::new(metadata))
}
}
}
}
impl<'a> Deref for SourceInformation<'a> {
type Target = SourceInformationRef<'a>;
fn deref(&self) -> &Self::Target {
&self.inner_ref
}
}