-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
itf
native support for sum types
- Loading branch information
Showing
4 changed files
with
18 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,21 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)] | ||
pub struct EmptyObject {} | ||
|
||
pub type Height = i64; | ||
pub type Weight = i64; | ||
pub type Round = i64; | ||
pub type Address = String; | ||
pub type NonNilValue = String; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct VoteTypeTag { | ||
pub tag: String, // "Precommit" or "Prevote" | ||
pub value: EmptyObject, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
#[serde(tag = "tag", content = "value")] | ||
pub enum VoteType { | ||
Prevote, | ||
Precommit, | ||
} | ||
|
||
impl TryFrom<VoteTypeTag> for VoteType { | ||
type Error = String; | ||
fn try_from(v: VoteTypeTag) -> Result<Self, Self::Error> { | ||
match v.tag.as_str() { | ||
"Prevote" => Ok(VoteType::Prevote), | ||
"Precommit" => Ok(VoteType::Precommit), | ||
_ => todo!(), // error | ||
} | ||
} | ||
} | ||
|
||
pub type SerdeVoteType = serde_with::TryFromInto<VoteTypeTag>; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)] | ||
#[serde(untagged)] | ||
pub enum ValueValues { | ||
Val(NonNilValue), | ||
Nil(EmptyObject), | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ValueTag { | ||
pub tag: String, // "Nil" or "Val" | ||
pub value: ValueValues, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Hash)] | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)] | ||
#[serde(tag = "tag", content = "value")] | ||
pub enum Value { | ||
Nil, | ||
Val(String), | ||
} | ||
|
||
impl TryFrom<ValueTag> for Value { | ||
type Error = String; | ||
fn try_from(v: ValueTag) -> Result<Self, Self::Error> { | ||
match v.tag.as_str() { | ||
"Nil" => Ok(Value::Nil), | ||
"Val" => match v.value { | ||
ValueValues::Val(v) => Ok(Value::Val(v)), | ||
ValueValues::Nil(_) => todo!(), // error | ||
}, | ||
_ => todo!(), // error | ||
} | ||
} | ||
} | ||
|
||
pub type SerdeValue = serde_with::TryFromInto<ValueTag>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters