Skip to content

Commit

Permalink
encoding version inside ProgramABI (#17)
Browse files Browse the repository at this point in the history
This PR adds the field "encoding: Version" to the ProgramABI. This will
be used later by Sway to inform the SDK which version of the encoding
was used.

The current version will be "0", which can also be represented with
```encoding: None```; and the new one we are developing now will be
version "1".

The new JSON is at FuelLabs/sway#5481
  • Loading branch information
xunilrj authored Jan 23, 2024
1 parent 2f983ea commit e6cd958
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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);
}

0 comments on commit e6cd958

Please sign in to comment.