radix_common/data/manifest/
custom_extension.rs

1use crate::internal_prelude::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Copy)]
4pub enum ManifestCustomExtension {}
5
6impl CustomExtension for ManifestCustomExtension {
7    const PAYLOAD_PREFIX: u8 = MANIFEST_SBOR_V1_PAYLOAD_PREFIX;
8
9    type CustomValueKind = ManifestCustomValueKind;
10    type CustomTraversal = ManifestCustomTraversal;
11    // NOTE: ManifestSbor is actually validated against Scrypto schemas
12    type CustomSchema = ScryptoCustomSchema;
13
14    fn custom_value_kind_matches_type_kind(
15        schema: &Schema<Self::CustomSchema>,
16        custom_value_kind: Self::CustomValueKind,
17        type_kind: &LocalTypeKind<Self::CustomSchema>,
18    ) -> bool {
19        match custom_value_kind {
20            ManifestCustomValueKind::Address => matches!(
21                type_kind,
22                TypeKind::Custom(ScryptoCustomTypeKind::Reference)
23            ),
24            ManifestCustomValueKind::Bucket => {
25                matches!(type_kind, TypeKind::Custom(ScryptoCustomTypeKind::Own))
26            }
27            ManifestCustomValueKind::Proof => {
28                matches!(type_kind, TypeKind::Custom(ScryptoCustomTypeKind::Own))
29            }
30            ManifestCustomValueKind::AddressReservation => {
31                matches!(type_kind, TypeKind::Custom(ScryptoCustomTypeKind::Own))
32            }
33            // An Expression can only be a Vec<Proof> or Vec<Manifest> at the moment
34            // - in other words they're both a Vec<Own> at the TypeKind level
35            ManifestCustomValueKind::Expression => matches!(
36                type_kind,
37                TypeKind::Array { element_type }
38                    if match schema.resolve_type_kind(*element_type) {
39                        Some(TypeKind::Custom(ScryptoCustomTypeKind::Own)) => true,
40                        _ => false,
41                    }
42            ),
43            ManifestCustomValueKind::Blob => matches!(
44                type_kind,
45                TypeKind::Array { element_type }
46                    if match schema.resolve_type_kind(*element_type) {
47                        Some(TypeKind::U8) => true,
48                        _ => false,
49                    }
50            ),
51            ManifestCustomValueKind::Decimal => {
52                matches!(type_kind, TypeKind::Custom(ScryptoCustomTypeKind::Decimal))
53            }
54            ManifestCustomValueKind::PreciseDecimal => matches!(
55                type_kind,
56                TypeKind::Custom(ScryptoCustomTypeKind::PreciseDecimal)
57            ),
58            ManifestCustomValueKind::NonFungibleLocalId => matches!(
59                type_kind,
60                TypeKind::Custom(ScryptoCustomTypeKind::NonFungibleLocalId)
61            ),
62        }
63    }
64
65    fn custom_type_kind_matches_non_custom_value_kind(
66        _: &Schema<Self::CustomSchema>,
67        _: &<Self::CustomSchema as CustomSchema>::CustomLocalTypeKind,
68        _: ValueKind<Self::CustomValueKind>,
69    ) -> bool {
70        // No custom type kinds can match non-custom value kinds
71        false
72    }
73}