Skip to content

Commit

Permalink
fix(rsjudge-runner): 🙈 hide internal macros from other crates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Apr 3, 2024
1 parent 564bebe commit 61b7bbc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
4 changes: 4 additions & 0 deletions crates/rsjudge-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pub use crate::{

mod caps_check;
mod error;

#[macro_use]
mod user_macro;
pub mod user;

pub trait RunAs {
type Error;
fn run_as(&mut self, user: &User) -> Result<&mut Self>;
Expand Down
25 changes: 5 additions & 20 deletions crates/rsjudge-runner/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,11 @@ use std::sync::OnceLock;

use uzers::{get_user_by_name, User};

use crate::error::{Error, Result};

/// Generate functions to get user instances.
macro_rules! users {
($($vis:vis fn $id:ident() => $name:literal);* $(;)?) => {
$(
#[doc = concat!("Get an instance of user `", $name, "`.")]
///
/// # Errors
/// Returns an error if the user is not found.
pub fn $id() -> Result<&'static User> {
static INNER: OnceLock<Option<User>> = OnceLock::new();
INNER
.get_or_init(|| get_user_by_name($name))
.as_ref()
.ok_or_else(|| Error::UserNotFound { username: $name })
}
)*
};
}
use crate::{
error::{Error, Result},
// #[macro_use]
// user_macro::users,
};

users! {
pub fn supervisor() => "rsjudge-supervisor";
Expand Down
18 changes: 18 additions & 0 deletions crates/rsjudge-runner/src/user_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Generate functions to get user instances.
macro_rules! users {
($($vis:vis fn $id:ident() => $name:literal);* $(;)?) => {
$(
#[doc = concat!("Get an instance of user `", $name, "`.")]
///
/// # Errors
/// Returns an error if the user is not found.
pub fn $id() -> Result<&'static User> {
static INNER: OnceLock<Option<User>> = OnceLock::new();
INNER
.get_or_init(|| get_user_by_name($name))
.as_ref()
.ok_or_else(|| Error::UserNotFound { username: $name })
}
)*
};
}

0 comments on commit 61b7bbc

Please sign in to comment.