ferment-sys 0.2.14

Syntax tree morphing of FFI-compatible stuff
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::collections::HashSet;
use std::fmt::Formatter;
use indexmap::IndexMap;
use proc_macro2::Ident;
use syn::{parse_quote, Attribute, Item, ItemTrait, Path, PathSegment, Type};
use crate::Config;
use crate::composable::{TraitModelPart1, TypeModel, TypeModeled};
use crate::composer::CommaPunctuatedNestedArguments;
use crate::context::{CustomResolver, GenericResolver, ImportResolver, ScopeChain, ScopeResolver, ScopeSearchKey, TraitsResolver, TypeChain};
use crate::kind::{DictFermentableModelKind, DictTypeModelKind, GroupModelKind, MixinKind, ObjectKind, ScopeItemKind, SmartPointerModelKind, TypeModelKind};
use crate::ext::{AsType, GenericBoundKey, RefineInScope, Split, ToPath, ToType};
use crate::formatter::format_global_context;

#[derive(Clone)]
pub struct GlobalContext {
    pub config: Config,
    pub scope_register: ScopeResolver,
    pub generics: GenericResolver,
    pub traits: TraitsResolver,
    pub custom: CustomResolver,
    pub imports: ImportResolver,
    pub refined_mixins: IndexMap<MixinKind, HashSet<Option<Attribute>>>
}

impl std::fmt::Debug for GlobalContext {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format_global_context(self))
    }
}

impl std::fmt::Display for GlobalContext {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, f)
    }
}

impl From<&Config> for GlobalContext {
    fn from(config: &Config) -> Self {
        GlobalContext::with_config(config.clone())
    }
}
impl GlobalContext {
    pub fn with_config(config: Config) -> Self {
        Self { config, scope_register: ScopeResolver::default(), generics: Default::default(), traits: Default::default(), custom: Default::default(), imports: Default::default(), refined_mixins: IndexMap::default(), }
    }
    pub fn fermented_mod_name(&self) -> &str {
        &self.config.mod_name
    }
    pub fn is_fermented_mod(&self, ident: &Ident) -> bool {
        ident.eq(self.fermented_mod_name())
    }


