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
//  Copyright (C) 2020  Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

/// Parsers for transactions
pub mod transactions;

use crate::*;

/// Default hasher
pub type DefaultHasher = std::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>;

#[derive(Copy, Clone, Debug, Error)]
#[error("Fail to convert serde_json::Value into json_pest_parser::JSONValue")]
/// Error on conversion of serde_json value into pest_json value
pub struct JsonValueConversionError;

/// Convert serde_json value into pest_json value
pub fn serde_json_value_to_pest_json_value(
    value: &Value,
) -> Result<JSONValue<DefaultHasher>, JsonValueConversionError> {
    match value {
        Value::Null => Ok(JSONValue::Null),
        Value::Bool(boolean) => Ok(JSONValue::Boolean(*boolean)),
        Value::Number(number) => Ok(JSONValue::Number(if let Some(u64_) = number.as_u64() {
            Number::U64(u64_)
        } else if let Some(f64_) = number.as_f64() {
            Number::F64(f64_)
        } else {
            return Err(JsonValueConversionError);
        })),
        Value::String(string) => Ok(JSONValue::String(string)),
        Value::Array(values) => Ok(JSONValue::Array(
            values
                .iter()
                .map(serde_json_value_to_pest_json_value)
                .collect::<Result<Vec<JSONValue<DefaultHasher>>, JsonValueConversionError>>()?,
        )),
        Value::Object(map) => Ok(JSONValue::Object(
            map.into_iter()
                .map(|(k, v)| match serde_json_value_to_pest_json_value(v) {
                    Ok(v) => Ok((k.as_str(), v)),
                    Err(e) => Err(e),
                })
                .collect::<Result<
                    HashMap<&str, JSONValue<DefaultHasher>, DefaultHasher>,
                    JsonValueConversionError,
                >>()?,
        )),
    }
}

//std::collections::HashMap<&str, json_pest_parser::JSONValue<'_, std::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>>
//std::iter::Iterator<Item=(&std::string::String, json_pest_parser::JSONValue<'_, std::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>)>