|
| 1 | +use super::super::entities; |
| 2 | +use super::types; |
| 3 | + |
| 4 | +impl From<types::ProtocolParameters> for entities::ProtocolParameters { |
| 5 | + fn from(other: types::ProtocolParameters) -> Self { |
| 6 | + entities::ProtocolParameters::new(other.k, other.m, other.phi_f as f32) |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +impl From<entities::ProtocolParameters> for types::ProtocolParameters { |
| 11 | + fn from(other: entities::ProtocolParameters) -> Self { |
| 12 | + types::ProtocolParameters { |
| 13 | + k: other.k, |
| 14 | + m: other.m, |
| 15 | + phi_f: other.phi_f as f64, |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl From<entities::SignerWithStake> for entities::Signer { |
| 21 | + fn from(other: entities::SignerWithStake) -> Self { |
| 22 | + entities::Signer::new(other.party_id as types::ProtocolPartyId, "".to_string()) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl From<entities::SignerWithStake> for (types::ProtocolPartyId, types::ProtocolStake) { |
| 27 | + fn from(other: entities::SignerWithStake) -> Self { |
| 28 | + ( |
| 29 | + other.party_id as types::ProtocolPartyId, |
| 30 | + other.stake as types::ProtocolStake, |
| 31 | + ) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl From<(types::ProtocolPartyId, types::ProtocolStake)> for entities::SignerWithStake { |
| 36 | + fn from(other: (types::ProtocolPartyId, types::ProtocolStake)) -> Self { |
| 37 | + entities::SignerWithStake::new(other.0, "".to_string(), other.1) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +pub mod tests { |
| 43 | + use super::*; |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn test_protocol_parameters_from_into() { |
| 47 | + let protocol_parameters_expected = types::ProtocolParameters { |
| 48 | + k: 100, |
| 49 | + m: 1000, |
| 50 | + phi_f: 1.0, |
| 51 | + }; |
| 52 | + let protocol_initializer_entities_expected = entities::ProtocolParameters::new( |
| 53 | + protocol_parameters_expected.k, |
| 54 | + protocol_parameters_expected.m, |
| 55 | + protocol_parameters_expected.phi_f as f32, |
| 56 | + ); |
| 57 | + |
| 58 | + let protocol_initializer_entities_into: entities::ProtocolParameters = |
| 59 | + protocol_parameters_expected.into(); |
| 60 | + assert_eq!( |
| 61 | + protocol_initializer_entities_expected, |
| 62 | + protocol_initializer_entities_into |
| 63 | + ); |
| 64 | + |
| 65 | + let protocol_initializer_from: types::ProtocolParameters = |
| 66 | + protocol_initializer_entities_expected.into(); |
| 67 | + assert_eq!(protocol_parameters_expected, protocol_initializer_from); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_stake_distribution_from_into() { |
| 72 | + let stake_expected = (1 as types::ProtocolPartyId, 100 as types::ProtocolStake); |
| 73 | + let signer_with_stake_expected = entities::SignerWithStake::new(1, "".to_string(), 100); |
| 74 | + (); |
| 75 | + |
| 76 | + let signer_with_stake_expected_into: (types::ProtocolPartyId, types::ProtocolStake) = |
| 77 | + signer_with_stake_expected.clone().into(); |
| 78 | + assert_eq!(stake_expected, signer_with_stake_expected_into); |
| 79 | + |
| 80 | + let stake_expected_from = stake_expected.into(); |
| 81 | + assert_eq!(signer_with_stake_expected, stake_expected_from); |
| 82 | + } |
| 83 | +} |
0 commit comments