libgir/analysis/
override_string_type.rs

1use log::error;
2
3use crate::{config, env::Env, library::*};
4
5pub fn override_string_type_parameter(
6    env: &Env,
7    typ: TypeId,
8    configured_parameters: &[&config::functions::Parameter],
9) -> TypeId {
10    let string_type = configured_parameters.iter().find_map(|p| p.string_type);
11    apply(env, typ, string_type)
12}
13
14pub fn override_string_type_return(
15    env: &Env,
16    typ: TypeId,
17    configured_functions: &[&config::functions::Function],
18) -> TypeId {
19    let string_type = configured_functions.iter().find_map(|f| f.ret.string_type);
20    apply(env, typ, string_type)
21}
22
23fn apply(env: &Env, type_id: TypeId, string_type: Option<config::StringType>) -> TypeId {
24    let string_type = if let Some(string_type) = string_type {
25        string_type
26    } else {
27        return type_id;
28    };
29
30    let replace = {
31        use crate::config::StringType::*;
32        match string_type {
33            Utf8 => TypeId::tid_utf8(),
34            Filename => TypeId::tid_filename(),
35            OsString => TypeId::tid_os_string(),
36        }
37    };
38    match *env.library.type_(type_id) {
39        Type::Basic(Basic::Filename | Basic::OsString | Basic::Utf8) => replace,
40        Type::CArray(inner_tid) if can_overridden_basic(env, inner_tid) => {
41            Type::find_c_array(&env.library, replace, None)
42        }
43        _ => {
44            error!(
45                "Bad type {0} for string_type override",
46                type_id.full_name(&env.library)
47            );
48            type_id
49        }
50    }
51}
52
53fn can_overridden_basic(env: &Env, type_id: TypeId) -> bool {
54    matches!(
55        *env.library.type_(type_id),
56        Type::Basic(Basic::Filename | Basic::OsString | Basic::Utf8)
57    )
58}