pingap_config/storage.rs
1// Copyright 2024-2025 Tree xie.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{Error, Observer};
16use async_trait::async_trait;
17
18type Result<T, E = Error> = std::result::Result<T, E>;
19
20#[derive(Debug, Clone)]
21pub struct History {
22 pub data: String,
23 pub created_at: u64,
24}
25
26#[async_trait]
27pub trait Storage: Send + Sync {
28 /// fetch from storage
29 async fn fetch(&self, key: &str) -> Result<String>;
30
31 /// save to storage
32 async fn save(&self, key: &str, value: &str) -> Result<()>;
33
34 /// delete from storage
35 async fn delete(&self, key: &str) -> Result<()>;
36 fn support_observer(&self) -> bool {
37 false
38 }
39 fn support_history(&self) -> bool {
40 false
41 }
42 async fn fetch_history(&self, _key: &str) -> Result<Option<Vec<History>>> {
43 Ok(None)
44 }
45 /// Sets up a watch on the config path to observe changes
46 /// Note: May miss changes if processing takes too long between updates
47 /// Should be used with periodic full fetches to ensure consistency
48 async fn observe(&self) -> Result<Observer> {
49 Ok(Observer {
50 etcd_watch_stream: None,
51 })
52 }
53}