From cb1497b2e51dcbf5e23aa66fd32c101a6bac80f2 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Tue, 16 Jan 2024 17:19:09 +0000 Subject: [PATCH] encoding version inside ProgramABI --- src/abi/full_program.rs | 4 ++++ src/abi/program.rs | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/abi/full_program.rs b/src/abi/full_program.rs index b14261a..9990480 100644 --- a/src/abi/full_program.rs +++ b/src/abi/full_program.rs @@ -13,6 +13,8 @@ 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 @@ -20,6 +22,7 @@ use crate::{ /// represent is virtually the same. #[derive(Debug, Clone)] pub struct FullProgramABI { + pub encoding: Option, pub types: Vec, pub functions: Vec, pub logged_types: Vec, @@ -66,6 +69,7 @@ impl FullProgramABI { .collect(); Ok(Self { + encoding: program_abi.encoding.clone(), types, functions, logged_types, diff --git a/src/abi/program.rs b/src/abi/program.rs index fb40c19..716ff4f 100644 --- a/src/abi/program.rs +++ b/src/abi/program.rs @@ -10,6 +10,8 @@ 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, pub types: Vec, pub functions: Vec, pub logged_types: Option>, @@ -17,6 +19,33 @@ pub struct ProgramABI { pub configurables: Option>, } +#[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 { @@ -85,3 +114,18 @@ pub struct Attribute { pub name: String, pub arguments: Vec, } + +#[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); +}