rust/utils: move common code to a function
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com>
This commit is contained in:
parent
6dab08b646
commit
4d7af0b49b
@ -17,40 +17,12 @@ use std::io;
|
|||||||
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
/// Parse a JSON lockfile definition.
|
|
||||||
fn lockfile_parse_stream<R: io::Read>(
|
|
||||||
fmt: utils::InputFormat,
|
|
||||||
input: &mut R,
|
|
||||||
) -> Fallible<LockfileConfig> {
|
|
||||||
let lockfile: LockfileConfig = match fmt {
|
|
||||||
utils::InputFormat::JSON => {
|
|
||||||
let lf: LockfileConfig = serde_json::from_reader(input).map_err(|e| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::InvalidInput,
|
|
||||||
format!("serde-json: {}", e.to_string()),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
lf
|
|
||||||
}
|
|
||||||
utils::InputFormat::YAML => {
|
|
||||||
let lf: LockfileConfig = serde_yaml::from_reader(input).map_err(|e| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::InvalidInput,
|
|
||||||
format!("serde-yaml: {}", e.to_string()),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
lf
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(lockfile)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a lockfile filename, parse it
|
/// Given a lockfile filename, parse it
|
||||||
fn lockfile_parse<P: AsRef<Path>>(filename: P,) -> Fallible<LockfileConfig> {
|
fn lockfile_parse<P: AsRef<Path>>(filename: P,) -> Fallible<LockfileConfig> {
|
||||||
let filename = filename.as_ref();
|
let filename = filename.as_ref();
|
||||||
let fmt = utils::InputFormat::detect_from_filename(filename)?;
|
let fmt = utils::InputFormat::detect_from_filename(filename)?;
|
||||||
let mut f = io::BufReader::new(utils::open_file(filename)?);
|
let mut f = io::BufReader::new(utils::open_file(filename)?);
|
||||||
let lf = lockfile_parse_stream(fmt, &mut f).map_err(|e| {
|
let lf = utils::parse_stream(&fmt, &mut f).map_err(|e| {
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
io::ErrorKind::InvalidInput,
|
io::ErrorKind::InvalidInput,
|
||||||
format!("Parsing {}: {}", filename.to_string_lossy(), e.to_string()),
|
format!("Parsing {}: {}", filename.to_string_lossy(), e.to_string()),
|
||||||
@ -142,7 +114,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn basic_valid() {
|
fn basic_valid() {
|
||||||
let mut input = io::BufReader::new(VALID_PRELUDE_JS.as_bytes());
|
let mut input = io::BufReader::new(VALID_PRELUDE_JS.as_bytes());
|
||||||
let lockfile = lockfile_parse_stream(utils::InputFormat::JSON, &mut input).unwrap();
|
let lockfile: LockfileConfig = utils::parse_stream(&utils::InputFormat::JSON, &mut input).unwrap();
|
||||||
assert!(lockfile.packages.len() == 2);
|
assert!(lockfile.packages.len() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,11 +132,11 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn basic_valid_override() {
|
fn basic_valid_override() {
|
||||||
let mut base_input = io::BufReader::new(VALID_PRELUDE_JS.as_bytes());
|
let mut base_input = io::BufReader::new(VALID_PRELUDE_JS.as_bytes());
|
||||||
let mut base_lockfile = lockfile_parse_stream(utils::InputFormat::JSON, &mut base_input).unwrap();
|
let mut base_lockfile: LockfileConfig = utils::parse_stream(&utils::InputFormat::JSON, &mut base_input).unwrap();
|
||||||
assert!(base_lockfile.packages.len() == 2);
|
assert!(base_lockfile.packages.len() == 2);
|
||||||
|
|
||||||
let mut override_input = io::BufReader::new(OVERRIDE_JS.as_bytes());
|
let mut override_input = io::BufReader::new(OVERRIDE_JS.as_bytes());
|
||||||
let override_lockfile = lockfile_parse_stream(utils::InputFormat::JSON, &mut override_input).unwrap();
|
let override_lockfile: LockfileConfig = utils::parse_stream(&utils::InputFormat::JSON, &mut override_input).unwrap();
|
||||||
assert!(override_lockfile.packages.len() == 1);
|
assert!(override_lockfile.packages.len() == 1);
|
||||||
|
|
||||||
base_lockfile.merge(override_lockfile);
|
base_lockfile.merge(override_lockfile);
|
||||||
@ -176,7 +148,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_invalid() {
|
fn test_invalid() {
|
||||||
let mut input = io::BufReader::new(INVALID_PRELUDE_JS.as_bytes());
|
let mut input = io::BufReader::new(INVALID_PRELUDE_JS.as_bytes());
|
||||||
match lockfile_parse_stream(utils::InputFormat::JSON, &mut input) {
|
match utils::parse_stream::<LockfileConfig, _>(&utils::InputFormat::JSON, &mut input) {
|
||||||
Err(ref e) => match e.downcast_ref::<io::Error>() {
|
Err(ref e) => match e.downcast_ref::<io::Error>() {
|
||||||
Some(ref ioe) if ioe.kind() == io::ErrorKind::InvalidInput => {}
|
Some(ref ioe) if ioe.kind() == io::ErrorKind::InvalidInput => {}
|
||||||
_ => panic!("Expected invalid lockfile, not {}", e.to_string()),
|
_ => panic!("Expected invalid lockfile, not {}", e.to_string()),
|
||||||
|
@ -13,7 +13,6 @@ use failure::{Fallible, bail};
|
|||||||
use openat;
|
use openat;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde_yaml;
|
|
||||||
use std::collections::{HashMap, BTreeMap};
|
use std::collections::{HashMap, BTreeMap};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@ -63,26 +62,7 @@ fn treefile_parse_stream<R: io::Read>(
|
|||||||
input: &mut R,
|
input: &mut R,
|
||||||
basearch: Option<&str>,
|
basearch: Option<&str>,
|
||||||
) -> Fallible<TreeComposeConfig> {
|
) -> Fallible<TreeComposeConfig> {
|
||||||
let mut treefile: TreeComposeConfig = match fmt {
|
let mut treefile: TreeComposeConfig = utils::parse_stream(&fmt, input)?;
|
||||||
utils::InputFormat::YAML => {
|
|
||||||
let tf: TreeComposeConfig = serde_yaml::from_reader(input).map_err(|e| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::InvalidInput,
|
|
||||||
format!("serde-yaml: {}", e.to_string()),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
tf
|
|
||||||
}
|
|
||||||
utils::InputFormat::JSON => {
|
|
||||||
let tf: TreeComposeConfig = serde_json::from_reader(input).map_err(|e| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::InvalidInput,
|
|
||||||
format!("serde-json: {}", e.to_string()),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
tf
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
treefile.basearch = match (treefile.basearch, basearch) {
|
treefile.basearch = match (treefile.basearch, basearch) {
|
||||||
(Some(treearch), Some(arch)) => {
|
(Some(treearch), Some(arch)) => {
|
||||||
|
@ -14,6 +14,9 @@ use tempfile;
|
|||||||
|
|
||||||
use curl::easy::Easy;
|
use curl::easy::Easy;
|
||||||
|
|
||||||
|
use serde_json;
|
||||||
|
use serde_yaml;
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
/// Supported config serialization used by treefile and lockfile
|
/// Supported config serialization used by treefile and lockfile
|
||||||
pub enum InputFormat {
|
pub enum InputFormat {
|
||||||
@ -36,6 +39,34 @@ impl InputFormat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Given a lockfile/treefile config definition, parse it
|
||||||
|
pub fn parse_stream<T, R: io::Read>(fmt: &InputFormat, input: &mut R) -> Fallible<T>
|
||||||
|
where
|
||||||
|
T: serde::de::DeserializeOwned
|
||||||
|
{
|
||||||
|
let parsed: T = match fmt {
|
||||||
|
InputFormat::JSON => {
|
||||||
|
let pf: T = serde_json::from_reader(input).map_err(|e| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
format!("serde-json: {}", e.to_string()),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
pf
|
||||||
|
}
|
||||||
|
InputFormat::YAML => {
|
||||||
|
let pf: T = serde_yaml::from_reader(input).map_err(|e| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
format!("serde-yaml: {}", e.to_string()),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
pf
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(parsed)
|
||||||
|
}
|
||||||
|
|
||||||
fn download_url_to_tmpfile(url: &str) -> Fallible<fs::File> {
|
fn download_url_to_tmpfile(url: &str) -> Fallible<fs::File> {
|
||||||
let mut tmpf = tempfile::tempfile()?;
|
let mut tmpf = tempfile::tempfile()?;
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user