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_expr_to(name: Self::Name, ty: &Type, expr: Self::Expr) -> ConversionToComposer<Self> {
71 ConversionToComposer::<Self>::value_expr(name, ty, expr)
72 }
73}
74
75#[derive(Clone, Debug)]
76pub struct RustSpecification;
77
78impl Specification for RustSpecification {
79 type Attr = Vec<Attribute>;
80 type Gen = Option<Generics>;
81 type Lt = Vec<Lifetime>;
82 type TYC = TypeContext;
83 type Interface = InterfacePresentation;
84 type Expr = Expression<Self>;
85 type Var = FFIVariable<Self, Type>;
86 type Name = Name<Self>;
87 type Fermentate = RustFermentate;
88}
89
90pub trait LangAttrSpecification<T: Clone>: Clone + Default {
91 fn from_attrs(attrs: Vec<Attribute>) -> Self;
92 fn from_cfg_attrs(attrs: &Vec<Attribute>) -> Self {
93 Self::from_attrs(attrs.cfg_attributes())
94 }
95}
96pub trait LangGenSpecification<T: Clone>: Clone + Default + Debug {
97 fn from_generics(generics: Option<Generics>) -> Self;
98}
99pub trait LangLifetimeSpecification<T: Clone>: Clone + Default + Debug {
100 #[allow(unused)]
101 fn from_lifetimes(lifetimes: Vec<Lifetime>) -> Self;
102 #[allow(unused)]
103 fn add_lifetime(&mut self, lifetime: Lifetime);
104}
105
106impl<T> LangAttrSpecification<T> for Vec<Attribute> where T: Clone {
107 fn from_attrs(attrs: Vec<Attribute>) -> Self {
108 attrs
109 }
110}
111impl<T> LangGenSpecification<T> for Option<Generics> where T: Clone {
112 fn from_generics(generics: Option<Generics>) -> Self {
113 generics
114 }
115}
116impl<T> LangLifetimeSpecification<T> for Vec<Lifetime> where T: Clone {
117 fn from_lifetimes(lifetimes: Vec<Lifetime>) -> Self {
120 lifetimes
121 }
122
123 fn add_lifetime(&mut self, lifetime: Lifetime) {
124 self.push(lifetime);
125 }
126}
127#[cfg(feature = "objc")]
128impl<T> LangAttrSpecification<T> for AttrWrapper where T: Clone {
129 fn from_attrs(attrs: Vec<Attribute>) -> Self {
130 AttrWrapper::from(attrs)
131 }
132}
133
134#[derive(Debug, Clone)]
135#[non_exhaustive]
136pub enum Lang {
137 #[cfg(feature = "objc")]
138 ObjC(objc::Config),
139 #[cfg(feature = "java")]
140 Java(java::Config)
141}
142
143#[cfg(any(feature = "objc", feature = "java"))]
144impl CrateTreeConsumer for Lang {
145 fn generate(&self, crate_tree: &CrateTree) -> Result<(), error::Error> {
146 match self {
147 #[cfg(feature = "objc")]
148 Lang::ObjC(config) =>
149 config.generate(crate_tree),
150 #[cfg(feature = "java")]
151 Lang::Java(config) =>
152 config.generate(crate_tree),
153 #[cfg(all(not(feature = "objc"), not(feature = "java")))]
154 _ => Ok(())
155 }
156 }
157}
158
159#[cfg(feature = "objc")]
160impl Config {
161 pub fn maybe_objc_config(&self) -> Option<&objc::Config> {
162 self.languages.iter().find_map(|lang| match lang {
163 Lang::ObjC(config) => Some(config),
164 #[cfg(feature = "java")]
165 _ => None
166 })
167 }
168}
169
170#[cfg(feature = "objc")]
171impl FFIVarResolve<objc::ObjCSpecification> for GenericTypeKind {}