//! ref: composer/src/Composer/Package/Loader/JsonLoader.php use crate::json::JsonFile; use crate::package::PackageInterfaceHandle; use crate::package::loader::LoaderInterface; use anyhow::Result; use std::path::Path; pub enum JsonLoaderInput { File(JsonFile), String(String), } pub struct JsonLoader { loader: Box, } impl JsonLoader { pub fn new(loader: Box) -> Self { Self { loader } } pub fn load(&self, json: JsonLoaderInput) -> Result { let config = match json { JsonLoaderInput::File(mut json_file) => json_file.read()?, JsonLoaderInput::String(ref s) if Path::new(s).exists() => { let contents = std::fs::read_to_string(s)?; JsonFile::parse_json(Some(&contents), Some(s))? } JsonLoaderInput::String(ref s) => JsonFile::parse_json(Some(s), None)?, }; // TODO(phase-b): JsonFile::parse_json returns PhpMixed; loader::load expects IndexMap let _ = config; self.loader.load(indexmap::IndexMap::new(), None) } }