Skip to content

Commit

Permalink
Publish v0.1.12 add util::random_machine_id
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed Aug 31, 2023
1 parent ec7029c commit 999dcff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
39 changes: 38 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ regex = "1.5.4"
uuid = { version = "1.1", features = ["v4"] }
dunce = "1.0"
toml = "0.7"
rand = "0.8"

# painting
ansi_term = "0.12.1"
Expand Down
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod fs;
pub mod regex;
pub mod other;
pub mod toml;
pub mod util;

fn git_err(e: git2::Error) -> RhaiError {
err!("{:?}", e)
Expand Down
29 changes: 29 additions & 0 deletions src/api/util.rs
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());
}
4 changes: 2 additions & 2 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub type Result<T> = std::result::Result<T, Error>;

pub type RhaiResult<T> = std::result::Result<T, Box<EvalAltResult>>;


#[derive(Debug)]
pub enum Error {
Rhai(Box<EvalAltResult>),
Expand Down Expand Up @@ -46,7 +47,6 @@ pub struct Script {
}

impl Script {

pub fn new(p: impl AsRef<Path>) -> Result<Self> {
let engine = new_engine();
let mut scope = Scope::new();
Expand Down Expand Up @@ -83,7 +83,6 @@ impl Script {
) -> Result<()> {
self.call_fn(cmd, args)
}

}


Expand Down Expand Up @@ -111,6 +110,7 @@ fn new_engine() -> Engine {
crate::api::regex::add(&mut engine);
crate::api::other::add(&mut engine);
crate::api::toml::add(&mut engine);
crate::api::util::add(&mut engine);

engine
}
Expand Down

0 comments on commit 999dcff

Please sign in to comment.