1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::{
    ffi::c_void,
    fmt::{self, Formatter},
};

use crate::{error::TableGenError, raw::TableGenStringRef, string_ref::StringRef};

pub(crate) unsafe extern "C" fn print_callback(string: TableGenStringRef, data: *mut c_void) {
    let (formatter, result) = &mut *(data as *mut (&mut Formatter, fmt::Result));

    if result.is_err() {
        return;
    }

    *result = (|| {
        write!(
            formatter,
            "{}",
            TryInto::<&str>::try_into(StringRef::from_raw(string)).map_err(|_| fmt::Error)?
        )
    })();
}

pub(crate) unsafe extern "C" fn print_string_callback(
    string: TableGenStringRef,
    data: *mut c_void,
) {
    let (writer, result) = &mut *(data as *mut (String, Result<(), TableGenError>));

    if result.is_err() {
        return;
    }

    *result = (|| {
        writer.push_str(StringRef::from_raw(string).as_str()?);

        Ok(())
    })();
}