blob: 4e7faf9205e848f604f006105255af540fb96f92 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//! ref: composer/src/Composer/Package/Loader/JsonLoader.php
use std::path::Path;
use anyhow::Result;
use crate::json::json_file::JsonFile;
use crate::package::base_package::BasePackage;
use crate::package::loader::loader_interface::LoaderInterface;
pub enum JsonLoaderInput {
File(JsonFile),
String(String),
}
pub struct JsonLoader {
loader: Box<dyn LoaderInterface>,
}
impl JsonLoader {
pub fn new(loader: Box<dyn LoaderInterface>) -> Self {
Self { loader }
}
pub fn load(&self, json: JsonLoaderInput) -> Result<Box<BasePackage>> {
let config = match json {
JsonLoaderInput::File(json_file) => json_file.read()?,
JsonLoaderInput::String(ref s) if Path::new(s).exists() => {
JsonFile::parse_json(&std::fs::read_to_string(s)?, Some(s))?
}
JsonLoaderInput::String(ref s) => {
JsonFile::parse_json(s, None)?
}
};
Ok(self.loader.load(config, None))
}
}
|