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
macro_rules! declare_client {
($(
{
feature: $feature:literal,
field: $field:ident,
ty: $ty:ty,
doc: $doc:literal,
init: |$core_config:ident, $base_core_config:ident| $init:block $(,)?
}
),+ $(,)?) => {
/// 🚀 OpenLark客户端 - 极简设计
///
/// # 特性
/// - 零配置启动:`Client::from_env()`
/// - 单入口:meta 链式字段访问(`client.docs/...`)
/// - 编译时feature优化
/// - 高性能异步
/// - 现代化错误处理
///
/// # 示例
/// ```rust,no_run
/// use openlark_client::prelude::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // 从环境变量创建客户端
/// let client = Client::from_env()?;
///
/// // meta 链式入口(需要对应 feature)
/// // - 通讯:client.communication.im...
/// // - 文档:client.docs.ccm...
/// // - 认证:client.auth.app / client.auth.user / client.auth.oauth
///
/// Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct Client {
/// 服务注册表
registry: std::sync::Arc<crate::DefaultServiceRegistry>,
/// 统一配置(Arc 零拷贝共享,供所有 meta client 复用)
config: openlark_core::config::Config,
$(
#[doc = $doc]
#[cfg(feature = $feature)]
pub $field: $ty,
)*
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client")
.field("config", &"<CoreConfig>")
.field("registry", &"<Registry>")
$(
.field(stringify!($field), &cfg!(feature = $feature))
)*
.finish()
}
}
impl Client {
fn from_parts(
registry: std::sync::Arc<crate::DefaultServiceRegistry>,
_base_core_config: openlark_core::config::Config,
core_config: openlark_core::config::Config,
) -> crate::Result<Self> {
$(
#[cfg(feature = $feature)]
let $field: $ty = (|
$core_config: openlark_core::config::Config,
$base_core_config: openlark_core::config::Config,
| -> crate::Result<$ty> { Ok($init) })(
core_config.clone(),
_base_core_config.clone(),
)?;
)*
Ok(Self {
config: core_config.clone(),
registry,
$(
#[cfg(feature = $feature)]
$field,
)*
})
}
}
};
}