Skip to content

Commit

Permalink
Merge pull request #86 from H1rono/fix-macro-scope
Browse files Browse the repository at this point in the history
Fix macro scope
  • Loading branch information
H1rono authored Sep 25, 2023
2 parents a4283a7 + 86ed621 commit 8fdd84d
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! [examples](https://github.com/H1rono/traq-bot-http-rs/blob/main/examples)
mod events;
#[macro_use]
pub(crate) mod macros;
mod parser;
pub mod payloads;
Expand Down
25 changes: 13 additions & 12 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
#[macro_export]
macro_rules! impl_from_str {
($t:ty) => {
impl FromStr for $t {
type Err = serde_json::Error;
impl ::std::str::FromStr for $t {
type Err = ::serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
::serde_json::from_str(s)
}
}
};
}

#[macro_export]
macro_rules! impl_display {
($t:ty) => {
impl std::fmt::Display for $t {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl ::std::fmt::Display for $t {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self)
::serde_json::to_string(self)
.expect(concat!("failed to serialize ", stringify!($t)))
)
}
}
};
}

#[macro_export]
macro_rules! payload_impl {
($t:ty) => {
impl_from_str! {$t}
impl_display! {$t}
$crate::macros::impl_from_str! {$t}
$crate::macros::impl_display! {$t}
};
}

pub(crate) use impl_display;
pub(crate) use impl_from_str;
pub(crate) use payload_impl;
4 changes: 1 addition & 3 deletions src/payloads/channel.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! チャンネル関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{Channel, TimeStamp, User};
use crate::payload_impl;
use crate::macros::payload_impl;

/// CHANNEL_CREATEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_channel_created.go#L9-L13)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/message.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! メッセージ関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{DeletedDirectMessage, DeletedMessage, Message, MessageStamp, TimeStamp, Uuid};
use crate::payload_impl;
use crate::macros::payload_impl;

/// MESSAGE_CREATEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_message_created.go#L10-L14)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/stamp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! スタンプ関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{TimeStamp, User, Uuid};
use crate::payload_impl;
use crate::macros::payload_impl;

/// STAMP_CREATEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_stamp_created.go#L11-L18)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/system.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! システム関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{Channel, TimeStamp};
use crate::payload_impl;
use crate::macros::payload_impl;

/// PINGペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_ping.go#L5-L8)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/tag.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! タグ関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{TimeStamp, Uuid};
use crate::payload_impl;
use crate::macros::payload_impl;

/// TAG_ADDEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_tag_added.go#L11-L16)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! イベントペイロード内部で使われる型
use std::str::FromStr;

use serde::{Deserialize, Serialize};

#[cfg(feature = "chrono")]
Expand All @@ -11,7 +9,7 @@ use chrono::{DateTime, Utc};
#[cfg(feature = "time")]
use time::OffsetDateTime;

use crate::payload_impl;
use crate::macros::payload_impl;

#[cfg(feature = "chrono")]
pub type TimeStamp = DateTime<Utc>;
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/user.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! ユーザー関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{TimeStamp, User};
use crate::payload_impl;
use crate::macros::payload_impl;

/// USER_CREATEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/d2bc98f1e0e68f4acc371eb78e6a49a167446761/service/bot/event/payload/ev_user_created.go#L9-L13)
Expand Down
4 changes: 1 addition & 3 deletions src/payloads/user_group.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! ユーザーグループ関連のイベントペイロード
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use super::types::{GroupMember, TimeStamp, UserGroup, Uuid};
use crate::payload_impl;
use crate::macros::payload_impl;

/// USER_GROUP_CREATEDペイロード
/// - [traQの型定義](https://github.com/traPtitech/traQ/blob/a1aaf12d089a9033461d0f1fcabb69a92873a3b1/service/bot/event/payload/ev_user_group_created.go#L9-L13)
Expand Down

0 comments on commit 8fdd84d

Please sign in to comment.