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
use std::str::FromStr;

use crate::{analysis::function_parameters::Parameters, env::Env, library};

#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
pub enum SafetyAssertionMode {
    #[default]
    None,
    Skip,
    NotInitialized,
    InMainThread,
}

impl std::fmt::Display for SafetyAssertionMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SafetyAssertionMode::None => f.write_str(""),
            SafetyAssertionMode::NotInitialized => f.write_str("assert_not_initialized!();"),
            SafetyAssertionMode::Skip => f.write_str("skip_assert_initialized!();"),
            SafetyAssertionMode::InMainThread => f.write_str("assert_initialized_main_thread!();"),
        }
    }
}

impl FromStr for SafetyAssertionMode {
    type Err = String;
    fn from_str(name: &str) -> Result<SafetyAssertionMode, String> {
        match name {
            "none" => Ok(Self::None),
            "skip" => Ok(Self::Skip),
            "not-initialized" => Ok(Self::NotInitialized),
            "in-main-thread" => Ok(Self::InMainThread),
            _ => Err(format!("Unknown safety assertion mode '{name}'")),
        }
    }
}

impl SafetyAssertionMode {
    pub fn of(env: &Env, is_method: bool, params: &Parameters) -> Self {
        use crate::library::Type::*;
        if !env.config.generate_safety_asserts {
            return Self::None;
        }
        if is_method {
            return Self::None;
        }
        for par in &params.rust_parameters {
            let c_par = &params.c_parameters[par.ind_c];
            match env.library.type_(c_par.typ) {
                Class(..) | Interface(..)
                    if !*c_par.nullable && c_par.typ.ns_id == library::MAIN_NAMESPACE =>
                {
                    return Self::Skip
                }
                _ => (),
            }
        }

        Self::InMainThread
    }

    pub fn is_none(self) -> bool {
        matches!(self, Self::None)
    }
}