sui_types/
display.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::SUI_FRAMEWORK_ADDRESS;
5use crate::collection_types::VecMap;
6use crate::event::Event;
7use crate::id::{ID, UID};
8use move_core_types::ident_str;
9use move_core_types::identifier::IdentStr;
10use move_core_types::language_storage::StructTag;
11use serde::Deserialize;
12
13pub const DISPLAY_MODULE_NAME: &IdentStr = ident_str!("display");
14pub const DISPLAY_CREATED_EVENT_NAME: &IdentStr = ident_str!("DisplayCreated");
15pub const DISPLAY_VERSION_UPDATED_EVENT_NAME: &IdentStr = ident_str!("VersionUpdated");
16
17// TODO: add tests to keep in sync
18/// Rust version of the Move sui::display::Display type
19#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
20pub struct DisplayObject {
21    pub id: UID,
22    pub fields: VecMap<String, String>,
23    pub version: u16,
24}
25
26#[derive(Deserialize, Debug)]
27/// The event that is emitted when a `Display` version is "released".
28/// Serves for Display versioning.
29pub struct DisplayVersionUpdatedEvent {
30    pub id: ID,
31    pub version: u16,
32    pub fields: VecMap<String, String>,
33}
34
35impl DisplayVersionUpdatedEvent {
36    pub fn type_(inner: &StructTag) -> StructTag {
37        StructTag {
38            address: SUI_FRAMEWORK_ADDRESS,
39            name: DISPLAY_VERSION_UPDATED_EVENT_NAME.to_owned(),
40            module: DISPLAY_MODULE_NAME.to_owned(),
41            type_params: vec![inner.clone().into()],
42        }
43    }
44
45    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T>
46    pub fn is_display_updated_event(inner: &StructTag) -> bool {
47        inner.address == SUI_FRAMEWORK_ADDRESS
48            && inner.module.as_ident_str() == DISPLAY_MODULE_NAME
49            && inner.name.as_ident_str() == DISPLAY_VERSION_UPDATED_EVENT_NAME
50    }
51
52    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T> and returns a reference
53    // to the inner type T if so.
54    pub fn inner_type(inner: &StructTag) -> Option<&StructTag> {
55        use move_core_types::language_storage::TypeTag;
56
57        if !Self::is_display_updated_event(inner) {
58            return None;
59        }
60
61        match &inner.type_params[..] {
62            [TypeTag::Struct(struct_type)] => Some(struct_type),
63            _ => None,
64        }
65    }
66
67    pub fn try_from_event(event: &Event) -> Option<(&StructTag, Self)> {
68        let inner_type = Self::inner_type(&event.type_)?;
69
70        bcs::from_bytes(&event.contents)
71            .ok()
72            .map(|event| (inner_type, event))
73    }
74}
75
76#[derive(Deserialize, Debug)]
77pub struct DisplayCreatedEvent {
78    // The Object ID of Display Object
79    pub id: ID,
80}
81
82impl DisplayCreatedEvent {
83    pub fn type_(inner: &StructTag) -> StructTag {
84        StructTag {
85            address: SUI_FRAMEWORK_ADDRESS,
86            name: DISPLAY_CREATED_EVENT_NAME.to_owned(),
87            module: DISPLAY_MODULE_NAME.to_owned(),
88            type_params: vec![inner.clone().into()],
89        }
90    }
91}