tcpp/
ffi.rs

1use std::convert::TryFrom;
2use std::ffi::{CStr, CString, NulError};
3use std::ptr::null_mut;
4
5extern "C" {
6
7    fn error_type_to_string(error: libc::c_uint) -> *const libc::c_char;
8
9    fn create_input_stream(str: *const libc::c_char) -> *mut libc::c_void;
10
11}
12
13/// Struct representing an input stream of bytes or string
14#[repr(C)]
15pub struct IInputStream {
16    pub(crate) handler : *mut libc::c_void
17}
18
19impl Default for IInputStream{
20    /// synonym to [`IInputStream::null`]
21    fn default() -> Self {
22        Self::null()
23    }
24}
25
26impl IInputStream {
27    /// Creates a null stream, stands for no input at all
28    pub fn null() -> Self {
29        IInputStream {
30            handler: null_mut()
31        }
32    }
33}
34
35impl TryFrom<String> for IInputStream {
36
37    type Error = NulError;
38    /// Construct an input stream from a single string
39    fn try_from(value: String) -> Result<Self, Self::Error> {
40        let cstring = CString::new(value)?;
41        let stream = IInputStream {
42            handler: unsafe {
43                create_input_stream(cstring.as_ptr())
44            }
45        };
46        Ok(stream)
47    }
48
49}
50
51/// Struct representing a common error within preprocessor
52#[repr(C)]
53pub struct TErrorInfo {
54    m_type: libc::c_uint,
55    m_line: libc::size_t
56}
57
58impl TErrorInfo {
59    /// Get the line number of this error
60    pub fn get_line(&self) -> usize {
61        self.m_line
62    }
63
64    /// Get the lint message of this error, returns None if an encoding error occurs
65    pub fn get_message(&self) -> Option<String> {
66        println!("{}", self.m_type);
67        Some(unsafe {
68            let msg = error_type_to_string(self.m_type);
69            CStr::from_ptr(msg).to_str().ok()?.to_owned()
70        })
71    }
72
73}