Skip to main content

Crate vynil_core

Crate vynil_core 

Source
Expand description

§vynil-core

Generic Rust toolbox combining a Rhai scripting engine and a Handlebars templating engine, with optional Kubernetes / OCI / S3 / HTTP handlers.

Extracted from the vynil workspace so it can be reused by other projects (kuberest, kydah, …) without pulling in vynil’s business abstractions (CRDs, package model, instance controllers).

Status: API is not yet stable — expect breaking changes before 1.0.

§Features

FeatureDefaultWhat it adds
rhaiengine::Script engine + every other module’s Rhai bindings
hbshbs::HandleBars engine
hbs-scriptingregister_helper_dir / rhai_register_helper_dir (handlebars/script_helper). Implies hbs + rhai. Keep it separate because script_helper pulls smartstring which breaks String + &String in some graphs (see hbs docs)
httphttp::RestClient (reqwest) + http_mock::RestClientMock. Implies rhai
cryptoargon_hash / bcrypt_hash / gen_private_key helpers (Handlebars + Rhai)
k8sGeneric K8s handlers (k8s::K8sGeneric, k8s::K8sObject, …) + mocks. Implies rhai
ocioci::Registry + OCI mock. Implies rhai
s3S3 helpers (s3::s3_get_yaml, s3::s3_list_keys). Implies rhai
fsFilesystem access from Rhai (file_read, file_write, …)
shellShell execution (shell::run / shell::get_out + Rhai shell_run)
passwordgen_password / gen_password_alphanum (opt-in to avoid name collisions)
# default: Rhai + Handlebars + HTTP + crypto
vynil-core = "0.7"
# Kubernetes project
vynil-core = { version = "0.7", features = ["k8s"] }
# Handlebars only, no Rhai/smartstring in the graph
vynil-core = { version = "0.7", default-features = false, features = ["hbs", "crypto"] }

§Quick start

vynil_core::set_client_name(|| "my-app.example.com".to_string());

// Rhai
let mut script = vynil_core::engine::Script::new_bare(vec!["scripts/".into()]);
script.engine.register_fn("my_fn", |s: String| s.len() as i64);
// script.run_file(&std::path::PathBuf::from("scripts/run.rhai"))?;

// Handlebars
let mut hbs = vynil_core::hbs::HandleBars::new();
let out = hbs.render("Hello {{ name }}!", &serde_json::json!({"name": "world"})).unwrap();
assert_eq!(out, "Hello world!");

§Client identity

vynil-core does not assume an identity. Call set_client_name once at startup before any HTTP or Kubernetes call, otherwise those calls panic with an actionable message.

vynil_core::set_client_name(|| "my-app.example.com".to_string());
assert!(vynil_core::client_name_is_set());

§Rhai helpers (injected by engine::Script::new_bare)

Common: sha256, log_debug/info/warn/error, url_encode, get_env, to_decimal, base64_encode/decode, json_encode/decode, basename, dirname. Additional, feature-gated: yaml_encode/decode, semver_from + inc_*, glob, date_now/format, crc32_hash/bcrypt_hash/argon, gen_private_key, gen_password (feature password), file_* (feature fs), shell_* (feature shell), Registry / s3_* / RestClient / k8s_* when their feature is enabled.

Scripts also get assert and import_run / import_template shims for optional imports.

§Handlebars helpers (injected by hbs::HandleBars::new)

See hbs::CORE_HBS_HELPERS for the full list. Highlights: base64_encode/decode, url_encode, to_decimal, header_basic, crc32_hash, argon_hash/bcrypt_hash/gen_private_key (feature crypto), gen_password* (feature password), plus the handlebars_misc_helpers set and the vendored json_to_str / str_to_json / json_query family.

§Crate boundaries

This crate stays generic: no dependency on vynil, kuberest or kydah, no default client name, no CRDs or vynil-specific Handlebars helpers.

Re-exports§

pub use client_name::client_name_is_set;
pub use client_name::get_client_name;
pub use client_name::set_client_name;
pub use semver::Semver;
pub use engine::Script;rhai
pub use hbs::HandleBars;hbs
pub use k8s::update_cache;k8s

Modules§

chrono
Date/time helpers (DateTimeHandler). Date/time helper for Rhai.
client_name
Global client identity (User-Agent / field-manager). Global client identity used as HTTP User-Agent and Kubernetes field-manager.
enginerhai
Rhai scripting engine (engine::Script) and its registered helpers. Rhai scripting engine.
globrhai
Glob matching (glob Rhai helper). Glob matching helper for Rhai (glob function wrapping wildmatch).
hashes
Hash helpers (crc32, bcrypt, argon2). Hash helpers: crc32_hash (always), bcrypt_hash / Argon (feature crypto).
hbshbs
Handlebars templating engine (hbs::HandleBars) and its helpers. Handlebars templating engine.
httphttp
HTTP client (http::RestClient). HTTP client (RestClient) and free helpers (http_get_yaml, headers_get).
http_mockhttp
Mock HTTP client for tests (http_mock::RestClientMock). Mock HTTP client for tests — Rhai-compatible drop-in for crate::http::RestClient.
k8sk8s
Kubernetes handlers (K8sGeneric, K8sObject, …). Kubernetes handlers.
k8s_mockk8s
Mock Kubernetes helpers. Mock Kubernetes handlers for tests.
keycrypto
Private key generation (RSA / ed25519 via OpenSSL). Private key generation (RSA / ed25519 via OpenSSL).
ocioci
OCI registry client (oci::Registry). OCI registry helpers (Registry) and auth utilities.
oci_mockoci
Mock OCI helpers. Mock OCI registry for tests. See crate::oci::Registry for the real client.
password
Password generation. Password generation with per-class minimums.
s3s3
S3 helpers (s3_get_yaml, s3_list_keys). S3 helpers (s3_get_yaml, s3_list_keys) exposed to Rhai.
semver
Semver parsing and mutation. Semver parsing and bumping.
shellshell
Shell execution helpers. Shell execution helpers.
yaml
YAML ↔ JSON helpers. YAML ↔ JSON / Rhai helpers.

Macros§

register_k8s_generick8s
register_k8s_objectk8s
register_k8s_rawk8s

Enums§

Error
Errors returned by vynil-core operations.

Functions§

error_chain
Render an error together with its full source() chain, e.g. error sending request for url (...): dns error: failed to lookup address information: ....
rhai_errrhai
Convert a Error into a Rhai EvalAltResult, including its full source() chain (see error_chain) so the real cause of a connection-level failure isn’t swallowed.
rhai_err_strrhai
Convert a string into a Rhai EvalAltResult.

Type Aliases§

Result
Crate result type. E defaults to Error.
RhaiResrhai
Result type used by Rhai-exposed functions. Alias for Result<T, Box<EvalAltResult>>.