sui_analytics_indexer/
schema.rs1use std::borrow::Cow;
11use std::sync::Arc;
12
13use sui_types::dynamic_field::DynamicFieldType;
14use thiserror::Error;
15
16use crate::tables::InputObjectKind;
17use crate::tables::ObjectStatus;
18use crate::tables::OwnerType;
19
20#[derive(Debug, Error)]
22pub enum ColumnError {
23 #[error("invalid column index {0}")]
24 InvalidIndex(usize),
25}
26
27pub enum ColumnValue<'a> {
33 U64(u64),
34 Str(Cow<'a, str>),
35 Bool(bool),
36 I64(i64),
37 OptionU64(Option<u64>),
38 OptionStr(Option<Cow<'a, str>>),
39}
40
41impl<'a> From<&'a u64> for ColumnValue<'a> {
43 fn from(value: &'a u64) -> Self {
44 Self::U64(*value)
45 }
46}
47
48impl<'a> From<&'a i64> for ColumnValue<'a> {
49 fn from(value: &'a i64) -> Self {
50 Self::I64(*value)
51 }
52}
53
54impl<'a> From<&'a bool> for ColumnValue<'a> {
55 fn from(value: &'a bool) -> Self {
56 Self::Bool(*value)
57 }
58}
59
60impl<'a> From<&'a Option<u64>> for ColumnValue<'a> {
61 fn from(value: &'a Option<u64>) -> Self {
62 Self::OptionU64(*value)
63 }
64}
65
66impl<'a> From<&'a String> for ColumnValue<'a> {
68 fn from(value: &'a String) -> Self {
69 Self::Str(Cow::Borrowed(value.as_str()))
70 }
71}
72
73impl<'a> From<&'a Option<String>> for ColumnValue<'a> {
74 fn from(value: &'a Option<String>) -> Self {
75 Self::OptionStr(value.as_ref().map(|s| Cow::Borrowed(s.as_str())))
76 }
77}
78
79impl<'a> From<&'a OwnerType> for ColumnValue<'a> {
81 fn from(value: &'a OwnerType) -> Self {
82 Self::Str(Cow::Owned(value.to_string()))
83 }
84}
85
86impl<'a> From<&'a Option<OwnerType>> for ColumnValue<'a> {
87 fn from(value: &'a Option<OwnerType>) -> Self {
88 Self::OptionStr(value.as_ref().map(|v| Cow::Owned(v.to_string())))
89 }
90}
91
92impl<'a> From<&'a ObjectStatus> for ColumnValue<'a> {
93 fn from(value: &'a ObjectStatus) -> Self {
94 Self::Str(Cow::Owned(value.to_string()))
95 }
96}
97
98impl<'a> From<&'a Option<ObjectStatus>> for ColumnValue<'a> {
99 fn from(value: &'a Option<ObjectStatus>) -> Self {
100 Self::OptionStr(value.as_ref().map(|v| Cow::Owned(v.to_string())))
101 }
102}
103
104impl<'a> From<&'a Option<InputObjectKind>> for ColumnValue<'a> {
105 fn from(value: &'a Option<InputObjectKind>) -> Self {
106 Self::OptionStr(value.as_ref().map(|v| Cow::Owned(v.to_string())))
107 }
108}
109
110impl<'a> From<&'a DynamicFieldType> for ColumnValue<'a> {
111 fn from(value: &'a DynamicFieldType) -> Self {
112 Self::Str(Cow::Owned(value.to_string()))
113 }
114}
115
116impl<'a> From<&'a Option<DynamicFieldType>> for ColumnValue<'a> {
117 fn from(value: &'a Option<DynamicFieldType>) -> Self {
118 Self::OptionStr(value.as_ref().map(|v| Cow::Owned(v.to_string())))
119 }
120}
121
122pub trait RowSchema {
130 fn schema() -> &'static [&'static str]
135 where
136 Self: Sized;
137
138 fn column_count(&self) -> usize;
142
143 fn get_column(&self, idx: usize) -> Result<ColumnValue<'_>, ColumnError>;
147}
148
149impl<T: RowSchema> RowSchema for Arc<T> {
152 fn schema() -> &'static [&'static str]
153 where
154 Self: Sized,
155 {
156 T::schema()
157 }
158
159 fn column_count(&self) -> usize {
160 (**self).column_count()
161 }
162
163 fn get_column(&self, idx: usize) -> Result<ColumnValue<'_>, ColumnError> {
164 (**self).get_column(idx)
165 }
166}