1mod model;
10
11use askama::Template;
12use nativeview_codec::{FlatOpFieldType, FlatOpStreamDesc, FloatConstraint};
13
14use super::model::U64Split;
15use super::{CXX, DART, JAVA, SWIFT, TS, render};
16use crate::error::CodegenError;
17use crate::schema::export::GeneratedFile;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub(super) enum ScalarKind {
22 U32,
23 ColorRgba,
24 F32(FloatConstraint),
25}
26
27impl ScalarKind {
28 pub(super) const fn is_float(self) -> bool {
29 matches!(self, Self::F32(_))
30 }
31
32 pub(super) const fn rejects_negative(self) -> bool {
34 matches!(self, Self::F32(FloatConstraint::NonNegative))
35 }
36
37 pub(super) const fn rejects_non_positive(self) -> bool {
39 matches!(self, Self::F32(FloatConstraint::Positive))
40 }
41
42 pub(super) const fn is_color(self) -> bool {
43 matches!(self, Self::ColorRgba)
44 }
45
46 pub(super) const fn cxx_type(self) -> &'static str {
47 match self {
48 Self::U32 => "uint32_t",
49 Self::ColorRgba => "ColorRgba",
50 Self::F32(_) => "float",
51 }
52 }
53
54 pub(super) const fn cxx_default(self) -> &'static str {
55 match self {
56 Self::U32 => "0u",
57 Self::ColorRgba => "",
58 Self::F32(_) => "0.0f",
59 }
60 }
61
62 pub(super) const fn java_type(self) -> &'static str {
63 match self {
64 Self::U32 | Self::ColorRgba => "int",
65 Self::F32(_) => "float",
66 }
67 }
68
69 pub(super) const fn swift_type(self) -> &'static str {
70 match self {
71 Self::U32 | Self::ColorRgba => "UInt32",
72 Self::F32(_) => "Float",
73 }
74 }
75
76 pub(super) const fn dart_type(self) -> &'static str {
77 match self {
78 Self::U32 | Self::ColorRgba => "int",
79 Self::F32(_) => "double",
80 }
81 }
82
83 pub(super) const fn ts_type(self) -> &'static str {
84 "number"
85 }
86
87 pub(super) const fn of(ty: FlatOpFieldType) -> Option<Self> {
89 match ty {
90 FlatOpFieldType::U32 => Some(Self::U32),
91 FlatOpFieldType::ColorRgba => Some(Self::ColorRgba),
92 FlatOpFieldType::F32(constraint) => Some(Self::F32(constraint)),
93 FlatOpFieldType::Point2Slice { .. } | FlatOpFieldType::StrSlice { .. } => None,
94 }
95 }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub(super) enum ScopeRole {
101 None,
102 Push,
103 Pop,
104}
105
106#[derive(Debug, Clone)]
108pub(super) struct FlatOpScalarField<'a> {
109 pub(super) name: &'a str,
110 pub(super) camel: String,
111 pub(super) byte_offset: usize,
112 pub(super) arg_index: usize,
113 pub(super) kind: ScalarKind,
114}
115
116#[derive(Debug, Clone)]
118pub(super) struct FlatOpStrSliceField<'a> {
119 pub(super) name: &'a str,
120 pub(super) camel: String,
121 pub(super) arg_index: usize,
122 pub(super) max_bytes: u32,
123}
124
125#[derive(Debug, Clone)]
127pub(super) struct FlatOpPointSliceField<'a> {
128 pub(super) name: &'a str,
129 pub(super) camel: String,
130 pub(super) count_byte_offset: usize,
131 pub(super) max_const_screaming: &'a str,
132 pub(super) max_const_pascal: String,
133 pub(super) max_const_camel: String,
134 pub(super) coord_kind: ScalarKind,
136}
137
138#[derive(Debug, Clone)]
140pub(super) struct FlatOpArg {
141 pub(super) camel: String,
142 pub(super) arg_index: usize,
143 pub(super) kind: FlatOpArgKind,
144}
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147pub(super) enum FlatOpArgKind {
148 Scalar(ScalarKind),
149 Str,
150}
151
152impl FlatOpArg {
153 pub(super) const fn swift_type(&self) -> &'static str {
154 match self.kind {
155 FlatOpArgKind::Scalar(k) => k.swift_type(),
156 FlatOpArgKind::Str => "String",
157 }
158 }
159
160 pub(super) const fn java_type(&self) -> &'static str {
161 match self.kind {
162 FlatOpArgKind::Scalar(k) => k.java_type(),
163 FlatOpArgKind::Str => "String",
164 }
165 }
166
167 pub(super) const fn dart_type(&self) -> &'static str {
168 match self.kind {
169 FlatOpArgKind::Scalar(k) => k.dart_type(),
170 FlatOpArgKind::Str => "String",
171 }
172 }
173
174 pub(super) const fn ts_type(&self) -> &'static str {
175 match self.kind {
176 FlatOpArgKind::Scalar(k) => k.ts_type(),
177 FlatOpArgKind::Str => "string",
178 }
179 }
180}
181
182#[derive(Debug, Clone)]
184pub(super) enum FlatOpStep<'a> {
185 Scalar(FlatOpScalarField<'a>),
186 StrSlice(FlatOpStrSliceField<'a>),
187 Point2Slice(FlatOpPointSliceField<'a>),
188}
189
190impl<'a> FlatOpStep<'a> {
191 pub(super) fn as_scalar(&self) -> Option<&FlatOpScalarField<'a>> {
192 match self {
193 Self::Scalar(f) => Some(f),
194 _ => None,
195 }
196 }
197
198 pub(super) fn as_str_slice(&self) -> Option<&FlatOpStrSliceField<'a>> {
199 match self {
200 Self::StrSlice(s) => Some(s),
201 _ => None,
202 }
203 }
204
205 pub(super) fn as_point_slice(&self) -> Option<&FlatOpPointSliceField<'a>> {
206 match self {
207 Self::Point2Slice(ps) => Some(ps),
208 _ => None,
209 }
210 }
211}
212
213#[derive(Debug, Clone)]
215pub(super) struct FlatOpSliceLimit<'a> {
216 pub(super) screaming: &'a str,
217 pub(super) pascal: String,
218 pub(super) camel: String,
219 pub(super) max_len: u32,
220}
221
222#[derive(Debug, Clone)]
224pub(super) struct FlatOpScopeModel<'a> {
225 pub(super) limit_screaming: &'a str,
226 pub(super) limit_pascal: String,
227 pub(super) limit_camel: String,
228 pub(super) max_depth: u32,
229 pub(super) pop_pascal: &'a str,
230 pub(super) pop_snake: String,
231}
232
233#[derive(Debug, Clone)]
235pub(super) struct FlatOpModel<'a> {
236 pub(super) name: &'a str,
237 pub(super) snake: String,
238 pub(super) camel: String,
239 pub(super) screaming: String,
240 pub(super) opcode: u32,
241 pub(super) scope_role: ScopeRole,
242 pub(super) scalars: Vec<FlatOpScalarField<'a>>,
243 pub(super) str_slices: Vec<FlatOpStrSliceField<'a>>,
244 pub(super) point_slice: Option<FlatOpPointSliceField<'a>>,
245 pub(super) non_point_args: Vec<FlatOpArg>,
246 pub(super) steps: Vec<FlatOpStep<'a>>,
247 pub(super) fixed_bytes: usize,
250}
251
252impl FlatOpModel<'_> {
253 pub(super) fn is_scope_push(&self) -> bool {
254 self.scope_role == ScopeRole::Push
255 }
256
257 pub(super) fn is_scope_pop(&self) -> bool {
258 self.scope_role == ScopeRole::Pop
259 }
260
261 pub(super) fn has_point_slice(&self) -> bool {
262 self.point_slice.is_some()
263 }
264
265 pub(super) fn has_str_slice(&self) -> bool {
266 !self.str_slices.is_empty()
267 }
268}
269
270#[derive(Debug, Clone)]
272pub(super) struct FlatOpStreamModel<'a> {
273 pub(super) name: &'a str,
274 pub(super) snake: String,
275 pub(super) screaming: String,
276 pub(super) kind_name: &'a str,
277 pub(super) kind: U64Split,
278 pub(super) max_ops_screaming: &'a str,
279 pub(super) max_ops_pascal: String,
280 pub(super) max_ops_camel: String,
281 pub(super) max_ops: u32,
282 pub(super) slice_limits: Vec<FlatOpSliceLimit<'a>>,
283 pub(super) header: Vec<FlatOpScalarField<'a>>,
285 pub(super) header_bytes: usize,
287 pub(super) has_point_slice: bool,
288 pub(super) has_str_slice: bool,
289 pub(super) has_color: bool,
290 pub(super) scope: Option<FlatOpScopeModel<'a>>,
291 pub(super) ops: Vec<FlatOpModel<'a>>,
292}
293
294#[derive(Template)]
295#[template(path = "c/flat_op_codec.hpp", escape = "none")]
296struct CxxFlatOpCodec<'a> {
297 m: &'a FlatOpStreamModel<'a>,
298}
299
300#[derive(Template)]
301#[template(path = "swift/FlatOpCodec.swift", escape = "none")]
302struct SwiftFlatOpCodec<'a> {
303 m: &'a FlatOpStreamModel<'a>,
304 import_nv_host: bool,
305}
306
307#[derive(Template)]
308#[template(path = "java/FlatOpCodec.java", escape = "none")]
309struct JavaFlatOpCodec<'a> {
310 package: &'a str,
311 m: &'a FlatOpStreamModel<'a>,
312}
313
314#[derive(Template)]
315#[template(path = "dart/flat_op_codec.dart", escape = "none")]
316struct DartFlatOpCodec<'a> {
317 m: &'a FlatOpStreamModel<'a>,
318}
319
320#[derive(Template)]
321#[template(path = "ts/flat_op_codec.ts", escape = "none")]
322struct TsFlatOpCodec<'a> {
323 m: &'a FlatOpStreamModel<'a>,
324}
325
326pub fn generate_flat_op_codec_files(
330 desc: &FlatOpStreamDesc,
331) -> Result<Vec<GeneratedFile>, CodegenError> {
332 let m = FlatOpStreamModel::new(desc)?;
333 generate_flat_op_codec_files_with_targets(
334 &m,
335 (format!("{SWIFT}/Sources/NativeViewHost/Nv{}.generated.swift", m.name), false),
336 Some((
337 "io.github.nativeview.host.generated",
338 format!("{JAVA}/main/java/io/github/nativeview/host/generated/Nv{}.java", m.name),
339 )),
340 format!("{DART}/lib/nv_{}.generated.dart", m.snake),
341 )
342}
343
344pub fn generate_flat_op_codec_files_for_module(
351 desc: &FlatOpStreamDesc,
352 cap_dir: &str,
353 swift_target: Option<&str>,
354 java_package: Option<&str>,
355) -> Result<Vec<GeneratedFile>, CodegenError> {
356 let m = FlatOpStreamModel::new(desc)?;
357 let swift_target_pair = match swift_target {
358 Some(target) => (
359 format!("packages/modules/{cap_dir}/apple/Sources/{target}/Nv{}.generated.swift", m.name),
360 true,
361 ),
362 None => (format!("{SWIFT}/Sources/NativeViewHost/Nv{}.generated.swift", m.name), false),
363 };
364 let java_target = java_package.map(|pkg| {
365 let pkg_path = pkg.replace('.', "/");
366 (
367 format!("{pkg}.generated"),
368 format!(
369 "packages/modules/{cap_dir}/android/src/main/java/{pkg_path}/generated/Nv{}.java",
370 m.name
371 ),
372 )
373 });
374 let dart_path = format!("packages/modules/{cap_dir}/dart/lib/src/nv_{}.generated.dart", m.snake);
375 generate_flat_op_codec_files_with_targets(
376 &m,
377 swift_target_pair,
378 java_target.as_ref().map(|(pkg, path)| (pkg.as_str(), path.clone())),
379 dart_path,
380 )
381}
382
383fn generate_flat_op_codec_files_with_targets(
384 m: &FlatOpStreamModel<'_>,
385 (swift_path, import_nv_host): (String, bool),
386 java_target: Option<(&str, String)>,
387 dart_path: String,
388) -> Result<Vec<GeneratedFile>, CodegenError> {
389 let mut files = Vec::with_capacity(5);
390 files.push(render(
391 &CxxFlatOpCodec { m },
392 format!("{CXX}/include/nv/generated/nv_{}.generated.hpp", m.snake),
393 )?);
394 files.push(render(&SwiftFlatOpCodec { m, import_nv_host }, swift_path)?);
395 if let Some((package, path)) = java_target {
396 files.push(render(&JavaFlatOpCodec { package, m }, path)?);
397 }
398 files.push(render(&DartFlatOpCodec { m }, dart_path)?);
399 files.push(render(
400 &TsFlatOpCodec { m },
401 format!("{TS}/src/generated/nv_{}.generated.ts", m.snake),
402 )?);
403 Ok(files)
404}