-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish v0.1.12 add
util::random_machine_id
- Loading branch information
1 parent
ec7029c
commit 999dcff
Showing
5 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use super::RhaiResult; | ||
|
||
use std::fmt::Write; | ||
|
||
use rhai::{Engine, Module}; | ||
|
||
use rand::RngCore; | ||
|
||
|
||
// returns a random value that can be used in /etc/machine-id | ||
fn random_machine_id() -> RhaiResult<String> { | ||
let mut bytes = [0u8; 16]; | ||
rand::thread_rng().fill_bytes(&mut bytes); | ||
|
||
let mut s = String::with_capacity(32); | ||
for b in bytes { | ||
write!(s, "{b:02x}").unwrap(); | ||
} | ||
|
||
Ok(s) | ||
} | ||
|
||
|
||
pub fn add(engine: &mut Engine) { | ||
let mut util_mod = Module::new(); | ||
util_mod.set_native_fn("random_machine_id", random_machine_id); | ||
engine | ||
.register_static_module("util", util_mod.into()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters