iota_indexer/models/
display.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use diesel::prelude::*;
6use iota_types::display::DisplayVersionUpdatedEvent;
7
8use crate::schema::display;
9
10#[derive(Queryable, Insertable, Selectable, Debug, Clone)]
11#[diesel(table_name = display)]
12pub struct StoredDisplay {
13    pub object_type: String,
14    pub id: Vec<u8>,
15    pub version: i16,
16    pub bcs: Vec<u8>,
17}
18
19impl StoredDisplay {
20    pub fn try_from_event(event: &iota_types::event::Event) -> Option<Self> {
21        let (ty, display_event) = DisplayVersionUpdatedEvent::try_from_event(event)?;
22
23        Some(Self {
24            object_type: ty.to_canonical_string(/* with_prefix */ true),
25            id: display_event.id.bytes.to_vec(),
26            version: display_event.version as i16,
27            bcs: event.contents.clone(),
28        })
29    }
30
31    pub fn to_display_update_event(&self) -> Result<DisplayVersionUpdatedEvent, bcs::Error> {
32        bcs::from_bytes(&self.bcs)
33    }
34}