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