    pub fn resolve_trait_type<'a>(&'a self, from_type: &'a Type) -> Option<&'a ObjectKind> {
        // println!("resolve_trait_type: {} ({:?})", from_type.to_token_stream(), from_type);
        // RESOLVE PATHS
        // Self::asyn::query::TransportRequest::Client::Error
        // ? [Self::asyn::query::TransportRequest::Client::Error] Self
        // : [Self::asyn::query::TransportRequest::Client] Self::Error
        // : [Self::asyn::query::TransportRequest] Self::Client::Error
        //  : [Self::asyn::query::TransportRequest] Self::Client -> [Self::asyn::query::TransportClient] Self::Error

        // aa::bb::cc::dd::ee
        // 1. a) [aa::bb::cc::dd::ee] Self
        // 2. a) [aa::bb::cc::dd] Self::ee
        // 3. a) [aa::bb::cc::dd] Self, [Self::ee]
        // 4. a) [aa::bb::cc] Self::dd::ee, b) [aa::bb::cc] Self::dd
        let current_scope: Path = parse_quote!(#from_type);
        // println!("current_scope: {}", current_scope);
        let mut i = 0;
        let mut maybe_trait: Option<&ObjectKind>  = None;
        while i < current_scope.segments.len() && maybe_trait.is_none() {
            let (root, mut head) = current_scope.split(i);
            head = if head.segments.is_empty() {
                parse_quote!(Self)
            } else {
                parse_quote!(Self::#head)
            };

            let ty = head.to_type();
            let root_scope = self.maybe_scope_ref(&root);
            if let Some(scope) = root_scope {
                //maybe_trait = self.maybe_local_scope_object_ref_by_key(&ty, scope);
                maybe_trait = ScopeSearchKey::maybe_from(ty)
                    .and_then(|key| self.scope_register.maybe_object_ref_by_key_in_scope(key, scope));
            }
            //maybe_trait = self.maybe_scope_type(&ty, &root);
            if i > 0 {
                match maybe_trait {
                    Some(ObjectKind::Item(TypeModelKind::Trait(model), _)) |
                    Some(ObjectKind::Type(TypeModelKind::Trait(model))) => {
                        let ident = &head.segments.last()?.ident;
                        // println!("FFI (has decomposition) for: {}: {}", format_token_stream(ident), trait_ty);
                        if let Some(trait_type) = model.decomposition.types.get(ident) {
                            // println!("FFI (first bound) {:?}", trait_type);
                            if let Some(first_bound) = trait_type.trait_bounds.first() {
                                // println!("FFI (first bound) {}", format_token_stream(&first_bound.path));
                                let tt_type = first_bound.to_type();
                                if let Some(scope) = root_scope {
                                    maybe_trait = ScopeSearchKey::maybe_from(tt_type)
                                        .and_then(|key| self.scope_register.maybe_object_ref_by_key_in_scope(key, scope));
                                }
                                // println!("FFI (first bound full) {:?}", maybe_trait);
                            }
                        }
                    },
                    _ => {}
                }
            }
            // println!("FFI (resolve....) for: {} in [{}] ===> {:?}", format_token_stream(&head), format_token_stream(&root), maybe_trait);
            i += 1;
        }
        maybe_trait
    }

    pub fn item_trait_with_ident_for(&self, ident: &Ident, scope: &ScopeChain) -> Option<&TraitModelPart1> {
        self.traits
            .item_trait_with_ident_for(ident, scope)
    }

    pub fn maybe_trait_scope_pair(&self, link: &Type, scope: &ScopeChain) -> Option<(TraitModelPart1, ScopeChain)> {
        let parent_scope = scope.parent_scope()?;
        let trait_ty = link.to_type();
        let trait_scope = self.actual_scope_for_type(&trait_ty, parent_scope)?;
        let trait_path = link.to_path();
        let ident = trait_path.get_ident()?;
        self.item_trait_with_ident_for(ident, trait_scope)
            .map(|trait_model| {
                let mut model = trait_model.clone();
                // TODO: move to full and replace nested_arguments
                let value = TypeModelKind::Object(TypeModel::new_generic_scope_non_nested(scope, &trait_model.item.generics));
                model.implementors.push(value);
                (model, trait_scope.clone())
            })

    }

    fn maybe_obj_or_parent_object_ref_by_tree_key<'a>(&'a self, self_scope: &'a ScopeChain, parent_chain: &'a ScopeChain, ty: &'a Type) -> Option<&'a ObjectKind> {
        self.maybe_local_scope_object_ref_by_key(ty, self_scope)
            .or_else(move || match parent_chain {
                ScopeChain::Mod { .. } | ScopeChain::CrateRoot { .. } =>
                    self.maybe_local_scope_object_ref_by_key(ty, parent_chain),
                _ => None,
            })
    }
    fn maybe_obj_or_parent_object_ref_by_tree_search_key<'a>(&'a self, self_scope: &'a ScopeChain, parent_chain: &'a ScopeChain, search_key: ScopeSearchKey) -> Option<&'a ObjectKind> {
        self.maybe_object_ref_by_search_key_in_scope(search_key.clone(), self_scope)
            .or_else(move || match parent_chain {
                ScopeChain::Mod { .. } | ScopeChain::CrateRoot { .. } =>
                    self.maybe_object_ref_by_search_key_in_scope(search_key, parent_chain),
                _ => None,
            })
    }

