| 1 |
use std::{borrow::Cow, sync::Arc}; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
Env, |
| 5 |
analysis::conversion_type::ConversionType, |
| 6 |
config, |
| 7 |
library::{self, Infallible, Mandatory}, |
| 8 |
}; |
| 9 |
|
| 10 |
#[derive(Default, Clone, Debug)] |
| 11 |
pub enum TryFromGlib { |
| 12 |
#[default] |
| 13 |
Default, |
| 14 |
NotImplemented, |
| 15 |
Option, |
| 16 |
OptionMandatory, |
| 17 |
Result { |
| 18 |
ok_type: Arc<str>, |
| 19 |
err_type: Arc<str>, |
| 20 |
}, |
| 21 |
ResultInfallible { |
| 22 |
ok_type: Arc<str>, |
| 23 |
}, |
| 24 |
} |
| 25 |
|
| 26 |
impl TryFromGlib { |
| 27 |
fn _new( |
| 28 |
env: &Env, |
| 29 |
type_id: library::TypeId, |
| 30 |
mut config_mandatory: impl Iterator<Item = Mandatory>, |
| 31 |
mut config_infallible: impl Iterator<Item = Infallible>, |
| 32 |
) -> Self { |
| 33 |
let conversion_type = ConversionType::of(env, type_id); |
| 34 |
match conversion_type { |
| 35 |
ConversionType::Option => { |
| 36 |
if *config_mandatory.next().unwrap_or(Mandatory(false)) { |
| 37 |
TryFromGlib::OptionMandatory |
| 38 |
} else { |
| 39 |
TryFromGlib::Option |
| 40 |
} |
| 41 |
} |
| 42 |
ConversionType::Result { ok_type, err_type } => { |
| 43 |
if *config_infallible.next().unwrap_or(Infallible(false)) { |
| 44 |
TryFromGlib::ResultInfallible { |
| 45 |
ok_type: Arc::clone(&ok_type), |
| 46 |
} |
| 47 |
} else { |
| 48 |
TryFromGlib::Result { |
| 49 |
ok_type: Arc::clone(&ok_type), |
| 50 |
err_type: Arc::clone(&err_type), |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
_ => TryFromGlib::NotImplemented, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pub fn from_type_defaults(env: &Env, type_id: library::TypeId) -> Self { |
| 59 |
Self::_new(env, type_id, None.into_iter(), None.into_iter()) |
| 60 |
} |
| 61 |
|
| 62 |
pub fn or_type_defaults(&self, env: &Env, type_id: library::TypeId) -> Cow<'_, Self> { |
| 63 |
match self { |
| 64 |
TryFromGlib::Default => Cow::Owned(Self::from_type_defaults(env, type_id)), |
| 65 |
other => Cow::Borrowed(other), |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
pub fn from_parameter( |
| 70 |
env: &Env, |
| 71 |
type_id: library::TypeId, |
| 72 |
configured_parameters: &[&config::functions::Parameter], |
| 73 |
) -> Self { |
| 74 |
Self::_new( |
| 75 |
env, |
| 76 |
type_id, |
| 77 |
configured_parameters.iter().filter_map(|par| par.mandatory), |
| 78 |
configured_parameters |
| 79 |
.iter() |
| 80 |
.filter_map(|par| par.infallible), |
| 81 |
) |
| 82 |
} |
| 83 |
|
| 84 |
pub fn from_return_value( |
| 85 |
env: &Env, |
| 86 |
type_id: library::TypeId, |
| 87 |
configured_functions: &[&config::functions::Function], |
| 88 |
) -> Self { |
| 89 |
Self::_new( |
| 90 |
env, |
| 91 |
type_id, |
| 92 |
configured_functions.iter().filter_map(|f| f.ret.mandatory), |
| 93 |
configured_functions.iter().filter_map(|f| f.ret.infallible), |
| 94 |
) |
| 95 |
} |
| 96 |
} |