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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*-
 * shm-rs - a scheme serialization lib
 * Copyright (C) 2021  Aleksandr Morozov
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */



use std::fmt;
use std::io;
use std::num;

use std::collections::HashSet;
use crate::dynamic_scheme::error::DynamicSchemeError;
use crate::lexer::lexer_error::LexerError;
use crate::serializator::error::SerializationError;
use crate::static_scheme::error::StaticSchemeError;


pub type SchemeResult<T> = Result<T, SchemeError>;

#[derive(Debug)]
pub enum SchemeError 
{
    Io(io::Error),
    LexerErr(LexerError),
    SerderJsonErr(serde_json::Error),
    SedreJson(serde_json::error::Error),
    Utf8Error(std::str::Utf8Error),
    //CidrErr(cidr::NetworkParseError),
    ParseIntErr(num::ParseIntError),

    SchmStaticErr(StaticSchemeError),
    SchmDynamicErr(DynamicSchemeError),
    SchmSerialsErr(SerializationError),
}

impl fmt::Display for SchemeError 
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
    {
        match *self 
        {
            Self::Io(ref err) => err.fmt(f),
            Self::LexerErr(ref err) => err.fmt(f),
            Self::SerderJsonErr(ref err) => err.fmt(f),
            Self::SedreJson(ref err) => err.fmt(f),
            Self::Utf8Error(ref err) => err.fmt(f),
            //SchemeError::CidrErr(ref err) => err.fmt(f),
            Self::ParseIntErr(ref err) => err.fmt(f),

            Self::SchmStaticErr(ref err) => err.fmt(f),
            Self::SchmDynamicErr(ref err) => err.fmt(f),
            Self::SchmSerialsErr(ref err) => err.fmt(f),
        }
    }
}

impl From<io::Error> for SchemeError 
{
    fn from(err: io::Error) -> SchemeError 
    {
        SchemeError::Io(err)
    }
}

impl From<LexerError> for SchemeError 
{
    fn from(err: LexerError) -> SchemeError 
    {
        SchemeError::LexerErr(err)
    }
}

impl From<serde_json::Error> for SchemeError 
{
    fn from(err: serde_json::Error) -> SchemeError 
    {
        SchemeError::SerderJsonErr(err)
    }
}

impl From<std::str::Utf8Error> for SchemeError 
{
    fn from(err: std::str::Utf8Error) -> SchemeError 
    {
        SchemeError::Utf8Error(err)
    }
}

/*impl From<cidr::NetworkParseError> for SchemeError 
{
    fn from(err: cidr::NetworkParseError) -> SchemeError 
    {
        SchemeError::CidrErr(err)
    }
}*/

impl From<num::ParseIntError> for SchemeError 
{
    fn from(err: num::ParseIntError) -> SchemeError 
    {
        SchemeError::ParseIntErr(err)
    }
}

impl From<StaticSchemeError> for SchemeError 
{
    fn from(err: StaticSchemeError) -> SchemeError 
    {
        SchemeError::SchmStaticErr(err)
    }
}

impl From<DynamicSchemeError> for SchemeError 
{
    fn from(err: DynamicSchemeError) -> SchemeError 
    {
        SchemeError::SchmDynamicErr(err)
    }
}

impl From<SerializationError> for SchemeError 
{
    fn from(err: SerializationError) -> SchemeError 
    {
        SchemeError::SchmSerialsErr(err)
    }
}

pub 
fn convert_list<T: fmt::Display>(extr: &[T]) -> String
{
    let recs: Vec<String> = extr.iter().map(|v| format!("{}", v)).collect();

    return recs.join(" ");
}

pub 
fn convert_hashset<T: fmt::Display>(extr: &HashSet<T>, sep: &str) -> String
{
    let recs: Vec<String> = extr.iter().map(|v| format!("{}", v)).collect();

    return recs.join(sep);
}