use super::{
EnvReader, ManifestLoadStage, ManifestName, ManifestParse, NetsukeManifest, StdlibConfig,
StdlibRegistration, env_reader::disabled_env_reader, from_str_named, notify_stage,
workspace::open_manifest_workspace,
};
use crate::{localization, localization::keys, stdlib::NetworkPolicy};
use anyhow::{Context, Result};
use std::path::Path;
pub(crate) fn from_path_for_manifest_query(
path: impl AsRef<Path>,
on_stage: Option<&mut dyn FnMut(ManifestLoadStage)>,
) -> Result<NetsukeManifest> {
let env_reader = disabled_env_reader();
from_path_with_registration(path, &env_reader, on_stage, ManifestLoadMode::ManifestQuery)
}
pub(super) fn from_path_with_policy_and_env(
path: impl AsRef<Path>,
policy: NetworkPolicy,
env_reader: &EnvReader,
on_stage: Option<&mut dyn FnMut(ManifestLoadStage)>,
) -> Result<NetsukeManifest> {
from_path_with_registration(path, env_reader, on_stage, ManifestLoadMode::Full(policy))
}
enum ManifestLoadMode {
Full(NetworkPolicy),
ManifestQuery,
}
fn from_path_with_registration(
path: impl AsRef<Path>,
env_reader: &EnvReader,
mut on_stage: Option<&mut dyn FnMut(ManifestLoadStage)>,
mode: ManifestLoadMode,
) -> Result<NetsukeManifest> {
notify_stage(&mut on_stage, ManifestLoadStage::ManifestIngestion);
let path_ref = path.as_ref();
let workspace = open_manifest_workspace(path_ref)?;
let data = workspace
.dir
.read_to_string(&workspace.manifest_file)
.with_context(|| {
localization::message(keys::MANIFEST_READ_FAILED)
.with_arg("path", path_ref.display().to_string())
})?;
let name = ManifestName::new(path_ref.display().to_string());
let stdlib_registration = match mode {
ManifestLoadMode::Full(policy) => StdlibRegistration::Full(Box::new(
StdlibConfig::new(workspace.dir)?
.with_workspace_root_path(workspace.root)?
.with_network_policy(policy),
)),
ManifestLoadMode::ManifestQuery => StdlibRegistration::ManifestQuery,
};
from_str_named(
&data,
ManifestParse {
name: &name,
stdlib_registration: Some(stdlib_registration),
env_reader,
},
&mut on_stage,
)
}