sails_idl_parser/ffi/ast/
mod.rs

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::ast;
use std::{
    ffi::{c_char, CString},
    slice, str,
};

pub mod visitor;

#[repr(C)]
pub struct ParseResult {
    program: *mut Program,
    error: Error,
}

#[repr(C)]
pub struct Error {
    code: ErrorCode,
    details: *const c_char,
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ErrorCode {
    Ok,
    InvalidIDL,
    ParseError,
    NullPtr,
}

/// # Safety
///
/// See the safety documentation of [`slice::from_raw_parts`].
#[no_mangle]
pub unsafe extern "C" fn parse_idl(idl_ptr: *const u8, idl_len: u32) -> *mut ParseResult {
    let idl_slice = unsafe { slice::from_raw_parts(idl_ptr, idl_len as usize) };
    let idl_str = match str::from_utf8(idl_slice) {
        Ok(s) => s,
        Err(e) => return create_parse_error(ErrorCode::ParseError, e, "validate IDL string"),
    };

    let program = match ast::parse_idl(idl_str) {
        Ok(p) => p,
        Err(e) => return create_parse_error(ErrorCode::ParseError, e, "parse IDL"),
    };

    let result = ParseResult {
        error: Error {
            code: ErrorCode::Ok,
            details: std::ptr::null(),
        },
        program: Box::into_raw(Box::new(program)),
    };

    Box::into_raw(Box::new(result))
}

fn create_parse_error(
    code: ErrorCode,
    e: impl std::error::Error,
    context: &'static str,
) -> *mut ParseResult {
    let details = CString::new(format!("{}: {}", context, e)).unwrap();
    let result = ParseResult {
        error: Error {
            code,
            details: details.into_raw(),
        },
        program: std::ptr::null_mut(),
    };
    Box::into_raw(Box::new(result))
}

/// # Safety
///
/// Pointer must be obtained from [`parse_idl`].
#[no_mangle]
pub unsafe extern "C" fn free_parse_result(result: *mut ParseResult) {
    if result.is_null() {
        return;
    }
    unsafe {
        let result = Box::from_raw(result);
        if result.error.code != ErrorCode::Ok {
            let details = CString::from_raw(result.error.details as _);
            drop(details);
        }
    }
}

pub type Program = ast::Program;

#[repr(C)]
pub struct Ctor {
    raw_ptr: Ptr,
}

#[repr(C)]
pub struct CtorFunc {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
    docs_ptr: *const u8,
    docs_len: u32,
}

#[repr(C)]
pub struct Service {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
}

#[repr(C)]
pub struct ServiceFunc {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
    is_query: bool,
    docs_ptr: *const u8,
    docs_len: u32,
}

pub type ServiceEvent = EnumVariant;

#[repr(C)]
pub struct FuncParam {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
}

#[repr(C)]
pub struct Type {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
    docs_ptr: *const u8,
    docs_len: u32,
}

#[repr(C)]
pub struct TypeDecl {
    raw_ptr: Ptr,
}

pub type PrimitiveType = ast::PrimitiveType;

#[repr(C)]
pub struct StructDef {
    raw_ptr: Ptr,
}

#[repr(C)]
pub struct StructField {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
    docs_ptr: *const u8,
    docs_len: u32,
}

#[repr(C)]
pub struct EnumDef {
    raw_ptr: Ptr,
}

#[repr(C)]
pub struct EnumVariant {
    raw_ptr: Ptr,
    name_ptr: *const u8,
    name_len: u32,
    docs_ptr: *const u8,
    docs_len: u32,
}

#[repr(transparent)]
pub struct Ptr(*const ());

impl<T> From<&T> for Ptr {
    fn from(t: &T) -> Self {
        Self(t as *const T as *const ())
    }
}

impl<T> AsRef<T> for Ptr {
    fn as_ref(&self) -> &T {
        unsafe { (self.0 as *const T).as_ref() }.unwrap()
    }
}