    fn maybe_fn_object_ref_by_tree_key<'a>(&'a self, fn_scope: &'a ScopeChain, parent_scope: &'a ScopeChain, ty: &'a Type) -> Option<&'a ObjectKind> {
        self.maybe_local_scope_object_ref_by_key(ty, fn_scope)
            .or_else(move || match parent_scope {
                ScopeChain::CrateRoot { .. } | ScopeChain::Mod { .. } =>
                    self.maybe_local_scope_object_ref_by_key(ty, parent_scope),
                ScopeChain::Fn { parent, .. } =>
                    self.maybe_fn_object_ref_by_tree_key(parent_scope, parent, ty),
                ScopeChain::Trait { parent, .. } |
                ScopeChain::Object { parent, .. } |
                ScopeChain::Impl { parent, .. } =>
                    self.maybe_local_scope_object_ref_by_key(ty, parent_scope)
                        .or_else(|| match &**parent {
                            ScopeChain::CrateRoot { .. } |
                            ScopeChain::Mod { ..} =>
                                self.maybe_local_scope_object_ref_by_key(ty, parent),
                            _ => None,
                        }),
        })
    }
    fn maybe_fn_object_ref_by_tree_search_key<'a>(&'a self, fn_scope: &'a ScopeChain, parent_scope: &'a ScopeChain, search_key: ScopeSearchKey) -> Option<&'a ObjectKind> {
        self.maybe_object_ref_by_search_key_in_scope(search_key.clone(), fn_scope)
            .or_else(move || match parent_scope {
                ScopeChain::CrateRoot { .. } | ScopeChain::Mod { .. } =>
                    self.maybe_object_ref_by_search_key_in_scope(search_key, parent_scope),
                ScopeChain::Fn { parent, .. } =>
                    self.maybe_fn_object_ref_by_tree_search_key(parent_scope, parent, search_key),
                ScopeChain::Trait { parent, .. } |
                ScopeChain::Object { parent, .. } |
                ScopeChain::Impl { parent, .. } =>
                    self.maybe_object_ref_by_search_key_in_scope(search_key.clone(), parent_scope)
                        .or_else(|| match &**parent {
                            ScopeChain::CrateRoot { .. } |
                            ScopeChain::Mod { ..} =>
                                self.maybe_object_ref_by_search_key_in_scope(search_key, parent),
                            _ => None,
                        }),
        })
    }

    pub fn maybe_object_ref_by_tree_search_key<'a>(&'a self, search_key: ScopeSearchKey, scope: &'a ScopeChain) -> Option<&'a ObjectKind> {
        match scope {
            ScopeChain::Mod { .. } | ScopeChain::CrateRoot { .. } =>
                self.maybe_object_ref_by_search_key_in_scope(search_key, scope),
            ScopeChain::Fn { parent, .. } =>
                self.maybe_fn_object_ref_by_tree_search_key(scope, parent, search_key),
            ScopeChain::Trait { parent, .. } |
            ScopeChain::Object { parent, .. } |
            ScopeChain::Impl { parent, .. } =>
                self.maybe_obj_or_parent_object_ref_by_tree_search_key(scope, parent, search_key),
        }

    }
    pub fn maybe_object_ref_by_tree_key<'a>(&'a self, ty: &'a Type, scope: &'a ScopeChain) -> Option<&'a ObjectKind> {
         match scope {
             ScopeChain::Mod { .. } | ScopeChain::CrateRoot { .. } =>
                 self.maybe_local_scope_object_ref_by_key(ty, scope),
             ScopeChain::Fn { parent, .. } =>
                 self.maybe_fn_object_ref_by_tree_key(scope, parent, ty),
             ScopeChain::Trait { parent, .. } |
             ScopeChain::Object { parent, .. } |
             ScopeChain::Impl { parent, .. } =>
                 self.maybe_obj_or_parent_object_ref_by_tree_key(scope, parent, ty),
         }
    }

    pub fn maybe_type_model_kind_ref_by_key<'a>(&'a self, ty: &'a Type, scope: &'a ScopeChain) -> Option<&'a TypeModelKind> {
        self.maybe_object_ref_by_tree_key(ty, scope)
            .and_then(ObjectKind::maybe_type_model_kind_ref)
    }

    pub fn maybe_scope_item_ref<'a>(&'a self, path: &'a Path) -> Option<&'a ScopeItemKind> {
        if let Some(scope) = self.maybe_scope_ref(path) {
            let last_ident = &path.segments.last()?.ident;
            let ty = last_ident.to_type();
            if let Some(ObjectKind::Item(_, item)) = self.maybe_object_ref_by_search_key_in_scope(ScopeSearchKey::Type(ty, None), scope) {
                return Some(item);
            }
        }
        None
    }
    pub fn maybe_scope_item_ref_obj_first(&self, path: &Path) -> Option<&ScopeItemKind> {
        if let Some(scope) = self.maybe_scope_ref_obj_first(path) {
            let last_ident = &path.segments.last()?.ident;
            let ty = last_ident.to_type();
            if let Some(search_key) = ScopeSearchKey::maybe_from(ty) {
                if let Some(ObjectKind::Item(_, item)) = self.maybe_object_ref_by_tree_search_key(search_key, scope) {
                    return Some(item);
                }
            }
        }
        None
    }

    pub fn maybe_item_trait(&self, trait_path: &Path) -> Option<ItemTrait> {
        match self.maybe_scope_item_ref_obj_first(trait_path) {
            Some(ScopeItemKind::Item(Item::Trait(item_trait), ..)) => Some(item_trait.clone()),
            _ => None
        }
    }

    pub fn actual_scope_for_type(&self, ty: &Type, current_scope: &ScopeChain) -> Option<&ScopeChain> {
        let p = GenericBoundKey::Path(ty.to_path());
        let search_key = ScopeSearchKey::maybe_from_ref(ty)?;
        if let Some(st) = self.maybe_object_ref_by_search_key_in_scope(search_key, current_scope) {
            let self_ty = st.maybe_type()?;
            let self_path: Path = self_ty.to_path();
            self.maybe_scope_ref(&self_path)
        } else if let Some(import_path) = self.maybe_scope_import_path_ref(current_scope, &p) {
            self.maybe_scope_ref(import_path)
        } else {
            None
        }
    }
}



