#![deny(rust_2018_idioms)]
#![warn(missing_docs)]
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
#[cfg(feature = "ast")]
pub mod ast;
#[cfg(feature = "ast")]
pub mod code_index;
#[cfg(feature = "computer")]
pub mod computer;
pub mod embed;
pub mod error;
pub mod fs;
pub mod fs_snapshot;
pub mod fs_watch;
pub mod host_conditions;
pub mod host_env_custody;
pub mod host_lease;
pub mod host_lease_capability;
pub mod process;
mod process_liveness;
pub mod sandbox;
pub mod scanner;
pub mod schemas;
pub mod secret_store;
pub mod session;
#[cfg(feature = "terminal-session")]
pub mod terminal_session;
pub mod tools;
pub mod verdict;
mod json;
mod registry;
mod text;
mod value_args;
pub use error::HostlibError;
pub use host_conditions::{
HostConditionObservation, HostConditionStatus, HostConditionsCapability,
HostConditionsSnapshot, HostConditionsSource, HostContentionQuestion, HostEnvironment,
InjectedHostConditionsSource, LocalHostConditionsSource, HOST_CONDITIONS_SCHEMA_VERSION,
};
pub use host_lease::{
HostLeaseAcquireReceipt, HostLeaseAcquireStatus, HostLeaseCargoExecutionContext,
HostLeaseDeferReason, HostLeaseDeferReceipt, HostLeaseError, HostLeaseExecutionContext,
HostLeaseHandle, HostLeaseMetadataUpdateReceipt, HostLeaseOperationKind, HostLeasePathIdentity,
HostLeasePriorityClass, HostLeaseProcessExit, HostLeaseQueueEvidence, HostLeaseReleaseReceipt,
HostLeaseRenewReceipt, HostLeaseRequest, HostLeaseResourceClass, HostLeaseResourceDefinition,
HostLeaseResourceKey, HostLeaseRunLaunchFailure, HostLeaseRunReceipt,
HostLeaseRunReleaseOutcome, HostLeaseRunStartFailure, HostLeaseRunState, HostLeaseState,
HostLeaseStore, DEFAULT_HOST_LEASE_DOMAIN, HOST_LEASE_ROOT_ENV,
};
pub use registry::{BuiltinRegistry, HostlibCapability, HostlibRegistry, RegisteredBuiltin};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedHarnReference {
pub from_path: String,
pub to_path: String,
pub to_name: String,
}
#[derive(Debug, Clone)]
pub struct HarnReferenceInput {
pub root: PathBuf,
pub files: Vec<PathBuf>,
pub source_overrides: HashMap<PathBuf, String>,
}
pub type HarnReferenceResolver =
Arc<dyn Fn(&HarnReferenceInput) -> Result<Vec<ResolvedHarnReference>, String> + Send + Sync>;
pub struct DefaultHostlibHandles {
#[cfg(feature = "ast")]
pub code_index: code_index::CodeIndexCapability,
}
pub fn install_default(vm: &mut harn_vm::Vm) -> HostlibRegistry {
install_default_with_embed(vm, embed::EmbedCapability::from_env()).0
}
pub fn install_default_with_handles(
vm: &mut harn_vm::Vm,
) -> (HostlibRegistry, DefaultHostlibHandles) {
install_default_with_embed(vm, embed::EmbedCapability::from_env())
}
pub fn install_default_with_embed(
vm: &mut harn_vm::Vm,
embed: embed::EmbedCapability,
) -> (HostlibRegistry, DefaultHostlibHandles) {
install_default_with_embed_and_harn_reference_resolver(vm, embed, None)
}
pub fn install_default_with_embed_and_harn_reference_resolver(
vm: &mut harn_vm::Vm,
embed: embed::EmbedCapability,
harn_reference_resolver: Option<HarnReferenceResolver>,
) -> (HostlibRegistry, DefaultHostlibHandles) {
let mut registry = HostlibRegistry::new();
let session = session::SessionCapability::with_embedder(embed.embedder().clone());
#[cfg(feature = "ast")]
let code_index_handle = {
let mut code_index = code_index::CodeIndexCapability::new();
if let Some(resolver) = harn_reference_resolver {
code_index = code_index.with_harn_reference_resolver(resolver);
}
let handle = code_index.clone();
registry = registry
.with(ast::AstCapabilityWithCodeIndex::new(code_index.shared()))
.with(code_index);
handle
};
#[cfg(not(feature = "ast"))]
let _ = harn_reference_resolver;
registry = registry
.with(scanner::ScannerCapability)
.with(embed)
.with(session)
.with(fs::FsCapability)
.with(fs_snapshot::FsSnapshotCapability)
.with(fs_watch::FsWatchCapability)
.with(tools::ToolsCapability)
.with(secret_store::SecretStoreCapability)
.with(verdict::VerdictCapability)
.with(host_conditions::HostConditionsCapability::default())
.with(host_lease_capability::HostLeaseCapability);
#[cfg(feature = "terminal-session")]
{
registry = registry.with(terminal_session::TerminalSessionCapability::new());
}
#[cfg(feature = "computer")]
{
registry = registry.with(computer::ComputerUseCapability::new());
}
registry.register_into_vm(vm);
vm.register_builtin("hostlib_enable", |_args, _out| Ok(harn_vm::VmValue::Nil));
let handles = DefaultHostlibHandles {
#[cfg(feature = "ast")]
code_index: code_index_handle,
};
(registry, handles)
}