forked from naskya/firefish
refactor: use cfg_attr to enable napi features conditionally
This commit is contained in:
parent
69cf9b42e0
commit
26cd2a68da
4 changed files with 8 additions and 15 deletions
|
@ -268,7 +268,7 @@ if (!nativeBinding) {
|
||||||
const {
|
const {
|
||||||
stringToAcct,
|
stringToAcct,
|
||||||
acctToString,
|
acctToString,
|
||||||
nativeRandomStr,
|
genString,
|
||||||
IdConvertType,
|
IdConvertType,
|
||||||
convertId,
|
convertId,
|
||||||
nativeGetTimestamp,
|
nativeGetTimestamp,
|
||||||
|
@ -278,7 +278,7 @@ const {
|
||||||
|
|
||||||
module.exports.stringToAcct = stringToAcct;
|
module.exports.stringToAcct = stringToAcct;
|
||||||
module.exports.acctToString = acctToString;
|
module.exports.acctToString = acctToString;
|
||||||
module.exports.nativeRandomStr = nativeRandomStr;
|
module.exports.genString = genString;
|
||||||
module.exports.IdConvertType = IdConvertType;
|
module.exports.IdConvertType = IdConvertType;
|
||||||
module.exports.convertId = convertId;
|
module.exports.convertId = convertId;
|
||||||
module.exports.nativeGetTimestamp = nativeGetTimestamp;
|
module.exports.nativeGetTimestamp = nativeGetTimestamp;
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
use napi_derive::napi;
|
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||||
#[napi(object)]
|
#[cfg_attr(feature = "napi", napi_derive::napi(object))]
|
||||||
pub struct Acct {
|
pub struct Acct {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub host: Option<String>,
|
pub host: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[cfg_attr(feature = "napi", napi_derive::napi)]
|
||||||
pub fn string_to_acct(acct: String) -> Acct {
|
pub fn string_to_acct(acct: String) -> Acct {
|
||||||
let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') {
|
let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') {
|
||||||
stripped
|
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 {
|
pub fn acct_to_string(acct: Acct) -> String {
|
||||||
match acct.host {
|
match acct.host {
|
||||||
Some(host) => format!("{}@{}", acct.username, host),
|
Some(host) => format!("{}@{}", acct.username, host),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||||
|
|
||||||
/// Generate random string based on [thread_rng] and [Alphanumeric].
|
/// Generate random string based on [thread_rng] and [Alphanumeric].
|
||||||
|
#[cfg_attr(feature = "napi", napi_derive::napi)]
|
||||||
pub fn gen_string(length: u16) -> String {
|
pub fn gen_string(length: u16) -> String {
|
||||||
thread_rng()
|
thread_rng()
|
||||||
.sample_iter(Alphanumeric)
|
.sample_iter(Alphanumeric)
|
||||||
|
@ -9,12 +10,6 @@ pub fn gen_string(length: u16) -> String {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "napi")]
|
|
||||||
#[napi_derive::napi]
|
|
||||||
pub fn native_random_str(length: u16) -> String {
|
|
||||||
gen_string(length)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod unit_test {
|
mod unit_test {
|
||||||
use pretty_assertions::{assert_eq, assert_ne};
|
use pretty_assertions::{assert_eq, assert_ne};
|
||||||
|
|
|
@ -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 {
|
export function secureRndstr(length = 32, _ = true): string {
|
||||||
return nativeRandomStr(length);
|
return genString(length);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue