1use iota_sdk_types::{StructTag, TypeTag};
6use serde::{Deserialize, Serialize};
7
8use crate::{MoveTypeTagTrait, base_types::EpochId, id::UID};
9
10#[derive(Debug, Serialize, Deserialize)]
12pub struct Config {
13 pub id: UID,
14}
15
16#[derive(Debug, Serialize, Deserialize)]
18pub struct Setting<V> {
19 pub data: Option<SettingData<V>>,
20}
21
22#[derive(Debug, Serialize, Deserialize)]
24pub struct SettingData<V> {
25 pub newer_value_epoch: u64,
26 pub newer_value: Option<V>,
27 pub older_value_opt: Option<V>,
28}
29
30pub fn setting_type(value_tag: TypeTag) -> StructTag {
31 StructTag::new_config_setting(value_tag)
32}
33
34impl<V: MoveTypeTagTrait> MoveTypeTagTrait for Setting<V> {
35 fn get_type_tag() -> TypeTag {
36 TypeTag::Struct(Box::new(setting_type(V::get_type_tag())))
37 }
38}
39
40impl<V> Setting<V> {
41 pub fn read_value(&self, cur_epoch: Option<EpochId>) -> Option<&V> {
45 self.data.as_ref()?.read_value(cur_epoch)
46 }
47}
48
49impl<V> SettingData<V> {
50 pub fn read_value(&self, cur_epoch: Option<EpochId>) -> Option<&V> {
55 let use_newer_value = match cur_epoch {
56 Some(cur_epoch) => cur_epoch > self.newer_value_epoch,
57 None => true,
58 };
59 if use_newer_value {
60 self.newer_value.as_ref()
61 } else {
62 self.older_value_opt.as_ref()
63 }
64 }
65}