/// Imports
impl GlobalContext {
    pub fn maybe_scope_import_path_ref(&self, scope: &ScopeChain, chunk: &GenericBoundKey) -> Option<&Path> {
        self.imports.maybe_path(scope, chunk)
    }

    pub fn maybe_imports_scope_ref(&self, path: &Path) -> Option<&ScopeChain> {
        self.imports
            .inner
            .keys()
            .find(|scope_chain| path.eq(scope_chain.self_path_ref()))

    }

    pub fn maybe_import_path_ref(&self, scope: &ScopeChain, path: &GenericBoundKey) -> Option<&Path> {
        self.imports.maybe_import(scope, path)
    }

    pub fn maybe_import_scope_pair_ref(&self, scope_path_last_segment: &PathSegment, scope_path_candidate: &Path) -> Option<(&ScopeChain, &Path)> {
        self.maybe_imports_scope_ref(scope_path_candidate)
            .and_then(|reexport_scope| {
                let path = GenericBoundKey::ident(&scope_path_last_segment.ident);
                self.maybe_import_path_ref(reexport_scope, &path).map(|import| (reexport_scope, import))
            })
    }

    // We need to find full qualified paths for involved chunk and bind them to actual items
    pub(crate) fn maybe_refined_object(&self, scope: &ScopeChain, object: &ObjectKind) -> Option<ObjectKind> {
        let mut refined = object.clone();
        refined.refine_in_scope(scope, self)
            .then_some(refined)
    }
    pub(crate) fn maybe_custom_type(&self, ty: &Type) -> Option<Type> {
        self.custom.maybe_type(ty)
    }

