Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete ICS20 Implementation #1989

Merged
merged 238 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
238 commits
Select commit Hold shift + click to select a range
462bd02
Add ics26 Module trait
hu55a1n1 Feb 2, 2022
1b42a79
newtype for Acknowledgement
hu55a1n1 Feb 3, 2022
ca23457
Define Router interface
hu55a1n1 Feb 3, 2022
f2753fd
Improve Router trait
hu55a1n1 Feb 4, 2022
a6e55c5
Update mock router impl
hu55a1n1 Feb 4, 2022
131ff62
Test for router API
hu55a1n1 Feb 4, 2022
ed69197
Disallow duplicate module_ids in MockRouter
hu55a1n1 Feb 4, 2022
b4662bb
Add RouterBuilder for seal-style API
hu55a1n1 Feb 4, 2022
3ed880e
Fix failing test
hu55a1n1 Feb 4, 2022
7e6d479
Fix CI after merge
hu55a1n1 Feb 10, 2022
1804fd6
Chainable RouterBuilder::add_route()
hu55a1n1 Feb 10, 2022
e86e261
Fix test
hu55a1n1 Feb 10, 2022
2215a96
Add Router::has_route() trait method
hu55a1n1 Feb 10, 2022
d03e506
Add comments
hu55a1n1 Feb 10, 2022
0bc52ad
Separate mutating/non-mutating trait methods for shadow-paging style API
hu55a1n1 Feb 11, 2022
b62a89e
Make most Module trait methods optional
hu55a1n1 Feb 11, 2022
76658d9
Default impl for MockModule::on_chan_open_try()
hu55a1n1 Feb 11, 2022
5b162f8
Acknowledgement trait
hu55a1n1 Feb 11, 2022
82ac86a
Extend router test
hu55a1n1 Feb 11, 2022
f1243df
Simplify OnRechPacketResult using FnOnce()
hu55a1n1 Feb 11, 2022
5801997
Cleanup
hu55a1n1 Feb 11, 2022
b6964fc
Use Cow<str> for CapabilityName::new()
hu55a1n1 Feb 12, 2022
07a8b5b
Use Cow<str> for CapabilityName::new()
hu55a1n1 Feb 12, 2022
6ee02f5
Use newtype ModuleId instead of trait assoc type
hu55a1n1 Feb 12, 2022
7d97990
Module callbacks' args as refs
hu55a1n1 Feb 12, 2022
d608600
Fix mock impl
hu55a1n1 Feb 12, 2022
1d54ba8
WIP channel callbacks
hu55a1n1 Feb 12, 2022
aae6881
Fix ModuleId ctor validation
hu55a1n1 Feb 12, 2022
2ca0a78
TypedCapability
hu55a1n1 Feb 14, 2022
7e3908e
Use typed capabilities
hu55a1n1 Feb 14, 2022
93a4718
Add Router::route_mut()
hu55a1n1 Feb 14, 2022
7086326
Implement pre-dispatch channel message validation
hu55a1n1 Feb 14, 2022
3f593c9
Avoid cloning message during channel msg dispatch
hu55a1n1 Feb 14, 2022
b510391
Add ChannelReader::lookup_module_by_channel()
hu55a1n1 Feb 14, 2022
c6f2bdc
Complete ics4 dispatch with callbacks
hu55a1n1 Feb 14, 2022
1ecf0a1
Fix compile errors from rebase
hu55a1n1 Feb 16, 2022
fff4cf5
Fix CI test
hu55a1n1 Feb 16, 2022
17acd75
Improve mock impl for capabilities
hu55a1n1 Feb 16, 2022
fa8263f
cargo fmt
hu55a1n1 Feb 16, 2022
fc8ef74
Set channel version returned from on_chan_open_try()
hu55a1n1 Feb 16, 2022
e8e3b72
Implement packet handler verification
hu55a1n1 Feb 16, 2022
274cd67
Add missing check already received packets on ordered channels
hu55a1n1 Feb 16, 2022
62cd000
Avoid cloning message during packet msg dispatch
hu55a1n1 Feb 16, 2022
840e6cc
Implement RecvPacket NoOp
hu55a1n1 Feb 16, 2022
28d76ca
cargo fmt and remove unused errors
hu55a1n1 Feb 16, 2022
3158725
Make on_recv_packet() Ack result optional
hu55a1n1 Feb 17, 2022
9a38b74
Don't return Result from on_recv_packet() and pass GenericAcknowledge…
hu55a1n1 Feb 17, 2022
3e67aab
Implement packet callbacks
hu55a1n1 Feb 17, 2022
d63769f
Allow callbacks to write logs and emit events
hu55a1n1 Feb 17, 2022
818d85b
Fix test
hu55a1n1 Feb 17, 2022
2a69737
Add .changelog entry
hu55a1n1 Feb 17, 2022
49a50dd
Add a comment for state rollback expectation from dispatch()
hu55a1n1 Feb 18, 2022
13a7b97
ics26_routing::handler::deliver() takes single message as input
hu55a1n1 Feb 21, 2022
87d6ee7
ics26_routing::handler::deliver() returns logs as well
hu55a1n1 Feb 21, 2022
275262e
Merge remote-tracking branch 'origin/master' into hu55a1n1/1758-compl…
hu55a1n1 Mar 14, 2022
3b138bb
Remove ctx_ro
hu55a1n1 Mar 17, 2022
1d430c9
Callbacks return ModuleOutput<T>
hu55a1n1 Mar 17, 2022
c34dfa9
Return HandlerOutputBuilder from channel and packet dispatch fn
hu55a1n1 Mar 18, 2022
8baaa3c
Revert "Callbacks return ModuleOutput<T>"
hu55a1n1 Mar 22, 2022
df4e968
Address review feedback for comments
hu55a1n1 Mar 22, 2022
2cc6b71
Extract ChannelMsg::lookup_module()
hu55a1n1 Mar 22, 2022
18242cf
Add ICS20 Denom type
hu55a1n1 Mar 21, 2022
0d5db5e
Add ICS20 TracePrefix & TracePath type
hu55a1n1 Mar 21, 2022
f3e5c49
Define Coin and Decimal types
hu55a1n1 Mar 21, 2022
fc5ddc4
Impl conversions from/to RawDenomTrace
hu55a1n1 Mar 22, 2022
7c8e8cd
Make better use of derive-more
hu55a1n1 Mar 22, 2022
14addac
Impl conversions for Coin type
hu55a1n1 Mar 22, 2022
35835a8
Add HashedDenom and polish DenomTrace impl
hu55a1n1 Mar 23, 2022
0a727da
Define PacketData domain type and conversions
hu55a1n1 Mar 23, 2022
525eb24
Use Coin domain type in MsgTransfer
hu55a1n1 Mar 23, 2022
5fe1756
Fix usage of Coin type in relayer code
hu55a1n1 Mar 23, 2022
4cdf5b6
Always panic on decimal arith overflow
hu55a1n1 Mar 23, 2022
b71647b
Polish generic Signer impl
hu55a1n1 Mar 23, 2022
b3ed86d
Implement ICS20 signer type
hu55a1n1 Mar 23, 2022
9505f87
Coin type with generic Denom param
hu55a1n1 Mar 23, 2022
a6818e8
Use IbcCoin in MsgTransfer and PrefixedCoin in PacketData
hu55a1n1 Mar 23, 2022
c3d1182
Minor refactoring
hu55a1n1 Mar 23, 2022
2aec438
Fix test_util
hu55a1n1 Mar 23, 2022
d76d61d
Impl AsRef<[u8]> for GenericAcknowledgement
hu55a1n1 Mar 24, 2022
ec9fc47
Ics20Context is no longer a supertrait of Ics26Context
hu55a1n1 Mar 24, 2022
f1379b0
Add ICS20 Ack type
hu55a1n1 Mar 24, 2022
75dd843
Add ICS20 event enum placeholder
hu55a1n1 Mar 24, 2022
9902773
Define all ICS20 expected keepers and context
hu55a1n1 Mar 24, 2022
f054014
Update send_transfer() for recent context changes
hu55a1n1 Mar 24, 2022
04f2502
Give mut ref to set_channel_escrow_address()
hu55a1n1 Mar 24, 2022
fa60ef1
Define ICS20 callback functions
hu55a1n1 Mar 24, 2022
dbe4e09
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
hu55a1n1 Apr 5, 2022
5ee70d0
Fix test build
hu55a1n1 Apr 5, 2022
9a373c2
Move denom derive functions to integration tests
hu55a1n1 Apr 6, 2022
009cbdd
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
hu55a1n1 Apr 13, 2022
20ceaaa
Add version to chan_open_try callback
hu55a1n1 Apr 14, 2022
6285655
Rename ChannelId::counter() to sequence()
hu55a1n1 Apr 14, 2022
90f0eae
Add AppModule error variant to ChannelError
hu55a1n1 Apr 14, 2022
68e1db8
Implement validation helpers for callbacks
hu55a1n1 Apr 14, 2022
89b3f9a
Define Ics20 callback errors
hu55a1n1 Apr 14, 2022
dbc05d3
Implement channel handshake callbacks
hu55a1n1 Apr 14, 2022
6e9cd1d
Fix clippy errors
hu55a1n1 Apr 18, 2022
253cf9b
Impl Deserialize for PacketData
hu55a1n1 Apr 19, 2022
8b6819a
Manually implement AsRef<str> for Signer
hu55a1n1 Apr 19, 2022
1545de3
Fix ICS20 Ack success from impl
hu55a1n1 Apr 19, 2022
1e381fb
Add more ICS20 errors
hu55a1n1 Apr 19, 2022
89deeb3
Improve ack type ctor
hu55a1n1 Apr 19, 2022
33b7976
Add ctor for TracePrefix
hu55a1n1 Apr 19, 2022
a33ba57
Provide denom trace methods to remove prefix and check for empty trac…
hu55a1n1 Apr 19, 2022
77a677a
Implement conversion from PrefixedCoin to IbcCoin
hu55a1n1 Apr 19, 2022
e856b6b
Add Ics20Reader trait methods to check send/receive enabled
hu55a1n1 Apr 19, 2022
4018471
Provided trait method get_channel_escrow_address()
hu55a1n1 Apr 19, 2022
c4e0d8b
Add BankReader trait for is_blocked_account()
hu55a1n1 Apr 19, 2022
873e8f9
Add FromStr bound for Ics20Context::AccountId
hu55a1n1 Apr 19, 2022
2a18d57
Use IbcCoins in Bank traits
hu55a1n1 Apr 19, 2022
fc6ff29
Impl on_recv_packet() for cases where receiver chain is source
hu55a1n1 Apr 19, 2022
42624ae
Fallible OnRecvPacket::write_fn()
hu55a1n1 Apr 20, 2022
c8dfdb6
Complete on_recv_packet impl
hu55a1n1 Apr 20, 2022
4513372
Fix test build
hu55a1n1 Apr 20, 2022
e9ca5b9
Fix clippy errors
hu55a1n1 Apr 20, 2022
70ac48f
Implement remaining packet callbacks for ICS20
hu55a1n1 Apr 20, 2022
5954d22
Make set_denom_trace() fallible
hu55a1n1 Apr 21, 2022
d18d568
Don't derive AsRef
hu55a1n1 Apr 21, 2022
a8b7749
Complete send_transfer impl
hu55a1n1 Apr 21, 2022
1ae5c32
Manual deserialize impl for Acknowledgement
hu55a1n1 Apr 23, 2022
65f9e5a
Add ctor for Acknowledgement
hu55a1n1 Apr 23, 2022
3e8d405
Handle packet-data deserialize error separately
hu55a1n1 Apr 23, 2022
518f49e
Use U256 for Denom and move bigint.rs to modules/src
hu55a1n1 Apr 23, 2022
9b127f7
Rename Denom to Amount
hu55a1n1 Apr 23, 2022
ed9aaed
Cleanup
hu55a1n1 Apr 23, 2022
89e7f39
Fix trait definitions
hu55a1n1 Apr 23, 2022
3a996bc
Use source enum
hu55a1n1 Apr 24, 2022
b00b8c4
Fix AccountReader trait
hu55a1n1 Apr 24, 2022
a6f0337
Validate port_id
hu55a1n1 Apr 24, 2022
9d080ca
More refactoring
hu55a1n1 Apr 24, 2022
2f1fcb9
Rename Signer to Address
hu55a1n1 Apr 25, 2022
0f98435
Use Address instead of Signer where applicable
hu55a1n1 Apr 25, 2022
6d4e5dd
Fix send_transfer packet creation
hu55a1n1 Apr 25, 2022
350dfde
Fix clippy warnings
hu55a1n1 Apr 25, 2022
fa271d6
Define ICS20 events
hu55a1n1 Apr 25, 2022
e90d6d3
Extract relay code into separate files
hu55a1n1 Apr 26, 2022
c051df0
Fix clippy warnings
hu55a1n1 Apr 26, 2022
9b08971
Make HandlerOutput/Builder generic over events
hu55a1n1 Apr 28, 2022
a14deed
Define ModuleEvent
hu55a1n1 Apr 28, 2022
09ee509
Add AppModule variant to IbcEvent
hu55a1n1 Apr 28, 2022
97ef661
Impl Display for Acknowledgement
hu55a1n1 Apr 29, 2022
66a115a
Derive serde for ModuleId
hu55a1n1 Apr 29, 2022
0e59174
Add ModuleId to ModuleEvent
hu55a1n1 Apr 29, 2022
55deccb
Impl conversion from tuple for ModuleEventAttribute
hu55a1n1 Apr 29, 2022
920e4fa
Impl conversions from ICS20 events to ModuleEvent
hu55a1n1 Apr 29, 2022
4aee2a3
Add event for transfer
hu55a1n1 Apr 29, 2022
81bd1b2
Remove bech32 validation from Address
hu55a1n1 Apr 29, 2022
67c143b
Change MsgTransfer receiver type to Address
hu55a1n1 Apr 29, 2022
6e9e737
Extract MockContext IbcStore
hu55a1n1 Apr 29, 2022
f473623
Improve conversion from Signer to AccountId
hu55a1n1 Apr 29, 2022
a6fd295
Add deliver method to Module trait
hu55a1n1 Apr 30, 2022
f424170
Implement conversions for MsgTransfer to/from Protobuf Any
hu55a1n1 Apr 30, 2022
d398fb5
Store packet result directly in send_transfer()
hu55a1n1 Apr 30, 2022
9997012
Make all ICS20 mods public
hu55a1n1 Apr 30, 2022
f11d86e
Make all IbsStore fields public to enable access for app modules
hu55a1n1 Apr 30, 2022
9f76587
Implement Ics20Context for DummyTransferModule
hu55a1n1 Apr 30, 2022
3e23f61
Fix failing test
hu55a1n1 Apr 30, 2022
75cc110
Manual Clone impl for MockContext
hu55a1n1 May 3, 2022
40aec61
Fix failing test
hu55a1n1 May 3, 2022
e87c0e8
Revert "Fix failing test"
hu55a1n1 May 3, 2022
63c1661
Fix MockContext Clone impl
hu55a1n1 May 3, 2022
36b929e
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
hu55a1n1 May 4, 2022
7ece694
Update trait definitions after merge with master
hu55a1n1 May 4, 2022
6373eba
Replace ModuleOutput with ModuleOutputBuilder in callbacks
hu55a1n1 May 4, 2022
37afb4e
Add inout param ModuleOutputBuilder to Ics20 callback handlers
hu55a1n1 May 4, 2022
7df33ca
Emit ICS20 receive packet events
hu55a1n1 May 4, 2022
626b339
Module::deliver() must be able to emit IbcEvents
hu55a1n1 May 4, 2022
d9a9159
Allow HandlerOutputBuilder to merge HandlerOutput
hu55a1n1 May 4, 2022
cf55074
Emit transfer event
hu55a1n1 May 4, 2022
bf298a2
Add AckStatusEvent
hu55a1n1 May 4, 2022
553b7a7
Emit ICS20 ack events
hu55a1n1 May 4, 2022
7665732
Emit ICS20 timeout events
hu55a1n1 May 4, 2022
2c48e76
Remove ABCI error code
hu55a1n1 May 4, 2022
2ae5962
Remove #[allow(unused)]
hu55a1n1 May 4, 2022
676e2f8
Add log for send transfer
hu55a1n1 May 4, 2022
d42e3bf
MsgReceipt abstraction
hu55a1n1 May 5, 2022
5a2f899
Handle missing IbcEvent to AbciEvent conversions for RecvPacket and T…
hu55a1n1 May 5, 2022
0a73bcb
Implement ModuleEvent to AbciEvent conversion
hu55a1n1 May 5, 2022
a791c71
Make send_transfer() public
hu55a1n1 May 6, 2022
8ddcaef
Allow empty TracePaths
hu55a1n1 May 6, 2022
70fbc24
Fix TracePath multiple prefix parse bug
hu55a1n1 May 6, 2022
510eb1f
Fix TracePath empty str parse bug
hu55a1n1 May 6, 2022
e9554c9
Improve DenomTrace FromStr
hu55a1n1 May 6, 2022
8a26799
Add denom validation test
hu55a1n1 May 6, 2022
b4f86ab
Add denom trace and serde tests
hu55a1n1 May 6, 2022
b35f878
TracePath tests
hu55a1n1 May 6, 2022
58766d4
Allow Denom with '/'
hu55a1n1 May 6, 2022
d724be4
Fix HashedDenom FromStr impl for empty hash
hu55a1n1 May 6, 2022
d54d315
Minor refactoring
hu55a1n1 May 6, 2022
43a9321
Add IbcCoin tests
hu55a1n1 May 6, 2022
0c99c9c
Add .changelog entry
hu55a1n1 May 9, 2022
92a36c6
Merge remote-tracking branch 'origin/master' into hu54a1n1/1884-ics20
hu55a1n1 May 12, 2022
8a4dc5c
Fix clippy errors
hu55a1n1 May 12, 2022
4b48efd
Disallow empty Signer and replace Address with it
hu55a1n1 May 13, 2022
b78e1ab
Use ToString as trait bound for ICS20 AccountId
hu55a1n1 May 13, 2022
5af05e5
Change AccountId trait bound from FromStr to TryFrom<Signer>
hu55a1n1 May 13, 2022
692d922
Remove Signer::new()
hu55a1n1 May 13, 2022
3c5be72
Delete OCap related TODO
hu55a1n1 May 13, 2022
1aaf710
Remove the PortKeeper
hu55a1n1 May 13, 2022
517590f
Remove Module::deliver()
hu55a1n1 May 13, 2022
88ae8d7
Fix clippy warnings
hu55a1n1 May 13, 2022
bac5f05
Rename transfer module
hu55a1n1 May 13, 2022
4192362
Rename mod relay
hu55a1n1 May 13, 2022
85d5c1f
Fix tests failing due to empty signer
hu55a1n1 May 19, 2022
e1d12bf
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
hu55a1n1 May 19, 2022
eff458c
Rename PORT_ID const to PORT_ID_STR
hu55a1n1 May 20, 2022
ea0d622
Fix escrow addr gen
hu55a1n1 May 20, 2022
0d349d2
Test cosmos escrow addr gen
hu55a1n1 May 20, 2022
d6ec510
Rename receiver account
hu55a1n1 May 20, 2022
823e7dd
Remove `has_denom_trace()`
hu55a1n1 May 20, 2022
2c61fc2
Remove `BankReader::is_blocked_account()`
hu55a1n1 May 20, 2022
a1cdafa
Remove `AccountId::ToString` bound
hu55a1n1 May 20, 2022
2d3a224
Remove `AccountReader`
hu55a1n1 May 20, 2022
a75453d
Mint/burn into user accounts directly
hu55a1n1 May 23, 2022
df94b92
Use `chunks_exact()` instead of `windows().step_by()`
hu55a1n1 May 23, 2022
9194f01
Rename `DenomTrace::has_prefix()` to `trace_starts_with()`
hu55a1n1 May 23, 2022
76b1a27
Move trace related methods into `TracePath`
hu55a1n1 May 23, 2022
a7622c9
Fix add/remove prefix
hu55a1n1 May 23, 2022
80be0a8
Add test for add/remove trace prefix
hu55a1n1 May 23, 2022
00f4ba6
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
hu55a1n1 May 23, 2022
eb5128a
Set denom trace only if not already set
hu55a1n1 May 24, 2022
81a9804
Use truncate instead of drain in cosmos_adr028_escrow_address()
hu55a1n1 May 24, 2022
b386e86
Rename DenomTrace as PrefixedDenom
hu55a1n1 May 24, 2022
49ad00c
Modify all `BankKeeper` methods to use `PrefixedCoin` instead of `Ibc…
hu55a1n1 May 24, 2022
abd290f
Rename `Denom` as `BaseDenom`
hu55a1n1 May 24, 2022
e16bcca
Impl Into<U256> and checked_add/sub() for Amount
hu55a1n1 May 26, 2022
cd289e3
Add more TracePath tests
hu55a1n1 May 26, 2022
8813602
Test TracePath is_empty()
hu55a1n1 May 26, 2022
8d1493f
Remove IbcCoin
hu55a1n1 May 30, 2022
2639b73
Remove HashedDenom and HashedCoin
hu55a1n1 May 30, 2022
c449599
Fix test compilation
hu55a1n1 May 30, 2022
27a5d74
Add comment for send_transfer()
hu55a1n1 May 30, 2022
74989bc
Functions for determining source chain
hu55a1n1 May 30, 2022
961ea36
Minor refactoring
hu55a1n1 May 31, 2022
14f2a8f
cargo fmt
hu55a1n1 May 31, 2022
24bf5cb
Derive serde for PacketCommitment and AcknowledgementCommitment
hu55a1n1 May 31, 2022
da45313
docstring and comment
plafer May 31, 2022
e5e027c
Merge remote-tracking branch 'origin/master' into hu55a1n1/1884-ics20
plafer May 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Complete ICS20 implementation ([#1759](https://github.com/informalsystems/ibc-rs/issues/1759))
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ subtle-encoding = { version = "0.5", default-features = false }
sha2 = { version = "0.10.2", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
num-traits = { version = "0.2.15", default-features = false }
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
uint = { version = "0.9", default-features = false }

[dependencies.tendermint]
version = "=0.23.7"
Expand Down

This file was deleted.

28 changes: 0 additions & 28 deletions modules/src/applications/ics20_fungible_token_transfer/denom.rs

This file was deleted.

46 changes: 0 additions & 46 deletions modules/src/applications/ics20_fungible_token_transfer/error.rs

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion modules/src/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! Various packet encoding semantics which underpin the various types of transactions.

pub mod ics20_fungible_token_transfer;
pub mod transfer;
61 changes: 61 additions & 0 deletions modules/src/applications/transfer/acknowledgement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::error::Error;
use crate::core::ics26_routing::context::Acknowledgement as AckTrait;
use crate::prelude::*;
use core::fmt::{Display, Formatter};

use serde::{Deserialize, Deserializer};

/// A string constant included in error acknowledgements.
/// NOTE: Changing this const is state machine breaking as acknowledgements are written into state
pub const ACK_ERR_STR: &str = "error handling packet on destination chain: see events for details";
pub const ACK_SUCCESS_B64: &[u8] = b"AQ==";

#[derive(Clone, Debug)]
pub enum Acknowledgement {
/// Equivalent to b"AQ==" (i.e. `base64::encode(0x01)`)
Success(Vec<u8>),
/// Error Acknowledgement
Error(String),
}

impl Acknowledgement {
pub fn success() -> Self {
Self::Success(ACK_SUCCESS_B64.to_vec())
}

pub fn from_error(err: Error) -> Self {
Self::Error(format!("{}: {}", ACK_ERR_STR, err))
}
}

impl AsRef<[u8]> for Acknowledgement {
fn as_ref(&self) -> &[u8] {
match self {
Acknowledgement::Success(b) => b.as_slice(),
Acknowledgement::Error(s) => s.as_bytes(),
}
}
}

impl<'de> Deserialize<'de> for Acknowledgement {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let ack = if s.as_bytes() == ACK_SUCCESS_B64 {
Self::Success(ACK_SUCCESS_B64.to_vec())
} else {
Self::Error(s)
};
Ok(ack)
}
}

impl Display for Acknowledgement {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Acknowledgement::Success(_) => write!(f, "AQ=="),
Acknowledgement::Error(err_str) => write!(f, "{}", err_str),
}
}
}

impl AckTrait for Acknowledgement {}
Loading