Skip to content

Commit 9329fbc

Browse files
committed
Merge branch 'feat/clarity-serialization-crate' into feat/move-clarity-serialization-to-root
2 parents 995eaa5 + 649a33e commit 9329fbc

File tree

8 files changed

+28
-61
lines changed

8 files changed

+28
-61
lines changed

clarity-serialization/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,13 @@ pub enum CodecError {
117117
#[error("An unexpected internal error occurred: {0}")]
118118
Expect(String),
119119
}
120+
121+
// Implement PartialEq for testing and simple equality checks by comparing the
122+
// string representations of each error. This avoids requiring all wrapped
123+
// fields (like `std::io::Error`) to implement PartialEq.
124+
#[cfg(any(test, feature = "testing"))]
125+
impl PartialEq for CodecError {
126+
fn eq(&self, other: &Self) -> bool {
127+
self.to_string() == other.to_string()
128+
}
129+
}

clarity-serialization/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use errors::CodecError;
3232
pub use representations::{ClarityName, ContractName};
3333
pub use types::Value;
3434

35-
#[cfg(any(test, feature = "testing"))]
35+
#[cfg(test)]
3636
pub mod tests;
3737

3838
// set via _compile-time_ envars

clarity-serialization/src/tests/mod.rs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,7 @@
1515
mod representations;
1616
mod types;
1717

18-
use stacks_common::address::{AddressHashMode, C32_ADDRESS_VERSION_TESTNET_SINGLESIG};
19-
use stacks_common::types::chainstate::{StacksAddress, StacksPrivateKey, StacksPublicKey};
20-
21-
use crate::errors::CodecError;
22-
use crate::types::{PrincipalData, StandardPrincipalData, Value};
23-
use crate::{version_string, BUILD_TYPE};
24-
25-
impl Value {
26-
pub fn list_from(list_data: Vec<Value>) -> Result<Value, CodecError> {
27-
Value::cons_list_unsanitized(list_data)
28-
}
29-
}
30-
31-
// Implement PartialEq for testing and simple equality checks by comparing the
32-
// string representations of each error. This avoids requiring all wrapped
33-
// fields (like `std::io::Error`) to implement PartialEq.
34-
impl PartialEq for CodecError {
35-
fn eq(&self, other: &Self) -> bool {
36-
self.to_string() == other.to_string()
37-
}
38-
}
39-
40-
impl From<&StacksPrivateKey> for StandardPrincipalData {
41-
fn from(o: &StacksPrivateKey) -> StandardPrincipalData {
42-
let stacks_addr = StacksAddress::from_public_keys(
43-
C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
44-
&AddressHashMode::SerializeP2PKH,
45-
1,
46-
&vec![StacksPublicKey::from_private(o)],
47-
)
48-
.unwrap();
49-
StandardPrincipalData::from(stacks_addr)
50-
}
51-
}
52-
53-
impl From<&StacksPrivateKey> for PrincipalData {
54-
fn from(o: &StacksPrivateKey) -> PrincipalData {
55-
PrincipalData::Standard(StandardPrincipalData::from(o))
56-
}
57-
}
58-
59-
impl From<&StacksPrivateKey> for Value {
60-
fn from(o: &StacksPrivateKey) -> Value {
61-
Value::from(StandardPrincipalData::from(o))
62-
}
63-
}
18+
use crate::{BUILD_TYPE, version_string};
6419

6520
#[test]
6621
fn test_version_string_basic_no_env() {

clarity-serialization/src/tests/representations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use test_case::test_case;
1818

1919
use crate::errors::CodecError;
2020
use crate::representations::{
21-
ClarityName, ContractName, CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, MAX_STRING_LEN,
21+
CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, ClarityName, ContractName, MAX_STRING_LEN,
2222
};
2323
use crate::stacks_common::codec::StacksMessageCodec;
2424

clarity-serialization/src/tests/types/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ mod signatures;
1717

1818
use stacks_common::types::StacksEpochId;
1919

20+
use crate::CodecError;
2021
use crate::types::{
21-
BuffData, ListTypeData, PrincipalData, SequenceData, TupleData, TypeSignature, Value,
22-
MAX_VALUE_SIZE,
22+
BuffData, ListTypeData, MAX_VALUE_SIZE, PrincipalData, SequenceData, TupleData, TypeSignature,
23+
Value,
2324
};
24-
use crate::CodecError;
2525

2626
#[test]
2727
fn test_constructors() {

clarity-serialization/src/tests/types/serialization.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::io::Write;
1616

1717
use crate::errors::CodecError;
1818
use crate::types::{
19-
PrincipalData, QualifiedContractIdentifier, StandardPrincipalData, TupleData, TypeSignature,
20-
Value, MAX_VALUE_SIZE,
19+
MAX_VALUE_SIZE, PrincipalData, QualifiedContractIdentifier, StandardPrincipalData, TupleData,
20+
TypeSignature, Value,
2121
};
2222

2323
fn test_deser_ser(v: Value) {
@@ -51,12 +51,9 @@ fn test_bad_expectation(v: Value, e: TypeSignature) {
5151

5252
#[test]
5353
fn test_lists() {
54-
let list_list_int = Value::list_from(vec![Value::list_from(vec![
55-
Value::Int(1),
56-
Value::Int(2),
57-
Value::Int(3),
54+
let list_list_int = Value::list_from(vec![
55+
Value::list_from(vec![Value::Int(1), Value::Int(2), Value::Int(3)]).unwrap(),
5856
])
59-
.unwrap()])
6057
.unwrap();
6158
test_deser_ser(list_list_int.clone());
6259
test_deser_ser(Value::list_from(vec![]).unwrap());
@@ -290,8 +287,8 @@ fn test_vectors() {
290287
Ok(StandardPrincipalData::new(
291288
0x00,
292289
[
293-
0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff, 0x11, 0xde,
294-
0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff,
290+
0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff, 0x11, 0xde, 0xad,
291+
0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff,
295292
],
296293
)
297294
.unwrap()

clarity-serialization/src/tests/types/signatures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use std::collections::HashSet;
1616

1717
use crate::errors::CodecError;
18-
use crate::types::signatures::{CallableSubtype, TypeSignature};
1918
use crate::types::TypeSignature::{BoolType, IntType, ListUnionType, UIntType};
19+
use crate::types::signatures::{CallableSubtype, TypeSignature};
2020
use crate::types::{
2121
QualifiedContractIdentifier, SequenceSubtype, TraitIdentifier, TupleTypeSignature,
2222
};

clarity-serialization/src/types/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@ impl Value {
860860
})))
861861
}
862862

863+
#[cfg(any(test, feature = "testing"))]
864+
pub fn list_from(list_data: Vec<Value>) -> Result<Value, CodecError> {
865+
Value::cons_list_unsanitized(list_data)
866+
}
867+
863868
pub fn cons_list(list_data: Vec<Value>, epoch: &StacksEpochId) -> Result<Value, CodecError> {
864869
// Constructors for TypeSignature ensure that the size of the Value cannot
865870
// be greater than MAX_VALUE_SIZE (they error on such constructions)

0 commit comments

Comments
 (0)