    fn num_of_nested_exposable_types_for_generic<'a>(&'a self, args: &'a CommaPunctuatedNestedArguments) -> usize {
        args.iter().filter_map(|arg| {
            match arg.object().maybe_type_model_kind_ref() {
                Some(tyc) => match tyc {
                    TypeModelKind::Unknown(..) |
                    TypeModelKind::Dictionary(DictTypeModelKind::NonPrimitiveOpaque(..)) =>
                        self.maybe_custom_type(tyc.as_type())
                            .is_some()
                            .then_some(tyc),
                    TypeModelKind::Dictionary(
                        DictTypeModelKind::NonPrimitiveFermentable(
                            DictFermentableModelKind::Cow(TypeModel { nested_arguments, .. }) |
                            DictFermentableModelKind::SmartPointer(
                                SmartPointerModelKind::Arc(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::Box(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::Rc(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::Mutex(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::OnceLock(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::RwLock(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::Cell(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::RefCell(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::UnsafeCell(TypeModel { nested_arguments, .. }) |
                                SmartPointerModelKind::Pin(TypeModel { nested_arguments, .. })
                            ) |
                            DictFermentableModelKind::Group(
                                GroupModelKind::BTreeSet(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::HashSet(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::Map(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::Result(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::Vec(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::IndexMap(TypeModel { nested_arguments, .. }) |
                                GroupModelKind::IndexSet(TypeModel { nested_arguments, .. })
                            ) |
                            DictFermentableModelKind::Other(TypeModel { nested_arguments, .. }))) |
                    TypeModelKind::Optional(TypeModel { nested_arguments, .. }) => {
                        let is_custom = self.maybe_custom_type(tyc.as_type());
                        let num_of_fermentable = self.num_of_nested_exposable_types_for_generic(nested_arguments);
                        let all_of_them_are_non_fermentable = num_of_fermentable == 0 && !nested_arguments.is_empty();
                        (!all_of_them_are_non_fermentable || is_custom.is_some() || nested_arguments.is_empty())
                            .then_some(tyc)
                    },
                    TypeModelKind::Trait(..) | TypeModelKind::TraitType(..) => None,
                    tyc => Some(tyc)
                }
                _ => None
            }
        }).collect::<Vec<_>>().len()
    }

    pub(crate) fn should_skip_from_expanding(&self, object: &ObjectKind) -> bool {
        let skip = match object.maybe_type_model_kind_ref() {
            Some(conversion) => {
                let maybe_custom = self.maybe_custom_type(conversion.as_type());
                let nested_arguments = conversion.nested_arguments_ref();
                let num_of_fermentable = self.num_of_nested_exposable_types_for_generic(nested_arguments);
                let all_of_them_are_non_fermentable = num_of_fermentable == 0 && !nested_arguments.is_empty();
                let maybe_lambda = conversion.is_lambda();
                all_of_them_are_non_fermentable && maybe_custom.is_none() && !maybe_lambda
            }
            None => false
        };
        skip
    }

}




/// Scope
impl GlobalContext {
    pub fn scope_mut(&mut self, scope: &ScopeChain) -> &mut TypeChain {
        self.scope_register.type_chain_mut(scope)
    }
    pub fn maybe_scope_ref(&self, path: &Path) -> Option<&ScopeChain> {
        self.scope_register.maybe_scope(path)
    }
    pub fn maybe_scope_ref_obj_first(&self, path: &Path) -> Option<&ScopeChain> {
        self.scope_register.maybe_first_obj_scope(path)
    }
    pub fn maybe_object_ref_by_value<'a>(&'a self, ty: &'a Type) -> Option<&'a ObjectKind> {
        ScopeSearchKey::maybe_from_ref(ty)
            .and_then(|search_key| self.maybe_object_ref_by_search_value(search_key.clone()))
    }
    fn maybe_local_scope_object_ref_by_key<'a>(&'a self, ty: &'a Type, scope: &'a ScopeChain) -> Option<&'a ObjectKind> {
        ScopeSearchKey::maybe_from_ref(ty)
            .and_then(|search_key| self.maybe_object_ref_by_search_key_in_scope(search_key, scope))
    }
    fn maybe_object_ref_by_search_key_in_scope<'a>(&'a self, search_key: ScopeSearchKey, scope: &'a ScopeChain) -> Option<&'a ObjectKind> {
        self.scope_register.maybe_object_ref_by_key_in_scope(search_key, scope)

    }
    fn maybe_object_ref_by_search_value(&self, search_key: ScopeSearchKey) -> Option<&ObjectKind> {
        self.scope_register.maybe_object_ref_by_value(search_key)
    }
}