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

encoding version inside ProgramABI #17

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions src/abi/full_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ use crate::{
utils::TypePath,
};

use super::program::Version;

/// 'Full' versions of the ABI structures are needed to simplify duplicate
/// detection later on. The original ones([`ProgramABI`], [`TypeApplication`],
/// [`TypeDeclaration`] and others) are not suited for this due to their use of
/// ids, which might differ between contracts even though the type they
/// represent is virtually the same.
#[derive(Debug, Clone)]
pub struct FullProgramABI {
pub encoding: Option<Version>,
pub types: Vec<FullTypeDeclaration>,
pub functions: Vec<FullABIFunction>,
pub logged_types: Vec<FullLoggedType>,
Expand Down Expand Up @@ -66,6 +69,7 @@ impl FullProgramABI {
.collect();

Ok(Self {
encoding: program_abi.encoding.clone(),
types,
functions,
logged_types,
Expand Down
44 changes: 44 additions & 0 deletions src/abi/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,42 @@ use serde::{Deserialize, Serialize};
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProgramABI {
#[serde(skip_serializing_if = "Option::is_none")]
pub encoding: Option<Version>,
pub types: Vec<TypeDeclaration>,
pub functions: Vec<ABIFunction>,
pub logged_types: Option<Vec<LoggedType>>,
pub messages_types: Option<Vec<MessageType>>,
pub configurables: Option<Vec<Configurable>>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Version(pub String);

impl From<&str> for Version {
fn from(value: &str) -> Self {
Version(value.into())
}
}

impl Version {
pub fn major(&self) -> Option<&str> {
let s = self.0.split('.').next().map(|x| x.trim());
match s {
Some("") => None,
s => s,
}
}

pub fn minor(&self) -> Option<&str> {
let s = self.0.split('.').nth(1).map(|x| x.trim());
match s {
Some("") => None,
s => s,
}
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ABIFunction {
Expand Down Expand Up @@ -85,3 +114,18 @@ pub struct Attribute {
pub name: String,
pub arguments: Vec<String>,
}

#[test]
fn version_extraction_test() {
let v = Version("1.2".to_string());
assert_eq!(v.major(), Some("1"));
assert_eq!(v.minor(), Some("2"));

let v = Version("1".to_string());
assert_eq!(v.major(), Some("1"));
assert_eq!(v.minor(), None);

let v = Version("".to_string());
assert_eq!(v.major(), None);
assert_eq!(v.minor(), None);
}
Loading