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
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * 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 version 3 of the
 * License.
 *
 * 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/>.
 */

use super::*;
use serde::de::SeqAccess;
use serde::de::Visitor;
use serde::ser::SerializeSeq;
use serde::Deserializer;
use serde::Serializer;
use std::fmt;

pub mod par_serializer {
    use super::*;

    pub fn serialize<S>(value: &ParResult, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(2))?;
        seq.serialize_element(&value.left_size)?;
        seq.serialize_element(&value.right_size)?;
        seq.end()
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<ParResult, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ParVisitor;
        impl<'de> Visitor<'de> for ParVisitor {
            type Value = ParResult;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("[left_size, right_size]")
            }

            fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
                let left_size = seq.next_element::<u32>()?;
                let right_size = seq.next_element::<u32>()?;

                let (left_size, right_size) = match (left_size, right_size) {
                    (Some(left_size), Some(right_size)) => (left_size, right_size),
                    _ => return Err(serde::de::Error::custom(
                        "failed to deserialize ParResult, not enough elements in serialized array",
                    )),
                };
                let par_result = ParResult::new(left_size, right_size);

                Ok(par_result)
            }
        }

        deserializer.deserialize_seq(ParVisitor {})
    }
}