use crate::{sys, LanguageType, SBFileSpec, SBLineEntry, SBStream, SBTypeList, TypeClass};
use std::fmt;
pub struct SBCompileUnit {
pub raw: sys::SBCompileUnitRef,
}
impl SBCompileUnit {
pub(crate) fn wrap(raw: sys::SBCompileUnitRef) -> SBCompileUnit {
SBCompileUnit { raw }
}
pub(crate) fn maybe_wrap(raw: sys::SBCompileUnitRef) -> Option<SBCompileUnit> {
if unsafe { sys::SBCompileUnitIsValid(raw) } {
Some(SBCompileUnit { raw })
} else {
None
}
}
pub fn is_valid(&self) -> bool {
unsafe { sys::SBCompileUnitIsValid(self.raw) }
}
pub fn filespec(&self) -> SBFileSpec {
SBFileSpec::wrap(unsafe { sys::SBCompileUnitGetFileSpec(self.raw) })
}
pub fn line_entries(&self) -> SBCompileUnitLineEntryIter {
SBCompileUnitLineEntryIter {
source: self,
idx: 0,
}
}
pub fn types(&self, type_mask: TypeClass) -> SBTypeList {
SBTypeList::wrap(unsafe { sys::SBCompileUnitGetTypes(self.raw, type_mask.bits()) })
}
pub fn language(&self) -> LanguageType {
unsafe { sys::SBCompileUnitGetLanguage(self.raw) }
}
}
impl Clone for SBCompileUnit {
fn clone(&self) -> SBCompileUnit {
SBCompileUnit {
raw: unsafe { sys::CloneSBCompileUnit(self.raw) },
}
}
}
impl fmt::Debug for SBCompileUnit {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let stream = SBStream::new();
unsafe { sys::SBCompileUnitGetDescription(self.raw, stream.raw) };
write!(fmt, "SBCompileUnit {{ {} }}", stream.data())
}
}
impl Drop for SBCompileUnit {
fn drop(&mut self) {
unsafe { sys::DisposeSBCompileUnit(self.raw) };
}
}
unsafe impl Send for SBCompileUnit {}
unsafe impl Sync for SBCompileUnit {}
pub struct SBCompileUnitLineEntryIter<'d> {
source: &'d SBCompileUnit,
idx: u32,
}
impl<'d> Iterator for SBCompileUnitLineEntryIter<'d> {
type Item = SBLineEntry;
fn next(&mut self) -> Option<SBLineEntry> {
if self.idx < unsafe { sys::SBCompileUnitGetNumLineEntries(self.source.raw) } {
let r = Some(SBLineEntry::wrap(unsafe {
sys::SBCompileUnitGetLineEntryAtIndex(self.source.raw, self.idx)
}));
self.idx += 1;
r
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let sz = unsafe { sys::SBCompileUnitGetNumLineEntries(self.source.raw) } as usize;
(sz - self.idx as usize, Some(sz))
}
}
impl<'d> ExactSizeIterator for SBCompileUnitLineEntryIter<'d> {}
#[cfg(feature = "graphql")]
#[graphql_object]
impl SBCompileUnit {
fn filespec() -> SBFileSpec {
self.filespec()
}
}