mastrust-ls-lib 0.1.0

mastrust language server library for rust
Documentation
//! # This is the Raw Client to the Bindings generated from my C code
//! This is the First Wrapper Class, all Functions define we with the 'RAW' suffix, and we use only C Types
//! 
//! First after this, in the 'optim' folder we found functions, to make the integration between Rust and C easier
//! 
//! For all Enduser, generelly you will never see this RawClasses in Your Master Project, but this is the First Step to integrate Mastrust in your Project
include!(concat!(env!("OUT_DIR"), "/mastrust_bindings.rs"));
use std::ffi::CString;
use std::ptr;

pub struct TokenRaw {
    pub typ: *mut ::std::os::raw::c_char,
    pub value: *mut u8,
    pub string: *mut ::std::os::raw::c_char,
    pub length: u8,
    token: CToken,
}

impl TokenRaw {

    pub fn new(_string: CString) -> Self {
        let cstring = _string.to_str().unwrap();
        // Erstelle eine CString aus dem gegebenen &str
        let c_char_string = std::ffi::CString::new(cstring).expect("Failed to create CString.");
        let c_char_ptr: *mut ::std::os::raw::c_char = c_char_string.into_raw();

        let _token = CToken {
            type_: c_char_ptr,
            value: ptr::null_mut(),
            string: c_char_ptr,
            length: ptr::null_mut(), // Annahme: length ist ein u8, nicht ein Zeiger
        };

        let mut token_raw = TokenRaw {
            typ: c_char_ptr,
            value: ptr::null_mut(),
            string: c_char_ptr,
            length: 0,
            token: _token,
        };

        unsafe {
            ctoken_init(&mut token_raw.token, c_char_ptr);
        }

        token_raw
    }

    pub fn tokenize(&mut self) {
        let _token = &mut self.token;
        unsafe {
            ctoken_tokenize(_token);
            self.value = (*_token).value;
            self.length = (*_token).length as u8; // Konvertierung zu u8
        }
    }

    pub fn get_token(&self) -> Vec<u8> {
        unsafe {
            if self.value.is_null() {
                return Vec::new();
            }
            Vec::from_raw_parts(self.value, self.length as usize, self.length as usize)
        }
    }
}