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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Amazon Web Services, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::commands::validate::generic_summary::GenericSummary;
use crate::commands::validate::{DataFile, OutputFormatType, Reporter};
use crate::rules::errors::Error;
use crate::rules::eval::eval_rules_file;
use crate::rules::eval_context::root_scope;
use crate::rules::path_value::traversal::Traversal;
use crate::rules::path_value::PathAwareValue;
use crate::rules::Result;
use std::convert::TryFrom;
use std::io::BufWriter;
use std::rc::Rc;

#[allow(dead_code)]
pub struct ValidateInput<'a> {
    pub content: &'a str,
    pub file_name: &'a str,
}

#[allow(dead_code)]
pub fn validate_and_return_json(
    data: ValidateInput,
    rules: ValidateInput,
    verbose: bool,
) -> Result<String> {
    let path_value = match serde_json::from_str::<serde_json::Value>(data.content) {
        Ok(value) => PathAwareValue::try_from(value),
        Err(_) => {
            let value = serde_yaml::from_str::<serde_yaml::Value>(data.content)?;
            PathAwareValue::try_from(value)
        }
    }
    .map_err(|e| {
        Error::ParseError(format!(
            "Unable to process data in file {}, Error {e},",
            data.file_name,
        ))
    })?;

    let input_data = DataFile {
        content: "".to_string(), // not used later
        path_value,
        name: data.file_name.to_owned(),
    };

    let span = crate::rules::parser::Span::new_extra(rules.content, rules.file_name);

    let rules_file_name = rules.file_name;
    return match crate::rules::parser::rules_file(span) {
        Ok(Some(rules)) => {
            let mut write_output = BufWriter::new(Vec::new());
            let root = input_data.path_value;
            let traversal = Traversal::from(&root);
            let mut root_scope = root_scope(&rules, Rc::new(root.clone()))?;
            let status = eval_rules_file(&rules, &mut root_scope, Some(&input_data.name))?;
            let root_record = root_scope.reset_recorder().extract();

            if verbose {
                return Ok(serde_json::to_string_pretty(&root_record)?);
            }

            let reporter = &GenericSummary::new() as &dyn Reporter;

            reporter.report_eval(
                &mut write_output,
                status,
                &root_record,
                rules_file_name,
                data.file_name,
                data.content,
                &traversal,
                OutputFormatType::JSON,
            )?;

            match String::from_utf8(write_output.buffer().to_vec()) {
                Ok(val) => Ok(val),
                Err(e) => Err(Error::ParseError(e.to_string())),
            }
        }
        Ok(None) => Ok(String::default()),
        Err(e) => Err(Error::ParseError(e.to_string())),
    };
}