lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

wasm_declarations.rs (35325B)


      1 use std::path::Path;
      2 
      3 use dto_bindgen_backend_ts::{
      4     TypeScriptDeclaration, TypeScriptModule, TypeScriptParameter, TypeScriptProperty,
      5     TypeScriptType, TypeScriptValue,
      6 };
      7 
      8 use crate::sdk_generation::package_matrix::WasmPackageSpec;
      9 
     10 const WASM_DECLARATION_HEADER: &str =
     11     "// @generated by cargo xtask generate wasm via dto_bindgen\n// Do not edit by hand.\n";
     12 
     13 #[derive(Clone, Debug, Eq, PartialEq)]
     14 pub struct WasmDeclarationFile {
     15     pub relative_path: String,
     16     pub contents: String,
     17 }
     18 
     19 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     20 struct WasmDeclarationInventory {
     21     public_functions: &'static [WasmPublicFunction],
     22     support_exports: &'static [WasmSupportExport],
     23 }
     24 
     25 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     26 struct WasmPublicFunction {
     27     name: &'static str,
     28     parameter: Option<&'static str>,
     29     bigint_parameter: Option<&'static str>,
     30     return_kind: WasmReturnKind,
     31     low_level: LowLevelSignature,
     32 }
     33 
     34 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     35 enum WasmReturnKind {
     36     String,
     37     Any,
     38     Void,
     39 }
     40 
     41 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     42 struct LowLevelSignature {
     43     number_parameters: usize,
     44     bigint_parameters: usize,
     45     returns: usize,
     46 }
     47 
     48 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     49 struct WasmSupportExport {
     50     name: &'static str,
     51     export_type: WasmSupportExportType,
     52 }
     53 
     54 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     55 enum WasmSupportExportType {
     56     Memory,
     57     Table,
     58     Function(LowLevelSignature),
     59     NumberFunction(usize),
     60 }
     61 
     62 macro_rules! public_function {
     63     ($name:literal, $parameter:expr, $return_kind:ident, $low_parameters:literal, $low_returns:literal) => {
     64         WasmPublicFunction {
     65             name: $name,
     66             parameter: $parameter,
     67             bigint_parameter: None,
     68             return_kind: WasmReturnKind::$return_kind,
     69             low_level: LowLevelSignature {
     70                 number_parameters: $low_parameters,
     71                 bigint_parameters: 0,
     72                 returns: $low_returns,
     73             },
     74         }
     75     };
     76 }
     77 
     78 macro_rules! public_function_with_bigint {
     79     ($name:literal, $parameter:literal, $bigint_parameter:literal, $return_kind:ident, $low_number_parameters:literal, $low_bigint_parameters:literal, $low_returns:literal) => {
     80         WasmPublicFunction {
     81             name: $name,
     82             parameter: Some($parameter),
     83             bigint_parameter: Some($bigint_parameter),
     84             return_kind: WasmReturnKind::$return_kind,
     85             low_level: LowLevelSignature {
     86                 number_parameters: $low_number_parameters,
     87                 bigint_parameters: $low_bigint_parameters,
     88                 returns: $low_returns,
     89             },
     90         }
     91     };
     92 }
     93 
     94 macro_rules! memory_export {
     95     () => {
     96         WasmSupportExport {
     97             name: "memory",
     98             export_type: WasmSupportExportType::Memory,
     99         }
    100     };
    101 }
    102 
    103 macro_rules! table_export {
    104     ($name:literal) => {
    105         WasmSupportExport {
    106             name: $name,
    107             export_type: WasmSupportExportType::Table,
    108         }
    109     };
    110 }
    111 
    112 macro_rules! void_function_export {
    113     ($name:literal, $parameters:literal) => {
    114         WasmSupportExport {
    115             name: $name,
    116             export_type: WasmSupportExportType::Function(LowLevelSignature {
    117                 number_parameters: $parameters,
    118                 bigint_parameters: 0,
    119                 returns: 0,
    120             }),
    121         }
    122     };
    123 }
    124 
    125 macro_rules! number_function_export {
    126     ($name:literal, $parameters:literal) => {
    127         WasmSupportExport {
    128             name: $name,
    129             export_type: WasmSupportExportType::NumberFunction($parameters),
    130         }
    131     };
    132 }
    133 
    134 pub fn write_declaration_files(root: &Path, spec: WasmPackageSpec) -> Result<(), String> {
    135     let package_dir = root.join(spec.package_dir);
    136     for file in declaration_files(spec)? {
    137         crate::sdk_generation::fs::write_if_changed(
    138             &package_dir.join(&file.relative_path),
    139             &file.contents,
    140         )?;
    141     }
    142     Ok(())
    143 }
    144 
    145 pub fn declaration_files(spec: WasmPackageSpec) -> Result<Vec<WasmDeclarationFile>, String> {
    146     let inventory = inventory(spec)?;
    147     Ok(vec![
    148         WasmDeclarationFile {
    149             relative_path: format!("dist/{}.d.ts", spec.out_name),
    150             contents: render_declaration_module(primary_module(spec, inventory)),
    151         },
    152         WasmDeclarationFile {
    153             relative_path: format!("dist/{}_bg.wasm.d.ts", spec.out_name),
    154             contents: render_declaration_module(sidecar_module(spec, inventory)),
    155         },
    156     ])
    157 }
    158 
    159 fn inventory(spec: WasmPackageSpec) -> Result<WasmDeclarationInventory, String> {
    160     match spec.key {
    161         "event_codec" => Ok(WasmDeclarationInventory {
    162             public_functions: EVENT_CODEC_FUNCTIONS,
    163             support_exports: EVENT_CODEC_SUPPORT_EXPORTS,
    164         }),
    165         "replica_store" => Ok(WasmDeclarationInventory {
    166             public_functions: REPLICA_STORE_FUNCTIONS,
    167             support_exports: SQL_WASM_SUPPORT_EXPORTS,
    168         }),
    169         "replica_sync" => Ok(WasmDeclarationInventory {
    170             public_functions: REPLICA_SYNC_FUNCTIONS,
    171             support_exports: SQL_WASM_SUPPORT_EXPORTS,
    172         }),
    173         _ => Err(format!(
    174             "missing wasm declaration inventory for {}",
    175             spec.key
    176         )),
    177     }
    178 }
    179 
    180 fn primary_module(spec: WasmPackageSpec, inventory: WasmDeclarationInventory) -> TypeScriptModule {
    181     let mut module = TypeScriptModule::new(format!("dist/{}.d.ts", spec.out_name));
    182     for function in inventory.public_functions {
    183         module.push_declaration(TypeScriptDeclaration::function(
    184             function.name,
    185             public_parameters(function.parameter, function.bigint_parameter),
    186             public_return_type(function.return_kind),
    187         ));
    188     }
    189     module.push_declaration(TypeScriptDeclaration::type_alias(
    190         "InitInput",
    191         init_input_type(),
    192     ));
    193     module.push_declaration(TypeScriptDeclaration::type_alias(
    194         "InitOutput",
    195         init_output_type(inventory),
    196     ));
    197     module.push_declaration(TypeScriptDeclaration::type_alias(
    198         "SyncInitInput",
    199         sync_init_input_type(),
    200     ));
    201     module.push_declaration(TypeScriptDeclaration::function(
    202         "initSync",
    203         vec![TypeScriptParameter::new(
    204             "module",
    205             TypeScriptType::union(vec![
    206                 TypeScriptType::object(vec![TypeScriptProperty::new(
    207                     "module",
    208                     TypeScriptType::named("SyncInitInput"),
    209                 )]),
    210                 TypeScriptType::named("SyncInitInput"),
    211             ]),
    212         )],
    213         TypeScriptType::named("InitOutput"),
    214     ));
    215     module.push_declaration(TypeScriptDeclaration::default_function(
    216         "__wbg_init",
    217         vec![
    218             TypeScriptParameter::new(
    219                 "module_or_path",
    220                 TypeScriptType::union(vec![
    221                     TypeScriptType::object(vec![TypeScriptProperty::new(
    222                         "module_or_path",
    223                         TypeScriptType::union(vec![
    224                             TypeScriptType::named("InitInput"),
    225                             promise_of(TypeScriptType::named("InitInput")),
    226                         ]),
    227                     )]),
    228                     TypeScriptType::named("InitInput"),
    229                     promise_of(TypeScriptType::named("InitInput")),
    230                 ]),
    231             )
    232             .optional(),
    233         ],
    234         promise_of(TypeScriptType::named("InitOutput")),
    235     ));
    236     module
    237 }
    238 
    239 fn sidecar_module(spec: WasmPackageSpec, inventory: WasmDeclarationInventory) -> TypeScriptModule {
    240     let mut module = TypeScriptModule::new(format!("dist/{}_bg.wasm.d.ts", spec.out_name));
    241     for export in inventory.support_exports {
    242         if matches!(export.export_type, WasmSupportExportType::Memory) {
    243             module.push_declaration(support_export_declaration(*export));
    244         }
    245     }
    246     for function in inventory.public_functions {
    247         module.push_declaration(TypeScriptDeclaration::constant(
    248             function.name,
    249             Some(low_level_function_type(function.low_level)),
    250             TypeScriptValue::string(""),
    251         ));
    252     }
    253     for export in inventory.support_exports {
    254         if !matches!(export.export_type, WasmSupportExportType::Memory) {
    255             module.push_declaration(support_export_declaration(*export));
    256         }
    257     }
    258     module
    259 }
    260 
    261 fn render_declaration_module(module: TypeScriptModule) -> String {
    262     let mut rendered = String::from(WASM_DECLARATION_HEADER);
    263     rendered.push_str(&module.render_declaration());
    264     rendered
    265 }
    266 
    267 fn public_parameters(
    268     parameter: Option<&'static str>,
    269     bigint_parameter: Option<&'static str>,
    270 ) -> Vec<TypeScriptParameter> {
    271     let mut parameters = parameter
    272         .map(|name| vec![TypeScriptParameter::new(name, TypeScriptType::String)])
    273         .unwrap_or_default();
    274     if let Some(name) = bigint_parameter {
    275         parameters.push(TypeScriptParameter::new(
    276             name,
    277             TypeScriptType::named("bigint"),
    278         ));
    279     }
    280     parameters
    281 }
    282 
    283 fn public_return_type(kind: WasmReturnKind) -> TypeScriptType {
    284     match kind {
    285         WasmReturnKind::String => TypeScriptType::String,
    286         WasmReturnKind::Any => TypeScriptType::Any,
    287         WasmReturnKind::Void => TypeScriptType::Void,
    288     }
    289 }
    290 
    291 fn init_input_type() -> TypeScriptType {
    292     TypeScriptType::union(vec![
    293         TypeScriptType::named("RequestInfo"),
    294         TypeScriptType::named("URL"),
    295         TypeScriptType::named("Response"),
    296         TypeScriptType::named("BufferSource"),
    297         TypeScriptType::named("WebAssembly.Module"),
    298     ])
    299 }
    300 
    301 fn sync_init_input_type() -> TypeScriptType {
    302     TypeScriptType::union(vec![
    303         TypeScriptType::named("BufferSource"),
    304         TypeScriptType::named("WebAssembly.Module"),
    305     ])
    306 }
    307 
    308 fn init_output_type(inventory: WasmDeclarationInventory) -> TypeScriptType {
    309     let mut properties = vec![TypeScriptProperty::new("memory", memory_type()).readonly()];
    310     properties.extend(inventory.public_functions.iter().map(|function| {
    311         TypeScriptProperty::new(function.name, low_level_function_type(function.low_level))
    312             .readonly()
    313     }));
    314     properties.extend(
    315         inventory
    316             .support_exports
    317             .iter()
    318             .filter(|export| !matches!(export.export_type, WasmSupportExportType::Memory))
    319             .map(|export| {
    320                 TypeScriptProperty::new(export.name, support_export_type(*export)).readonly()
    321             }),
    322     );
    323     TypeScriptType::object(properties)
    324 }
    325 
    326 fn support_export_declaration(export: WasmSupportExport) -> TypeScriptDeclaration {
    327     TypeScriptDeclaration::constant(
    328         export.name,
    329         Some(support_export_type(export)),
    330         TypeScriptValue::string(""),
    331     )
    332 }
    333 
    334 fn support_export_type(export: WasmSupportExport) -> TypeScriptType {
    335     match export.export_type {
    336         WasmSupportExportType::Memory => memory_type(),
    337         WasmSupportExportType::Table => TypeScriptType::named("WebAssembly.Table"),
    338         WasmSupportExportType::Function(signature) => low_level_function_type(signature),
    339         WasmSupportExportType::NumberFunction(parameters) => {
    340             low_level_number_function_type(parameters)
    341         }
    342     }
    343 }
    344 
    345 fn memory_type() -> TypeScriptType {
    346     TypeScriptType::named("WebAssembly.Memory")
    347 }
    348 
    349 fn low_level_number_function_type(parameters: usize) -> TypeScriptType {
    350     TypeScriptType::function(number_parameters(parameters), TypeScriptType::Number)
    351 }
    352 
    353 fn low_level_function_type(signature: LowLevelSignature) -> TypeScriptType {
    354     let mut parameters = number_parameters(signature.number_parameters);
    355     parameters.extend((0..signature.bigint_parameters).map(|index| {
    356         TypeScriptParameter::new(format!("i64_arg{index}"), TypeScriptType::named("bigint"))
    357     }));
    358     TypeScriptType::function(parameters, low_level_return_type(signature.returns))
    359 }
    360 
    361 fn number_parameters(count: usize) -> Vec<TypeScriptParameter> {
    362     (0..count)
    363         .map(|index| {
    364             TypeScriptParameter::new(low_level_parameter_name(index), TypeScriptType::Number)
    365         })
    366         .collect()
    367 }
    368 
    369 fn low_level_parameter_name(index: usize) -> String {
    370     match index {
    371         0 => "a".to_owned(),
    372         1 => "b".to_owned(),
    373         2 => "c".to_owned(),
    374         3 => "d".to_owned(),
    375         _ => format!("arg{index}"),
    376     }
    377 }
    378 
    379 fn low_level_return_type(count: usize) -> TypeScriptType {
    380     match count {
    381         0 => TypeScriptType::Void,
    382         1 => TypeScriptType::Number,
    383         _ => TypeScriptType::tuple(vec![TypeScriptType::Number; count]),
    384     }
    385 }
    386 
    387 fn promise_of(type_expr: TypeScriptType) -> TypeScriptType {
    388     TypeScriptType::generic("Promise", vec![type_expr])
    389 }
    390 
    391 const EVENT_CODEC_FUNCTIONS: &[WasmPublicFunction] = &[
    392     public_function!("article_tags", Some("article_json"), String, 2, 4),
    393     public_function!("coop_tags", Some("coop_json"), String, 2, 4),
    394     public_function!("document_tags", Some("document_json"), String, 2, 4),
    395     public_function!("farm_crdt_change_tags", Some("input_json"), String, 2, 4),
    396     public_function!("farm_file_metadata_tags", Some("file_json"), String, 2, 4),
    397     public_function!("farm_tags", Some("farm_json"), String, 2, 4),
    398     public_function!(
    399         "farm_workspace_manifest_tags",
    400         Some("workspace_json"),
    401         String,
    402         2,
    403         4
    404     ),
    405     public_function!("file_metadata_tags", Some("metadata_json"), String, 2, 4),
    406     public_function!("follow_tags", Some("follow_json"), String, 2, 4),
    407     public_function!("generic_repost_tags", Some("repost_json"), String, 2, 4),
    408     public_function!("gift_wrap_tags", Some("gift_wrap_json"), String, 2, 4),
    409     public_function!("group_admins_tags", Some("group_json"), String, 2, 4),
    410     public_function!("group_create_group_tags", Some("group_json"), String, 2, 4),
    411     public_function!("group_create_invite_tags", Some("group_json"), String, 2, 4),
    412     public_function!("group_delete_event_tags", Some("group_json"), String, 2, 4),
    413     public_function!("group_delete_group_tags", Some("group_json"), String, 2, 4),
    414     public_function!("group_edit_metadata_tags", Some("group_json"), String, 2, 4),
    415     public_function!("group_join_request_tags", Some("group_json"), String, 2, 4),
    416     public_function!("group_leave_request_tags", Some("group_json"), String, 2, 4),
    417     public_function!("group_members_tags", Some("group_json"), String, 2, 4),
    418     public_function!("group_metadata_tags", Some("group_json"), String, 2, 4),
    419     public_function!("group_put_user_tags", Some("group_json"), String, 2, 4),
    420     public_function!("group_remove_user_tags", Some("group_json"), String, 2, 4),
    421     public_function!("group_roles_tags", Some("group_json"), String, 2, 4),
    422     public_function!("http_auth_tags", Some("auth_json"), String, 2, 4),
    423     public_function!("job_feedback_tags", Some("job_json"), String, 2, 4),
    424     public_function!("job_request_tags", Some("job_json"), String, 2, 4),
    425     public_function!("job_result_tags", Some("job_json"), String, 2, 4),
    426     public_function!("knowledge_claim_tags", Some("claim_json"), String, 2, 4),
    427     public_function!(
    428         "knowledge_field_report_tags",
    429         Some("report_json"),
    430         String,
    431         2,
    432         4
    433     ),
    434     public_function!(
    435         "knowledge_relation_tags",
    436         Some("relation_json"),
    437         String,
    438         2,
    439         4
    440     ),
    441     public_function!("knowledge_review_tags", Some("review_json"), String, 2, 4),
    442     public_function!("knowledge_source_tags", Some("source_json"), String, 2, 4),
    443     public_function!("list_set_tags", Some("list_json"), String, 2, 4),
    444     public_function!("list_tags", Some("list_json"), String, 2, 4),
    445     public_function!(
    446         "operational_listing_tags",
    447         Some("listing_json"),
    448         String,
    449         2,
    450         4
    451     ),
    452     public_function!(
    453         "operational_listing_tags_full",
    454         Some("listing_json"),
    455         String,
    456         2,
    457         4
    458     ),
    459     public_function!("message_file_tags", Some("message_json"), String, 2, 4),
    460     public_function!("message_tags", Some("message_json"), String, 2, 4),
    461     public_function!("plot_tags", Some("plot_json"), String, 2, 4),
    462     public_function!("reaction_tags", Some("reaction_json"), String, 2, 4),
    463     public_function!("relay_auth_tags", Some("auth_json"), String, 2, 4),
    464     public_function!("report_tags", Some("report_json"), String, 2, 4),
    465     public_function!("repost_tags", Some("repost_json"), String, 2, 4),
    466     public_function!("seal_tags", Some("seal_json"), String, 2, 4),
    467     public_function!(
    468         "trade_evidence_manifest_parse_json",
    469         Some("canonical_bytes_hex"),
    470         String,
    471         2,
    472         4
    473     ),
    474     public_function!(
    475         "rhi_evidence_report_parse_json",
    476         Some("canonical_content"),
    477         String,
    478         2,
    479         4
    480     ),
    481     public_function_with_bigint!(
    482         "rhi_evidence_attestation_build_draft",
    483         "canonical_content",
    484         "created_at_unix_s",
    485         String,
    486         2,
    487         1,
    488         4
    489     ),
    490     public_function!(
    491         "rhi_evidence_attestation_validate_signed_json",
    492         Some("event_json"),
    493         String,
    494         2,
    495         4
    496     ),
    497     public_function!(
    498         "social_ask_build_authored_draft",
    499         Some("input_json"),
    500         String,
    501         2,
    502         4
    503     ),
    504     public_function!(
    505         "social_comment_build_authored_draft",
    506         Some("input_json"),
    507         String,
    508         2,
    509         4
    510     ),
    511     public_function!(
    512         "social_photo_update_build_authored_draft",
    513         Some("input_json"),
    514         String,
    515         2,
    516         4
    517     ),
    518     public_function!(
    519         "social_update_build_authored_draft",
    520         Some("input_json"),
    521         String,
    522         2,
    523         4
    524     ),
    525     public_function!("wiki_article_tags", Some("article_json"), String, 2, 4),
    526     public_function!(
    527         "wiki_merge_request_tags",
    528         Some("request_json"),
    529         String,
    530         2,
    531         4
    532     ),
    533     public_function!("wiki_redirect_tags", Some("redirect_json"), String, 2, 4),
    534     public_function!(
    535         "verify_and_decode_event_json",
    536         Some("event_json"),
    537         String,
    538         2,
    539         4
    540     ),
    541     public_function!("contract_manifest_json", None, String, 0, 4),
    542 ];
    543 
    544 const EVENT_CODEC_SUPPORT_EXPORTS: &[WasmSupportExport] = &[
    545     memory_export!(),
    546     table_export!("__wbindgen_externrefs"),
    547     number_function_export!("__wbindgen_malloc", 2),
    548     number_function_export!("__wbindgen_realloc", 4),
    549     void_function_export!("__externref_table_dealloc", 1),
    550     void_function_export!("__wbindgen_free", 3),
    551     void_function_export!("__wbindgen_start", 0),
    552 ];
    553 
    554 const REPLICA_STORE_FUNCTIONS: &[WasmPublicFunction] = &[
    555     public_function!("replica_store_export_begin", None, Any, 0, 3),
    556     public_function!("replica_store_export_finish", None, Void, 0, 2),
    557     public_function!("replica_store_export_json", None, Any, 0, 3),
    558     public_function!("replica_store_farm_create", Some("opts_json"), Any, 2, 3),
    559     public_function!("replica_store_farm_delete", Some("opts_json"), Any, 2, 3),
    560     public_function!("replica_store_farm_find_many", Some("opts_json"), Any, 2, 3),
    561     public_function!("replica_store_farm_find_one", Some("opts_json"), Any, 2, 3),
    562     public_function!("replica_store_farm_update", Some("opts_json"), Any, 2, 3),
    563     public_function!(
    564         "replica_store_farm_gcs_location_create",
    565         Some("opts_json"),
    566         Any,
    567         2,
    568         3
    569     ),
    570     public_function!(
    571         "replica_store_farm_gcs_location_delete",
    572         Some("opts_json"),
    573         Any,
    574         2,
    575         3
    576     ),
    577     public_function!(
    578         "replica_store_farm_gcs_location_find_many",
    579         Some("opts_json"),
    580         Any,
    581         2,
    582         3
    583     ),
    584     public_function!(
    585         "replica_store_farm_gcs_location_find_one",
    586         Some("opts_json"),
    587         Any,
    588         2,
    589         3
    590     ),
    591     public_function!(
    592         "replica_store_farm_gcs_location_update",
    593         Some("opts_json"),
    594         Any,
    595         2,
    596         3
    597     ),
    598     public_function!(
    599         "replica_store_farm_member_claim_create",
    600         Some("opts_json"),
    601         Any,
    602         2,
    603         3
    604     ),
    605     public_function!(
    606         "replica_store_farm_member_claim_delete",
    607         Some("opts_json"),
    608         Any,
    609         2,
    610         3
    611     ),
    612     public_function!(
    613         "replica_store_farm_member_claim_find_many",
    614         Some("opts_json"),
    615         Any,
    616         2,
    617         3
    618     ),
    619     public_function!(
    620         "replica_store_farm_member_claim_find_one",
    621         Some("opts_json"),
    622         Any,
    623         2,
    624         3
    625     ),
    626     public_function!(
    627         "replica_store_farm_member_claim_update",
    628         Some("opts_json"),
    629         Any,
    630         2,
    631         3
    632     ),
    633     public_function!(
    634         "replica_store_farm_member_create",
    635         Some("opts_json"),
    636         Any,
    637         2,
    638         3
    639     ),
    640     public_function!(
    641         "replica_store_farm_member_delete",
    642         Some("opts_json"),
    643         Any,
    644         2,
    645         3
    646     ),
    647     public_function!(
    648         "replica_store_farm_member_find_many",
    649         Some("opts_json"),
    650         Any,
    651         2,
    652         3
    653     ),
    654     public_function!(
    655         "replica_store_farm_member_find_one",
    656         Some("opts_json"),
    657         Any,
    658         2,
    659         3
    660     ),
    661     public_function!(
    662         "replica_store_farm_member_update",
    663         Some("opts_json"),
    664         Any,
    665         2,
    666         3
    667     ),
    668     public_function!(
    669         "replica_store_farm_tag_create",
    670         Some("opts_json"),
    671         Any,
    672         2,
    673         3
    674     ),
    675     public_function!(
    676         "replica_store_farm_tag_delete",
    677         Some("opts_json"),
    678         Any,
    679         2,
    680         3
    681     ),
    682     public_function!(
    683         "replica_store_farm_tag_find_many",
    684         Some("opts_json"),
    685         Any,
    686         2,
    687         3
    688     ),
    689     public_function!(
    690         "replica_store_farm_tag_find_one",
    691         Some("opts_json"),
    692         Any,
    693         2,
    694         3
    695     ),
    696     public_function!(
    697         "replica_store_farm_tag_update",
    698         Some("opts_json"),
    699         Any,
    700         2,
    701         3
    702     ),
    703     public_function!(
    704         "replica_store_gcs_location_create",
    705         Some("opts_json"),
    706         Any,
    707         2,
    708         3
    709     ),
    710     public_function!(
    711         "replica_store_gcs_location_delete",
    712         Some("opts_json"),
    713         Any,
    714         2,
    715         3
    716     ),
    717     public_function!(
    718         "replica_store_gcs_location_find_many",
    719         Some("opts_json"),
    720         Any,
    721         2,
    722         3
    723     ),
    724     public_function!(
    725         "replica_store_gcs_location_find_one",
    726         Some("opts_json"),
    727         Any,
    728         2,
    729         3
    730     ),
    731     public_function!(
    732         "replica_store_gcs_location_update",
    733         Some("opts_json"),
    734         Any,
    735         2,
    736         3
    737     ),
    738     public_function!("replica_store_import_json", Some("dump_json"), Void, 2, 2),
    739     public_function!(
    740         "replica_store_log_error_create",
    741         Some("opts_json"),
    742         Any,
    743         2,
    744         3
    745     ),
    746     public_function!(
    747         "replica_store_log_error_delete",
    748         Some("opts_json"),
    749         Any,
    750         2,
    751         3
    752     ),
    753     public_function!(
    754         "replica_store_log_error_find_many",
    755         Some("opts_json"),
    756         Any,
    757         2,
    758         3
    759     ),
    760     public_function!(
    761         "replica_store_log_error_find_one",
    762         Some("opts_json"),
    763         Any,
    764         2,
    765         3
    766     ),
    767     public_function!(
    768         "replica_store_log_error_update",
    769         Some("opts_json"),
    770         Any,
    771         2,
    772         3
    773     ),
    774     public_function!(
    775         "replica_store_media_image_create",
    776         Some("opts_json"),
    777         Any,
    778         2,
    779         3
    780     ),
    781     public_function!(
    782         "replica_store_media_image_delete",
    783         Some("opts_json"),
    784         Any,
    785         2,
    786         3
    787     ),
    788     public_function!(
    789         "replica_store_media_image_find_many",
    790         Some("opts_json"),
    791         Any,
    792         2,
    793         3
    794     ),
    795     public_function!(
    796         "replica_store_media_image_find_one",
    797         Some("opts_json"),
    798         Any,
    799         2,
    800         3
    801     ),
    802     public_function!(
    803         "replica_store_media_image_update",
    804         Some("opts_json"),
    805         Any,
    806         2,
    807         3
    808     ),
    809     public_function!(
    810         "replica_store_nostr_event_head_create",
    811         Some("opts_json"),
    812         Any,
    813         2,
    814         3
    815     ),
    816     public_function!(
    817         "replica_store_nostr_event_head_delete",
    818         Some("opts_json"),
    819         Any,
    820         2,
    821         3
    822     ),
    823     public_function!(
    824         "replica_store_nostr_event_head_find_many",
    825         Some("opts_json"),
    826         Any,
    827         2,
    828         3
    829     ),
    830     public_function!(
    831         "replica_store_nostr_event_head_find_one",
    832         Some("opts_json"),
    833         Any,
    834         2,
    835         3
    836     ),
    837     public_function!(
    838         "replica_store_nostr_event_head_update",
    839         Some("opts_json"),
    840         Any,
    841         2,
    842         3
    843     ),
    844     public_function!(
    845         "replica_store_nostr_profile_create",
    846         Some("opts_json"),
    847         Any,
    848         2,
    849         3
    850     ),
    851     public_function!(
    852         "replica_store_nostr_profile_delete",
    853         Some("opts_json"),
    854         Any,
    855         2,
    856         3
    857     ),
    858     public_function!(
    859         "replica_store_nostr_profile_find_many",
    860         Some("opts_json"),
    861         Any,
    862         2,
    863         3
    864     ),
    865     public_function!(
    866         "replica_store_nostr_profile_find_one",
    867         Some("opts_json"),
    868         Any,
    869         2,
    870         3
    871     ),
    872     public_function!(
    873         "replica_store_nostr_profile_relay_set",
    874         Some("opts_json"),
    875         Any,
    876         2,
    877         3
    878     ),
    879     public_function!(
    880         "replica_store_nostr_profile_relay_unset",
    881         Some("opts_json"),
    882         Any,
    883         2,
    884         3
    885     ),
    886     public_function!(
    887         "replica_store_nostr_profile_update",
    888         Some("opts_json"),
    889         Any,
    890         2,
    891         3
    892     ),
    893     public_function!(
    894         "replica_store_nostr_relay_create",
    895         Some("opts_json"),
    896         Any,
    897         2,
    898         3
    899     ),
    900     public_function!(
    901         "replica_store_nostr_relay_delete",
    902         Some("opts_json"),
    903         Any,
    904         2,
    905         3
    906     ),
    907     public_function!(
    908         "replica_store_nostr_relay_find_many",
    909         Some("opts_json"),
    910         Any,
    911         2,
    912         3
    913     ),
    914     public_function!(
    915         "replica_store_nostr_relay_find_one",
    916         Some("opts_json"),
    917         Any,
    918         2,
    919         3
    920     ),
    921     public_function!(
    922         "replica_store_nostr_relay_update",
    923         Some("opts_json"),
    924         Any,
    925         2,
    926         3
    927     ),
    928     public_function!("replica_store_plot_create", Some("opts_json"), Any, 2, 3),
    929     public_function!("replica_store_plot_delete", Some("opts_json"), Any, 2, 3),
    930     public_function!("replica_store_plot_find_many", Some("opts_json"), Any, 2, 3),
    931     public_function!("replica_store_plot_find_one", Some("opts_json"), Any, 2, 3),
    932     public_function!("replica_store_plot_update", Some("opts_json"), Any, 2, 3),
    933     public_function!(
    934         "replica_store_plot_gcs_location_create",
    935         Some("opts_json"),
    936         Any,
    937         2,
    938         3
    939     ),
    940     public_function!(
    941         "replica_store_plot_gcs_location_delete",
    942         Some("opts_json"),
    943         Any,
    944         2,
    945         3
    946     ),
    947     public_function!(
    948         "replica_store_plot_gcs_location_find_many",
    949         Some("opts_json"),
    950         Any,
    951         2,
    952         3
    953     ),
    954     public_function!(
    955         "replica_store_plot_gcs_location_find_one",
    956         Some("opts_json"),
    957         Any,
    958         2,
    959         3
    960     ),
    961     public_function!(
    962         "replica_store_plot_gcs_location_update",
    963         Some("opts_json"),
    964         Any,
    965         2,
    966         3
    967     ),
    968     public_function!(
    969         "replica_store_plot_tag_create",
    970         Some("opts_json"),
    971         Any,
    972         2,
    973         3
    974     ),
    975     public_function!(
    976         "replica_store_plot_tag_delete",
    977         Some("opts_json"),
    978         Any,
    979         2,
    980         3
    981     ),
    982     public_function!(
    983         "replica_store_plot_tag_find_many",
    984         Some("opts_json"),
    985         Any,
    986         2,
    987         3
    988     ),
    989     public_function!(
    990         "replica_store_plot_tag_find_one",
    991         Some("opts_json"),
    992         Any,
    993         2,
    994         3
    995     ),
    996     public_function!(
    997         "replica_store_plot_tag_update",
    998         Some("opts_json"),
    999         Any,
   1000         2,
   1001         3
   1002     ),
   1003     public_function!("replica_store_reset_database", None, Void, 0, 2),
   1004     public_function!("replica_store_run_migrations", None, Void, 0, 2),
   1005     public_function!(
   1006         "replica_store_trade_product_create",
   1007         Some("opts_json"),
   1008         Any,
   1009         2,
   1010         3
   1011     ),
   1012     public_function!(
   1013         "replica_store_trade_product_delete",
   1014         Some("opts_json"),
   1015         Any,
   1016         2,
   1017         3
   1018     ),
   1019     public_function!(
   1020         "replica_store_trade_product_find_many",
   1021         Some("opts_json"),
   1022         Any,
   1023         2,
   1024         3
   1025     ),
   1026     public_function!(
   1027         "replica_store_trade_product_find_one",
   1028         Some("opts_json"),
   1029         Any,
   1030         2,
   1031         3
   1032     ),
   1033     public_function!(
   1034         "replica_store_trade_product_location_set",
   1035         Some("opts_json"),
   1036         Any,
   1037         2,
   1038         3
   1039     ),
   1040     public_function!(
   1041         "replica_store_trade_product_location_unset",
   1042         Some("opts_json"),
   1043         Any,
   1044         2,
   1045         3
   1046     ),
   1047     public_function!(
   1048         "replica_store_trade_product_media_set",
   1049         Some("opts_json"),
   1050         Any,
   1051         2,
   1052         3
   1053     ),
   1054     public_function!(
   1055         "replica_store_trade_product_media_unset",
   1056         Some("opts_json"),
   1057         Any,
   1058         2,
   1059         3
   1060     ),
   1061     public_function!(
   1062         "replica_store_trade_product_update",
   1063         Some("opts_json"),
   1064         Any,
   1065         2,
   1066         3
   1067     ),
   1068 ];
   1069 
   1070 const REPLICA_SYNC_FUNCTIONS: &[WasmPublicFunction] = &[
   1071     public_function!("replica_sync_ingest_event", Some("event_json"), Any, 2, 3),
   1072     public_function!("replica_sync_sync_all", Some("request_json"), Any, 2, 3),
   1073 ];
   1074 
   1075 const SQL_WASM_SUPPORT_EXPORTS: &[WasmSupportExport] = &[
   1076     memory_export!(),
   1077     number_function_export!("__wbindgen_malloc", 2),
   1078     number_function_export!("__wbindgen_realloc", 4),
   1079     void_function_export!("__wbindgen_exn_store", 1),
   1080     number_function_export!("__externref_table_alloc", 0),
   1081     table_export!("__wbindgen_externrefs"),
   1082     void_function_export!("__externref_table_dealloc", 1),
   1083     void_function_export!("__wbindgen_start", 0),
   1084 ];
   1085 
   1086 #[cfg(test)]
   1087 mod tests {
   1088     use crate::sdk_generation::package_matrix::wasm_package_specs;
   1089 
   1090     use super::{WASM_DECLARATION_HEADER, declaration_files};
   1091 
   1092     #[test]
   1093     fn declaration_files_are_dto_owned_and_cover_primary_and_sidecar_paths() {
   1094         for spec in wasm_package_specs() {
   1095             let files = declaration_files(*spec).expect("declaration files");
   1096             assert_eq!(files.len(), 2);
   1097             assert!(
   1098                 files
   1099                     .iter()
   1100                     .any(|file| file.relative_path == format!("dist/{}.d.ts", spec.out_name))
   1101             );
   1102             assert!(
   1103                 files.iter().any(
   1104                     |file| file.relative_path == format!("dist/{}_bg.wasm.d.ts", spec.out_name)
   1105                 )
   1106             );
   1107             for file in files {
   1108                 assert!(file.contents.starts_with(WASM_DECLARATION_HEADER));
   1109                 assert!(!file.contents.contains("tslint:disable"));
   1110                 assert!(!file.contents.contains("eslint-disable"));
   1111             }
   1112         }
   1113     }
   1114 
   1115     #[test]
   1116     fn generated_declarations_include_public_wasm_entrypoints() {
   1117         let specs = wasm_package_specs();
   1118         let events = declaration_files(specs[0]).expect("event declarations");
   1119         assert!(
   1120             events[0]
   1121                 .contents
   1122                 .contains("export function operational_listing_tags")
   1123         );
   1124         assert!(
   1125             events[1]
   1126                 .contents
   1127                 .contains("export declare const operational_listing_tags")
   1128         );
   1129         for authored in [
   1130             "social_ask_build_authored_draft",
   1131             "social_comment_build_authored_draft",
   1132             "social_photo_update_build_authored_draft",
   1133             "social_update_build_authored_draft",
   1134         ] {
   1135             assert!(
   1136                 events[0]
   1137                     .contents
   1138                     .contains(&format!("export function {authored}"))
   1139             );
   1140             assert!(
   1141                 events[1]
   1142                     .contents
   1143                     .contains(&format!("export declare const {authored}"))
   1144             );
   1145         }
   1146         for evidence in [
   1147             "trade_evidence_manifest_parse_json",
   1148             "rhi_evidence_report_parse_json",
   1149             "rhi_evidence_attestation_build_draft",
   1150             "rhi_evidence_attestation_validate_signed_json",
   1151         ] {
   1152             assert!(
   1153                 events[0]
   1154                     .contents
   1155                     .contains(&format!("export function {evidence}"))
   1156             );
   1157             assert!(
   1158                 events[1]
   1159                     .contents
   1160                     .contains(&format!("export declare const {evidence}"))
   1161             );
   1162         }
   1163         assert!(events[0].contents.contains("created_at_unix_s: bigint"));
   1164         assert!(events[1].contents.contains("i64_arg0: bigint"));
   1165         for retired in [
   1166             "calendar_date_event_tags",
   1167             "calendar_event_rsvp_tags",
   1168             "calendar_tags",
   1169             "calendar_time_event_tags",
   1170         ] {
   1171             assert!(!events[0].contents.contains(retired));
   1172             assert!(!events[1].contents.contains(retired));
   1173         }
   1174         for retired in ["comment_tags", "post_tags"] {
   1175             assert!(
   1176                 !events[0]
   1177                     .contents
   1178                     .contains(&format!("export function {retired}("))
   1179             );
   1180             assert!(
   1181                 !events[1]
   1182                     .contents
   1183                     .contains(&format!("export declare const {retired}:"))
   1184             );
   1185         }
   1186 
   1187         let replica_store = declaration_files(specs[1]).expect("replica store declarations");
   1188         assert!(
   1189             replica_store[0]
   1190                 .contents
   1191                 .contains("export function replica_store_farm_create")
   1192         );
   1193         assert!(
   1194             replica_store[0]
   1195                 .contents
   1196                 .contains("export function replica_store_farm_update")
   1197         );
   1198         assert!(
   1199             replica_store[0]
   1200                 .contents
   1201                 .contains("export function replica_store_plot_update")
   1202         );
   1203         assert!(
   1204             replica_store[1]
   1205                 .contents
   1206                 .contains("export declare const replica_store_farm_create")
   1207         );
   1208         assert!(
   1209             replica_store[1]
   1210                 .contents
   1211                 .contains("export declare const replica_store_farm_update")
   1212         );
   1213         assert!(
   1214             replica_store[1]
   1215                 .contents
   1216                 .contains("export declare const replica_store_plot_update")
   1217         );
   1218 
   1219         let replica_sync = declaration_files(specs[2]).expect("replica sync declarations");
   1220         assert!(
   1221             replica_sync[0]
   1222                 .contents
   1223                 .contains("export function replica_sync_sync_all")
   1224         );
   1225         assert!(
   1226             replica_sync[1]
   1227                 .contents
   1228                 .contains("export declare const replica_sync_sync_all")
   1229         );
   1230     }
   1231 }