use std::os::raw::c_int;
use crate::ffi;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WarningCode {
CommentDropped,
CommentStyleDegraded,
ValueDropped,
TypeDegraded,
Unknown(c_int),
}
impl WarningCode {
fn from_c(c: c_int) -> Self {
match c {
0 => WarningCode::CommentDropped,
1 => WarningCode::CommentStyleDegraded,
2 => WarningCode::ValueDropped,
3 => WarningCode::TypeDegraded,
other => WarningCode::Unknown(other),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WarningCause {
FormatLimitation,
ExplicitOption,
Unknown(c_int),
}
impl WarningCause {
fn from_c(c: c_int) -> Self {
match c {
0 => WarningCause::FormatLimitation,
1 => WarningCause::ExplicitOption,
other => WarningCause::Unknown(other),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Warning {
pub code: WarningCode,
pub cause: WarningCause,
pub path: String,
pub note: String,
}
impl Warning {
pub(crate) unsafe fn from_ffi(w: &ffi::FigWarning) -> Self {
Warning {
code: WarningCode::from_c(w.code),
cause: WarningCause::from_c(w.cause),
path: unsafe { borrowed_string(w.path, w.path_len) },
note: unsafe { borrowed_string(w.note, w.note_len) },
}
}
}
unsafe fn borrowed_string(ptr: *const u8, len: usize) -> String {
if ptr.is_null() || len == 0 {
return String::new();
}
let bytes = unsafe { std::slice::from_raw_parts(ptr, len) };
String::from_utf8_lossy(bytes).into_owned()
}