1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
//! 远程对象选择器。
//!
//! 选择器是 awr 用于操作远程对象的核心机制。
//!
//! 在 awr 中,好友信息、群信息等抽象为远程对象,而 [`Friend`] [`Group`] 等对象是远程对象的本地副本。
//! 选择器是指向远程对象的指针,可以用于下载本地副本,或者在不下载远程对象的情况下对远程对象进行操作。
//!
//! 以好友为例,下图展示了 awr 中的好友对象的选择器机制。
//!
//! ```mermaid
//! graph LR
//! Client -->|"friend(...)"| FriendSelector
//! Client -->|"async get_friend(...)"| Friend
//! FriendSelector -->|"async fetch(...)"| Friend
//! Friend -.->|方法委托| FriendSelector
//! FriendSelector -.->|"API 调用"| S([服务器])
//! S -.->|数据| Friend
//! ```
//!
//! [`Friend`]: crate::client::friend::Friend
//! [`Group`]: crate::client::group::Group
use std::sync::Arc;
use std::{collections::HashMap, hash::Hash};
use crate::Client;
use async_trait::async_trait;
/// 远程对象选择器。
///
/// # Python
/// ```python
/// class Selector(Protocol, Generic[Target]): ...
/// ```
#[async_trait]
pub trait Selector: Clone + Sync + Send {
/// 选择的对象类型。
type Target;
/// 错误类型。
type Error;
/// 刷新缓存。
///
/// # Python
/// ```python
/// async def flush(self) -> Self:
/// ```
async fn flush(&self) -> &Self;
/// 获取客户端引用。
///
/// # Python
/// ```python
/// def as_client(self) -> Client: ...
/// ```
fn as_client(&self) -> &Arc<Client>;
/// 获取选择器。
///
/// # Python
/// ```python
/// def as_selector(self) -> Selector: ...
/// ```
fn as_selector(&self) -> &Self {
self
}
}
/// 单对象选择器。
///
/// # Python
/// ```python
/// class SingleSelector(Selector[Target]): ...
/// ```
#[async_trait]
pub trait SingleSelector: Selector {
/// 获取远程对象。
///
/// # Python
/// ```python
/// async def fetch(self) -> Target | None:
/// ```
async fn fetch(&self) -> Result<Self::Target, Self::Error>;
/// 刷新缓存并获取远程对象。
///
/// # Python
/// ```python
/// async def flush_and_fetch(self) -> Target | None:
/// ```
async fn flush_and_fetch(&self) -> Result<Self::Target, Self::Error> {
self.flush().await.fetch().await
}
}
/// 可空对象选择器。
///
/// # Python
/// ```python
/// class OptionSelector(Selector[Target]): ...
/// ```
#[async_trait]
pub trait OptionSelector: Selector {
/// 获取远程对象。
///
/// # Python
/// ```python
/// async def fetch(self) -> Target | None:
/// ```
async fn fetch(&self) -> Result<Option<Self::Target>, Self::Error>;
/// 刷新缓存并获取远程对象。
///
/// # Python
/// ```python
/// async def flush_and_fetch(self) -> Target | None:
/// ```
async fn flush_and_fetch(&self) -> Result<Option<Self::Target>, Self::Error> {
self.flush().await.fetch().await
}
}
/// 多个远程对象选择器。
///
/// # Python
/// ```python
/// class MultiSelector(Protocol, Generic[Key, Target]): ...
/// ```
#[async_trait]
pub trait MultiSelector: Selector {
/// 远程对象键类型。
type Key: Clone + Hash + Eq;
/// 获取远程对象。
///
/// # Python
/// ```python
/// async def fetch(self) -> dict[Key, Target]:
/// ```
async fn fetch(&self) -> Result<HashMap<Self::Key, Self::Target>, Self::Error>;
/// 刷新缓存并获取远程对象。
///
/// # Python
/// ```python
/// async def flush_and_fetch(self) -> dict[Key, Target]:
/// ```
async fn flush_and_fetch(&self) -> Result<HashMap<Self::Key, Self::Target>, Self::Error> {
self.flush().await;
self.fetch().await
}
}