ferment_sys/lang/
mod.rs

1pub(crate) mod rust;
2
3#[cfg(feature = "objc")]
4pub(crate) mod objc;
5
6#[cfg(feature = "java")]
7pub(crate) mod java;
8
9
10use std::fmt::{Debug, Display};
11use proc_macro2::Ident;
12use quote::ToTokens;
13use syn::{Attribute, Generics, Lifetime, Type};
14use crate::composable::CfgAttributes;
15use crate::composer::{ConversionFromComposer, ConversionToComposer, VarComposable, VarComposer};
16#[cfg(any(feature = "objc", feature = "java"))]
17use crate::error;
18#[cfg(feature = "objc")]
19use crate::Config;
20#[cfg(feature = "objc")]
21use crate::kind::GenericTypeKind;
22#[cfg(feature = "objc")]
23use crate::ext::FFIVarResolve;
24use crate::ext::{ExpressionComposable, Mangle, MangleDefault, ToType};
25#[cfg(feature = "objc")]
26use crate::lang::objc::composers::AttrWrapper;
27use crate::presentable::{NameTreeContext, TypeContext, Expression};
28use crate::presentation::{DictionaryName, FFIVariable, InterfacePresentation, Name, RustFermentate};
29#[cfg(any(feature = "objc", feature = "java"))]
30use crate::tree::CrateTree;
31
32
33#[cfg(any(feature = "objc", feature = "java"))]
34pub trait CrateTreeConsumer {
35    fn generate(&self, crate_tree: &CrateTree) -> Result<(), error::Error>;
36}
37
38pub trait FromDictionary {
39    fn dictionary_name(dictionary: DictionaryName) -> Self;
40}
41
42pub trait NameComposable<SPEC>
43    where SPEC: Specification {
44    fn ident(ident: Ident) -> Self;
45    fn index(ident: usize) -> Self;
46    fn unnamed_arg(index: usize) -> Self;
47}
48
49pub trait LangFermentable: Clone + Debug {
50
51}
52pub trait Specification: Clone + Debug {
53    type Attr: Clone + LangAttrSpecification<Self::Fermentate> + Debug;
54    type Gen: LangGenSpecification<Self::Fermentate>;
55    type Lt: LangLifetimeSpecification<Self::Fermentate>;
56    type TYC: NameTreeContext;
57    type Interface: ToTokens;
58    type Expr: ExpressionComposable<Self>;
59    type Var: VarComposable<Self> + ToType;
60    type Name: Clone + Default + Display + ToTokens + Mangle<MangleDefault> + FromDictionary + NameComposable<Self>;
61    type Fermentate: LangFermentable + ToTokens;
62
63    fn value_var(ty: &Type) -> VarComposer<Self> {
64        VarComposer::<Self>::value(ty)
65    }
66
67    fn value_expr_from(name: Self::Name, ty: &Type, expr: Self::Expr) -> ConversionFromComposer<Self> {
68        ConversionFromComposer::<Self>::value_expr(name, ty, expr)
69    }
70    fn value_ref_expr_from(name: &Self::Name, ty: &Type, expr: Self::Expr) -> ConversionFromComposer<Self> {
71        ConversionFromComposer::<Self>::value_ref_expr(name, ty, expr)
72    }
73    fn value_expr_to(name: Self::Name, ty: &Type, expr: Self::Expr) -> ConversionToComposer<Self> {
74        ConversionToComposer::<Self>::value_expr(name, ty, expr)
75    }
76    fn value_ref_expr_to(name: &Self::Name, ty: &Type, expr: Self::Expr) -> ConversionToComposer<Self> {
77        ConversionToComposer::<Self>::value_ref_expr(name, ty, expr)
78    }
79}
80
81#[derive(Clone, Debug)]
82pub struct RustSpecification;
83
84impl Specification for RustSpecification {
85    type Attr = Vec<Attribute>;
86    type Gen = Option<Generics>;
87    type Lt = Vec<Lifetime>;
88    type TYC = TypeContext;
89    type Interface = InterfacePresentation;
90    type Expr = Expression<Self>;
91    type Var = FFIVariable<Self, Type>;
92    type Name = Name<Self>;
93    type Fermentate = RustFermentate;
94}
95
96pub trait LangAttrSpecification<T: Clone>: Clone + Default {
97    fn from_attrs(attrs: Vec<Attribute>) -> Self;
98    fn from_cfg_attrs(attrs: &Vec<Attribute>) -> Self {
99        Self::from_attrs(attrs.cfg_attributes())
100    }
101}
102pub trait LangGenSpecification<T: Clone>: Clone + Default + Debug {
103    fn from_generics(generics: Option<Generics>) -> Self;
104}
105pub trait LangLifetimeSpecification<T: Clone>: Clone + Default + Debug {
106    #[allow(unused)]
107    fn from_lifetimes(lifetimes: Vec<Lifetime>) -> Self;
108    #[allow(unused)]
109    fn add_lifetime(&mut self, lifetime: Lifetime);
110}
111
112impl<T> LangAttrSpecification<T> for Vec<Attribute> where T: Clone {
113    fn from_attrs(attrs: Vec<Attribute>) -> Self {
114        attrs
115    }
116}
117impl<T> LangGenSpecification<T> for Option<Generics> where T: Clone {
118    fn from_generics(generics: Option<Generics>) -> Self {
119        generics
120    }
121}
122impl<T> LangLifetimeSpecification<T> for Vec<Lifetime> where T: Clone {
123    // type Iter = Vec<Lifetime>;
124
125    fn from_lifetimes(lifetimes: Vec<Lifetime>) -> Self {
126        lifetimes
127    }
128
129    fn add_lifetime(&mut self, lifetime: Lifetime) {
130        self.push(lifetime);
131    }
132}
133#[cfg(feature = "objc")]
134impl<T> LangAttrSpecification<T> for AttrWrapper where T: Clone {
135    fn from_attrs(attrs: Vec<Attribute>) -> Self {
136        AttrWrapper::from(attrs)
137    }
138}
139
140#[derive(Debug, Clone)]
141#[non_exhaustive]
142pub enum Lang {
143    #[cfg(feature = "objc")]
144    ObjC(objc::Config),
145    #[cfg(feature = "java")]
146    Java(java::Config)
147}
148
149#[cfg(any(feature = "objc", feature = "java"))]
150impl CrateTreeConsumer for Lang {
151    fn generate(&self, crate_tree: &CrateTree) -> Result<(), error::Error> {
152        match self {
153            #[cfg(feature = "objc")]
154            Lang::ObjC(config) =>
155                config.generate(crate_tree),
156            #[cfg(feature = "java")]
157            Lang::Java(config) =>
158                config.generate(crate_tree),
159            #[cfg(all(not(feature = "objc"), not(feature = "java")))]
160            _ => Ok(())
161        }
162    }
163}
164
165#[cfg(feature = "objc")]
166impl Config {
167    pub fn maybe_objc_config(&self) -> Option<&objc::Config> {
168        self.languages.iter().find_map(|lang| match lang {
169            Lang::ObjC(config) => Some(config),
170            #[cfg(feature = "java")]
171            _ => None
172        })
173    }
174}
175
176#[cfg(feature = "objc")]
177impl FFIVarResolve<objc::ObjCSpecification> for GenericTypeKind {}