Skip to content

Commit 1393537

Browse files
authored
Merge pull request #6310 from Jiloc/feat/clarity-depend-on-clarity-serialization
feat: use `clarity-serialization` in `clarity`
2 parents e6487f6 + acc851f commit 1393537

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+3822
-9021
lines changed

.github/workflows/cargo-hack-check.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ jobs:
9191
- name: Run cargo hack check (WASM Web)
9292
run: |
9393
cargo hack check \
94+
-p clarity \
9495
-p clarity-serialization \
9596
-p stacks-common \
9697
--each-feature \
9798
--no-dev-deps \
98-
--exclude-features=default,rusqlite,ctrlc-handler,wasm-deterministic \
99+
--exclude-features=default,rusqlite,ctrlc-handler,wasm-deterministic,testing \
99100
--features=wasm-web
100101
101102
- name: Run cargo hack check (WASM Deterministic)
@@ -108,9 +109,6 @@ jobs:
108109
--include-features=wasm-deterministic,slog_json \
109110
--features=wasm-deterministic
110111
111-
- name: Run cargo hack check
112-
run: ${{ matrix.command }}
113-
114112
fuzz-targets:
115113
name: Fuzz targets (nightly)
116114
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clarity-serialization/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ readme = "README.md"
1212
[dependencies]
1313
lazy_static = { workspace = true }
1414
regex = { version = "1", default-features = false }
15+
rusqlite = { workspace = true, optional = true }
1516
serde = { workspace = true }
17+
serde_json = { version = "1.0", default-features = false }
1618
serde_derive = { workspace = true }
1719
slog = { workspace = true }
1820
stacks_common = { package = "stacks-common", path = "../stacks-common", default-features = false }
19-
thiserror = { workspace = true }
2021

2122
[dev-dependencies]
2223
mutants = "0.0.3"
@@ -25,7 +26,9 @@ rstest = "0.17.0"
2526
[features]
2627
default = []
2728
testing = []
29+
developer-mode = ["stacks_common/developer-mode"]
2830
slog_json = ["stacks_common/slog_json"]
31+
rusqlite = ["stacks_common/rusqlite", "dep:rusqlite"]
2932

3033
# Wasm-specific features for easier configuration
3134
wasm-web = ["stacks_common/wasm-web"]

clarity-serialization/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ This crate provides the core components for working with Clarity data structures
2121
This example demonstrates how to construct a complex Clarity `(tuple)` and serialize it to its hexadecimal string representation, which is suitable for use as a transaction argument.
2222

2323
```rust
24-
use clarity_serialization::types::{Value, TupleData, PrincipalData};
24+
use clarity_serialization::types::{PrincipalData, TupleData, Value};
2525

2626
fn main() -> Result<(), Box<dyn std::error::Error>> {
2727
// 1. Construct the individual values that will go into our tuple.
2828
let id = Value::UInt(101);
29-
let owner = Value::Principal(
30-
PrincipalData::parse("SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G")?
31-
);
32-
let metadata = Value::some(
33-
Value::buff_from(vec![0xde, 0xad, 0xbe, 0xef])?
34-
)?;
29+
let owner = Value::Principal(PrincipalData::parse(
30+
"SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G",
31+
)?);
32+
let metadata = Value::some(Value::buff_from(vec![0xde, 0xad, 0xbe, 0xef])?)?;
3533

3634
// 2. Create a vec of name-value pairs for the tuple.
3735
let tuple_fields = vec![
@@ -44,7 +42,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4442
let my_tuple = Value::from(TupleData::from_data(tuple_fields)?);
4543

4644
// 4. Serialize the tuple to its consensus-cricital hex string.
47-
let hex_string = my_tuple.serialize_to_hex()?;
45+
let hex_string = my_tuple
46+
.serialize_to_hex()
47+
.map_err(|e| format!("Error serializing tuple to hex: {e:?}"))?;
4848

4949
println!("Clarity Tuple: {my_tuple}");
5050
println!("Serialized Hex: {hex_string}");
@@ -64,7 +64,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6464
This example shows the reverse process: taking a hex string and deserializing it into a structured `Value` object, while validating it against an expected type.
6565

6666
```rust
67-
use clarity_serialization::types::{Value, TypeSignature};
67+
use clarity_serialization::types::{TypeSignature, Value};
6868

6969
fn main() -> Result<(), Box<dyn std::error::Error>> {
7070
let hex_string = "0c000000030269640100000000000000000000000000000065086d657461646174610a0200000004deadbeef056f776e65720514a46ff88886c2ef9762d970b4d2c63678835bd39d";
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (C) 2025 Stacks Open Internet Foundation
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
use std::fmt;
17+
18+
use crate::representations::Span;
19+
20+
/// In a near future, we can go further in our static analysis and provide different levels
21+
/// of diagnostics, such as warnings, hints, best practices, etc.
22+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
23+
pub enum Level {
24+
Note,
25+
Warning,
26+
Error,
27+
}
28+
29+
pub trait DiagnosableError {
30+
fn message(&self) -> String;
31+
fn suggestion(&self) -> Option<String>;
32+
fn level(&self) -> Level {
33+
Level::Error
34+
}
35+
}
36+
37+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
38+
pub struct Diagnostic {
39+
pub level: Level,
40+
pub message: String,
41+
pub spans: Vec<Span>,
42+
pub suggestion: Option<String>,
43+
}
44+
45+
impl Diagnostic {
46+
pub fn err(error: &dyn DiagnosableError) -> Diagnostic {
47+
Diagnostic {
48+
spans: vec![],
49+
level: Level::Error,
50+
message: error.message(),
51+
suggestion: error.suggestion(),
52+
}
53+
}
54+
55+
pub fn add_span(&mut self, start_line: u32, start_column: u32, end_line: u32, end_column: u32) {
56+
self.spans.push(Span {
57+
start_line,
58+
start_column,
59+
end_line,
60+
end_column,
61+
});
62+
}
63+
}
64+
65+
impl fmt::Display for Diagnostic {
66+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67+
write!(f, "{:?}", self.level)?;
68+
match self.spans.len().cmp(&1) {
69+
std::cmp::Ordering::Equal => write!(
70+
f,
71+
" (line {}, column {})",
72+
self.spans[0].start_line, self.spans[0].start_column
73+
)?,
74+
std::cmp::Ordering::Greater => {
75+
let lines: Vec<String> = self
76+
.spans
77+
.iter()
78+
.map(|s| format!("line: {}", s.start_line))
79+
.collect();
80+
write!(f, " ({})", lines.join(", "))?;
81+
}
82+
_ => {}
83+
}
84+
write!(f, ": {}.", &self.message)?;
85+
if let Some(suggestion) = &self.suggestion {
86+
write!(f, "\n{suggestion}")?;
87+
}
88+
writeln!(f)
89+
}
90+
}

clarity-serialization/src/errors.rs

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)