refactor: use cfg_attr to enable napi features conditionally

This commit is contained in:
naskya 2024-01-14 12:19:32 +09:00
parent 69cf9b42e0
commit 26cd2a68da
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
4 changed files with 8 additions and 15 deletions

View file

@ -268,7 +268,7 @@ if (!nativeBinding) {
const {
stringToAcct,
acctToString,
nativeRandomStr,
genString,
IdConvertType,
convertId,
nativeGetTimestamp,
@ -278,7 +278,7 @@ const {
module.exports.stringToAcct = stringToAcct;
module.exports.acctToString = acctToString;
module.exports.nativeRandomStr = nativeRandomStr;
module.exports.genString = genString;
module.exports.IdConvertType = IdConvertType;
module.exports.convertId = convertId;
module.exports.nativeGetTimestamp = nativeGetTimestamp;

View file

@ -1,13 +1,11 @@
use napi_derive::napi;
#[derive(Clone, Eq, PartialEq, Debug)]
#[napi(object)]
#[cfg_attr(feature = "napi", napi_derive::napi(object))]
pub struct Acct {
pub username: String,
pub host: Option<String>,
}
#[napi]
#[cfg_attr(feature = "napi", napi_derive::napi)]
pub fn string_to_acct(acct: String) -> Acct {
let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') {
stripped
@ -27,7 +25,7 @@ pub fn string_to_acct(acct: String) -> Acct {
}
}
#[napi]
#[cfg_attr(feature = "napi", napi_derive::napi)]
pub fn acct_to_string(acct: Acct) -> String {
match acct.host {
Some(host) => format!("{}@{}", acct.username, host),

View file

@ -1,6 +1,7 @@
use rand::{distributions::Alphanumeric, thread_rng, Rng};
/// Generate random string based on [thread_rng] and [Alphanumeric].
#[cfg_attr(feature = "napi", napi_derive::napi)]
pub fn gen_string(length: u16) -> String {
thread_rng()
.sample_iter(Alphanumeric)
@ -9,12 +10,6 @@ pub fn gen_string(length: u16) -> String {
.collect()
}
#[cfg(feature = "napi")]
#[napi_derive::napi]
pub fn native_random_str(length: u16) -> String {
gen_string(length)
}
#[cfg(test)]
mod unit_test {
use pretty_assertions::{assert_eq, assert_ne};

View file

@ -1,5 +1,5 @@
import { nativeRandomStr } from "native-utils/built/index.js";
import { genString } from "native-utils/built/index.js";
export function secureRndstr(length = 32, _ = true): string {
return nativeRandomStr(length);
return genString(length);
}