1use crate::{sys, SBAddress, SBFileSpec, SBStream};
8use std::ffi::CStr;
9use std::fmt;
10
11pub struct SBBlock {
13 pub raw: sys::SBBlockRef,
15}
16
17impl SBBlock {
18 pub(crate) fn wrap(raw: sys::SBBlockRef) -> SBBlock {
20 SBBlock { raw }
21 }
22
23 pub(crate) fn maybe_wrap(raw: sys::SBBlockRef) -> Option<SBBlock> {
25 if unsafe { sys::SBBlockIsValid(raw) } {
26 Some(SBBlock { raw })
27 } else {
28 None
29 }
30 }
31
32 pub fn is_valid(&self) -> bool {
34 unsafe { sys::SBBlockIsValid(self.raw) }
35 }
36
37 pub fn is_inlined(&self) -> bool {
39 unsafe { sys::SBBlockIsInlined(self.raw) }
40 }
41
42 pub fn inlined_name(&self) -> &str {
44 unsafe {
45 match CStr::from_ptr(sys::SBBlockGetInlinedName(self.raw)).to_str() {
46 Ok(s) => s,
47 _ => panic!("Invalid string?"),
48 }
49 }
50 }
51
52 pub fn inlined_call_site_file(&self) -> Option<SBFileSpec> {
54 SBFileSpec::maybe_wrap(unsafe { sys::SBBlockGetInlinedCallSiteFile(self.raw) })
55 }
56
57 pub fn inlined_call_site_line(&self) -> Option<u32> {
59 let line = unsafe { sys::SBBlockGetInlinedCallSiteLine(self.raw) };
60 if line > 0 {
61 Some(line)
62 } else {
63 None
64 }
65 }
66
67 pub fn inlined_call_site_column(&self) -> Option<u32> {
69 let column = unsafe { sys::SBBlockGetInlinedCallSiteColumn(self.raw) };
70 if column > 0 {
71 Some(column)
72 } else {
73 None
74 }
75 }
76
77 pub fn parent(&self) -> Option<SBBlock> {
79 SBBlock::maybe_wrap(unsafe { sys::SBBlockGetParent(self.raw) })
80 }
81
82 pub fn containing_inlined_block(&self) -> Option<SBBlock> {
84 SBBlock::maybe_wrap(unsafe { sys::SBBlockGetContainingInlinedBlock(self.raw) })
85 }
86
87 pub fn sibling(&self) -> Option<SBBlock> {
89 SBBlock::maybe_wrap(unsafe { sys::SBBlockGetSibling(self.raw) })
90 }
91
92 pub fn first_child(&self) -> Option<SBBlock> {
94 SBBlock::maybe_wrap(unsafe { sys::SBBlockGetFirstChild(self.raw) })
95 }
96
97 pub fn num_ranges(&self) -> u32 {
99 unsafe { sys::SBBlockGetNumRanges(self.raw) }
100 }
101
102 pub fn range_start_address(&self, idx: u32) -> SBAddress {
104 SBAddress {
105 raw: unsafe { sys::SBBlockGetRangeStartAddress(self.raw, idx) },
106 }
107 }
108
109 pub fn range_end_address(&self, idx: u32) -> SBAddress {
111 SBAddress {
112 raw: unsafe { sys::SBBlockGetRangeEndAddress(self.raw, idx) },
113 }
114 }
115
116 pub fn range_index_for_block_address(&self, block_address: &SBAddress) -> u32 {
118 unsafe { sys::SBBlockGetRangeIndexForBlockAddress(self.raw, block_address.raw) }
119 }
120}
121
122impl Clone for SBBlock {
123 fn clone(&self) -> SBBlock {
124 SBBlock {
125 raw: unsafe { sys::CloneSBBlock(self.raw) },
126 }
127 }
128}
129
130impl fmt::Debug for SBBlock {
131 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
132 let stream = SBStream::new();
133 unsafe { sys::SBBlockGetDescription(self.raw, stream.raw) };
134 write!(fmt, "SBBlock {{ {} }}", stream.data())
135 }
136}
137
138impl Drop for SBBlock {
139 fn drop(&mut self) {
140 unsafe { sys::DisposeSBBlock(self.raw) };
141 }
142}
143
144unsafe impl Send for SBBlock {}
145unsafe impl Sync for SBBlock {}
146
147#[cfg(feature = "graphql")]
148#[juniper::graphql_object]
149impl SBBlock {
150 fn is_inlined() -> bool {
151 self.is_inlined()
152 }
153
154 fn inlined_name() -> &str {
155 self.inlined_name()
156 }
157
158 fn inlined_call_site_file() -> Option<SBFileSpec> {
159 self.inlined_call_site_file()
160 }
161
162 fn inlined_call_site_line() -> Option<i32> {
164 self.inlined_call_site_line().map(|i| i as i32)
165 }
166
167 fn inlined_call_site_column() -> Option<i32> {
169 self.inlined_call_site_column().map(|i| i as i32)
170 }
171}