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
use crate::bridged_type::BridgedType;
use crate::errors::{ParseError, ParseErrors};
use crate::parse::parse_extern_mod::ForeignModParser;
use crate::parse::parse_struct::SharedStructDeclarationParser;
use crate::SwiftBridgeModule;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Item, ItemMod};

mod parse_extern_mod;
mod parse_struct;

mod type_declarations;
pub(crate) use self::type_declarations::*;

impl Parse for SwiftBridgeModule {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let module_and_errors: SwiftBridgeModuleAndErrors = input.parse()?;

        module_and_errors.errors.combine_all()?;

        Ok(module_and_errors.module)
    }
}

pub(crate) struct SwiftBridgeModuleAndErrors {
    pub module: SwiftBridgeModule,
    pub errors: ParseErrors,
}

/// The language that a bridge type or function's implementation lives in.
#[derive(Debug, PartialEq, Copy, Clone)]
pub(crate) enum HostLang {
    /// The type or function is defined Rust.
    Rust,
    /// The type or function is defined Swift.
    Swift,
}

impl HostLang {
    pub fn is_rust(&self) -> bool {
        matches!(self, HostLang::Rust)
    }

    pub fn is_swift(&self) -> bool {
        matches!(self, HostLang::Swift)
    }
}

impl Parse for SwiftBridgeModuleAndErrors {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut errors = ParseErrors::new();

        if let Ok(item_mod) = input.parse::<ItemMod>() {
            let module_name = item_mod.ident;

            let mut functions = vec![];
            let mut type_declarations = TypeDeclarations::default();
            let mut unresolved_types = vec![];

            for outer_mod_item in item_mod.content.unwrap().1 {
                match outer_mod_item {
                    Item::ForeignMod(foreign_mod) => {
                        ForeignModParser {
                            errors: &mut errors,
                            type_declarations: &mut type_declarations,
                            functions: &mut functions,
                            unresolved_types: &mut unresolved_types,
                        }
                        .parse(foreign_mod)?;
                    }
                    Item::Struct(item_struct) => {
                        let shared_struct = SharedStructDeclarationParser {
                            item_struct,
                            errors: &mut errors,
                        }
                        .parse()?;
                        type_declarations.insert(
                            shared_struct.name.to_string(),
                            TypeDeclaration::Shared(SharedTypeDeclaration::Struct(shared_struct)),
                        );
                    }
                    _ => {
                        todo!(
                            r#"
                        Push an error that the module may only contain `extern` blocks, structs
                        and enums
                        "#
                        )
                    }
                };
            }

            for unresolved_type in unresolved_types.into_iter() {
                if BridgedType::new_with_type(&unresolved_type, &type_declarations).is_some() {
                    continue;
                }

                errors.push(ParseError::UndeclaredType {
                    ty: unresolved_type.clone(),
                });
            }

            let module = SwiftBridgeModule {
                name: module_name,
                types: type_declarations,
                functions,
                swift_bridge_path: syn::parse2(quote! { swift_bridge }).unwrap(),
            };
            Ok(SwiftBridgeModuleAndErrors { module, errors })
        } else {
            return Err(syn::Error::new_spanned(
                input.to_string(),
                "Only modules are supported.",
            ));
        }
    }
}