Skip to main content

datex_core/value_updates/
update_handler.rs

1use crate::{
2    prelude::*,
3    shared_values::base_shared_value_container::observers::TransceiverId,
4    value_updates::{
5        UpdateReturn,
6        errors::UpdateError,
7        update_data::{
8            AppendEntryUpdateData, DeleteEntryUpdateData, ListSpliceUpdateData,
9            ReplaceUpdateData, SetEntryUpdateData, Update, UpdateData,
10        },
11    },
12    values::value_container::ValueContainer,
13};
14
15pub type UpdateResult = Result<UpdateReturn, UpdateError>;
16
17/// Converts a Result with any types that can be converted into UpdateReturn and UpdateError into an UpdateResult.
18pub fn into_update_result<T: Into<UpdateReturn>, E: Into<UpdateError>>(
19    result: Result<T, E>,
20) -> UpdateResult {
21    match result {
22        Ok(value) => Ok(value.into()),
23        Err(err) => Err(err.into()),
24    }
25}
26
27pub trait UpdateHandler {
28    fn update(&mut self, update: Update) -> UpdateResult {
29        match update.data {
30            UpdateData::AppendEntry(data) => into_update_result(
31                self.try_append_entry(data, update.source_id),
32            ),
33            UpdateData::Clear => {
34                into_update_result(self.try_clear(update.source_id))
35            }
36            UpdateData::Replace(data) => {
37                into_update_result(self.try_replace(data, update.source_id))
38            }
39            UpdateData::SetEntry(data) => {
40                into_update_result(self.try_set_entry(data, update.source_id))
41            }
42            UpdateData::DeleteEntry(data) => into_update_result(
43                self.try_delete_entry(data, update.source_id),
44            ),
45            UpdateData::ListSplice(data) => {
46                into_update_result(self.try_list_splice(data, update.source_id))
47            }
48        }
49    }
50
51    fn try_replace(
52        &mut self,
53        _data: ReplaceUpdateData,
54        _source_id: TransceiverId,
55    ) -> Result<ValueContainer, UpdateError> {
56        unimplemented!()
57    }
58    fn try_set_entry(
59        &mut self,
60        _data: SetEntryUpdateData,
61        _source_id: TransceiverId,
62    ) -> Result<(), UpdateError> {
63        unimplemented!()
64    }
65    fn try_delete_entry(
66        &mut self,
67        _data: DeleteEntryUpdateData,
68        _source_id: TransceiverId,
69    ) -> Result<ValueContainer, UpdateError> {
70        unimplemented!()
71    }
72    fn try_append_entry(
73        &mut self,
74        _data: AppendEntryUpdateData,
75        _source_id: TransceiverId,
76    ) -> Result<(), UpdateError> {
77        unimplemented!()
78    }
79    fn try_clear(
80        &mut self,
81        _source_id: TransceiverId,
82    ) -> Result<(), UpdateError> {
83        unimplemented!()
84    }
85    fn try_list_splice(
86        &mut self,
87        _data: ListSpliceUpdateData,
88        _source_id: TransceiverId,
89    ) -> Result<Vec<ValueContainer>, UpdateError> {
90        unimplemented!()
91    }
92}