protoflow_blocks/blocks/
hash.rs

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
// This is free and unencumbered software released into the public domain.

#[cfg(not(feature = "hash"))]
pub mod hash {
    pub trait HashBlocks {}
    pub enum HashBlockConfig {}
}

#[cfg(feature = "hash")]
pub mod hash {
    use super::{
        prelude::{vec, Box, Cow, Named, Vec},
        types::HashAlgorithm,
        BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
    };
    use protoflow_core::Block;

    pub trait HashBlocks {
        fn hash_blake3(&mut self) -> Hash;
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum HashBlockTag {
        Hash,
    }

    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[derive(Clone, Debug)]
    pub enum HashBlockConfig {
        Hash {
            input: InputPortName,
            output: Option<OutputPortName>,
            hash: OutputPortName,
            algorithm: Option<HashAlgorithm>,
        },
    }

    impl Named for HashBlockConfig {
        fn name(&self) -> Cow<str> {
            use HashBlockConfig::*;
            Cow::Borrowed(match self {
                Hash { .. } => "Hash",
            })
        }
    }

    impl BlockConnections for HashBlockConfig {
        fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
            use HashBlockConfig::*;
            match self {
                Hash { output, hash, .. } => {
                    vec![("output", output.clone()), ("hash", Some(hash.clone()))]
                }
            }
        }
    }

    impl BlockInstantiation for HashBlockConfig {
        fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
            use HashBlockConfig::*;
            match self {
                Hash { algorithm, .. } => Box::new(super::Hash::with_system(system, *algorithm)),
            }
        }
    }

    mod hash;
    pub use hash::*;
}

pub use hash::*;