rust_wechat_core/
lib.rs

1//! 核心公共模块
2//! 包含API请求封装、认证鉴权、错误处理等公共功能
3
4pub mod client;
5pub mod error;
6pub mod utils;
7pub use error::*;
8
9#[macro_export]
10#[doc(hidden)]
11macro_rules! __setter {
12    ($field:ident: Option<$ty:ty>) => {
13        #[inline(always)]
14        pub fn $field<T: Into<$ty>>(mut self, value: T) -> Self {
15            self.$field = Some(value.into());
16            self
17        }
18    };
19    (children: Vec<$ty:ty>) => {
20        #[inline(always)]
21        pub fn add_child<T: Into<$ty>>(mut self, value: T) -> Self {
22            self.children.push(value.into());
23            self
24        }
25        #[inline(always)]
26        pub fn children<T: Into<Vec<$ty>>>(mut self, value: T) -> Self {
27            self.children = value.into();
28            self
29        }
30    };
31    ($field:ident: $ty:ty) => {
32        #[inline(always)]
33        pub fn $field<T: Into<$ty>>(mut self, value: T) -> Self {
34            self.$field = value.into();
35            self
36        }
37    };
38}
39
40
41#[macro_export]
42#[doc(hidden)]
43macro_rules! __string_enum {
44    ($name:ident { $($variant:ident = $value:literal $(| $value2:literal)*, )* }) => {
45        impl std::fmt::Display for $name {
46            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47                match *self {
48                    $( $name::$variant => write!(f, $value), )*
49                }
50            }
51        }
52
53        impl std::str::FromStr for $name {
54            type Err = String;
55
56            fn from_str(s: &str) -> Result<Self, Self::Err> {
57                match s {
58                    $($value $(| $value2)* => Ok($name::$variant),)*
59                    s => Err(format!(
60                        "Unkown Value. Found `{}`, Expected `{}`",
61                        s,
62                        stringify!($($value,)*)
63                    ))
64                }
65            }
66        }
67    }
68}
69
70pub type Result<T> = std::result::Result<T, WechatError>;