use crate::errors::{BlpError, Result};
pub struct Identity {
ptr: *mut crate::ffi::blpapi_Identity_t,
}
impl Identity {
pub(crate) fn from_raw(ptr: *mut crate::ffi::blpapi_Identity_t) -> Result<Self> {
if ptr.is_null() {
return Err(BlpError::Internal {
detail: "null identity pointer".into(),
});
}
Ok(Self { ptr })
}
pub(crate) fn as_ptr(&self) -> *mut crate::ffi::blpapi_Identity_t {
self.ptr
}
pub fn is_authorized(&self, service: &crate::Service<'_>) -> bool {
let rc = unsafe { crate::ffi::blpapi_Identity_isAuthorized(self.ptr, service.as_ptr()) };
rc != 0
}
pub fn has_entitlements(&self, service: &crate::Service<'_>, eids: &[i32]) -> Result<bool> {
let mut failed_count: i32 = 0;
let rc = unsafe {
crate::ffi::blpapi_Identity_hasEntitlements(
self.ptr,
service.as_ptr(),
std::ptr::null(),
eids.as_ptr(),
eids.len(),
std::ptr::null_mut(),
&mut failed_count,
)
};
Ok(rc != 0)
}
pub fn check_entitlements(
&self,
service: &crate::Service<'_>,
eids: &[i32],
) -> Result<EntitlementCheck> {
let mut failed = vec![0_i32; eids.len()];
let mut failed_count: i32 = eids.len() as i32;
let rc = unsafe {
crate::ffi::blpapi_Identity_hasEntitlements(
self.ptr,
service.as_ptr(),
std::ptr::null(),
eids.as_ptr(),
eids.len(),
failed.as_mut_ptr(),
&mut failed_count,
)
};
let entitled = rc != 0;
let failed_eids = if entitled {
Vec::new()
} else {
let count = usize::try_from(failed_count).unwrap_or(0).min(failed.len());
failed.truncate(count);
failed
};
Ok(EntitlementCheck {
entitled,
failed_eids,
})
}
pub fn seat_type(&self) -> Result<SeatType> {
let mut raw: i32 = -1;
let rc = unsafe { crate::ffi::blpapi_Identity_getSeatType(self.ptr, &mut raw) };
if rc != 0 {
return Err(BlpError::Internal {
detail: format!("getSeatType failed: rc={rc}"),
});
}
Ok(SeatType::from_raw(raw))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SeatType {
Bps,
NonBps,
Invalid,
}
impl SeatType {
fn from_raw(raw: i32) -> Self {
match raw {
0 => Self::Bps,
1 => Self::NonBps,
_ => Self::Invalid,
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Bps => "BPS",
Self::NonBps => "NONBPS",
Self::Invalid => "INVALID",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct EntitlementCheck {
pub entitled: bool,
pub failed_eids: Vec<i32>,
}
impl Drop for Identity {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { crate::ffi::blpapi_Identity_release(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}
}