libgir/codegen/
translate_to_glib.rs

1use crate::{
2    analysis::{function_parameters::TransformationType, ref_mode::RefMode},
3    library::Transfer,
4};
5
6pub trait TranslateToGlib {
7    fn translate_to_glib(&self) -> String;
8}
9
10impl TranslateToGlib for TransformationType {
11    fn translate_to_glib(&self) -> String {
12        use self::TransformationType::*;
13        match *self {
14            ToGlibDirect { ref name } => name.clone(),
15            ToGlibScalar {
16                ref name,
17                needs_into,
18                ..
19            } => {
20                let pre_into = if needs_into { ".into()" } else { "" };
21                format!("{}{}{}", name, pre_into, ".into_glib()")
22            }
23            ToGlibPointer {
24                ref name,
25                instance_parameter,
26                transfer,
27                ref_mode,
28                ref to_glib_extra,
29                ref pointer_cast,
30                ref explicit_target_type,
31                in_trait,
32                move_,
33                ..
34            } => {
35                let (left, right) = to_glib_xxx(transfer, ref_mode, explicit_target_type, move_);
36
37                if instance_parameter {
38                    format!(
39                        "{}self{}{}{}",
40                        left,
41                        if in_trait { to_glib_extra } else { "" },
42                        right,
43                        pointer_cast
44                    )
45                } else {
46                    format!("{left}{name}{to_glib_extra}{right}{pointer_cast}")
47                }
48            }
49            ToGlibBorrow => "/*Not applicable conversion Borrow*/".to_owned(),
50            ToGlibUnknown { ref name } => format!("/*Unknown conversion*/{name}"),
51            ToSome(ref name) => format!("Some({name})"),
52            IntoRaw(ref name) => format!("Box_::into_raw({name}) as *mut _"),
53            _ => unreachable!("Unexpected transformation type {:?}", self),
54        }
55    }
56}
57
58fn to_glib_xxx(
59    transfer: Transfer,
60    ref_mode: RefMode,
61    explicit_target_type: &str,
62    move_: bool,
63) -> (String, &'static str) {
64    use self::Transfer::*;
65    match transfer {
66        None => {
67            match ref_mode {
68                RefMode::None => (String::new(), ".to_glib_none_mut().0"), // unreachable!(),
69                RefMode::ByRef => match (move_, explicit_target_type.is_empty()) {
70                    (true, true) => (String::new(), ".into_glib_ptr()"),
71                    (true, false) => (
72                        format!("ToGlibPtr::<{explicit_target_type}>::into_glib_ptr("),
73                        ")",
74                    ),
75                    (false, true) => (String::new(), ".to_glib_none().0"),
76                    (false, false) => (
77                        format!("ToGlibPtr::<{explicit_target_type}>::to_glib_none("),
78                        ").0",
79                    ),
80                },
81                RefMode::ByRefMut => (String::new(), ".to_glib_none_mut().0"),
82                RefMode::ByRefImmut => ("mut_override(".into(), ".to_glib_none().0)"),
83                RefMode::ByRefConst => ("const_override(".into(), ".to_glib_none().0)"),
84                RefMode::ByRefFake => (String::new(), ""), // unreachable!(),
85            }
86        }
87        Full => {
88            if move_ {
89                ("".into(), ".into_glib_ptr()")
90            } else {
91                ("".into(), ".to_glib_full()")
92            }
93        }
94        Container => ("".into(), ".to_glib_container().0"),
95    }
96}