Skip to content

Commit 1e0c565

Browse files
committed
Remove cairo-lang-runner from conversions crate
commit-id:d4abcdb5
1 parent 2ad0ac5 commit 1e0c565

File tree

4 files changed

+81
-11
lines changed

4 files changed

+81
-11
lines changed

Cargo.lock

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

crates/conversions/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ anyhow.workspace = true
1111
blockifier.workspace = true
1212
starknet_api.workspace = true
1313
starknet-types-core.workspace = true
14-
cairo-lang-runner.workspace = true
1514
cairo-lang-utils.workspace = true
1615
cairo-vm.workspace = true
1716
starknet.workspace = true

crates/conversions/src/byte_array.rs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate as conversions; // trick for CairoDeserialize macro
22
use crate::serde::deserialize::{BufferReadError, BufferReadResult, BufferReader};
33
use crate::{serde::serialize::SerializeToFeltVec, string::TryFromHexStr};
4-
use cairo_lang_runner::short_string::as_cairo_short_string_ex;
54
use cairo_lang_utils::byte_array::{BYTE_ARRAY_MAGIC, BYTES_IN_WORD};
65
use cairo_serde_macros::{CairoDeserialize, CairoSerialize};
6+
use conversions::felt::ToShortString;
77
use starknet_types_core::felt::Felt;
88
use std::fmt;
99

@@ -56,15 +56,88 @@ impl ByteArray {
5656

5757
impl fmt::Display for ByteArray {
5858
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59-
let full_words_string = self
59+
let words: String = self
6060
.words
6161
.iter()
62-
.map(|word| as_cairo_short_string_ex(word, BYTES_IN_WORD).unwrap())
63-
.collect::<String>();
62+
.map(|word| {
63+
let word: String = word.to_short_string().map_err(|_| fmt::Error)?;
64+
if word.len() != BYTES_IN_WORD {
65+
return Err(fmt::Error)?;
66+
}
67+
Ok(word)
68+
})
69+
.collect::<Result<Vec<String>, fmt::Error>>()?
70+
.join("");
71+
let pending_word = self
72+
.pending_word
73+
.to_short_string()
74+
.map_err(|_| fmt::Error)?;
6475

65-
let pending_word_string =
66-
as_cairo_short_string_ex(&self.pending_word, self.pending_word_len).unwrap();
76+
write!(f, "{words}{pending_word}")
77+
}
78+
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn test_fmt_empty() {
86+
let array = ByteArray::from("");
87+
assert_eq!(array.to_string(), "");
88+
}
89+
90+
#[test]
91+
fn test_fmt_single_word() {
92+
let array = ByteArray::from("Hello");
93+
assert_eq!(array.to_string(), "Hello");
94+
}
95+
96+
#[test]
97+
fn test_fmt_multiple_words() {
98+
let array = ByteArray::from("Hello World! This is a test.");
99+
assert_eq!(array.to_string(), "Hello World! This is a test.");
100+
}
101+
102+
#[test]
103+
fn test_fmt_with_pending_word() {
104+
let array = ByteArray::from("abc");
105+
assert_eq!(array.to_string(), "abc");
106+
}
107+
108+
#[test]
109+
fn test_fmt_special_chars() {
110+
let special_chars = "!@#$%^&*()_+-=[]{}|;:,.<>?";
111+
let array = ByteArray::from(special_chars);
112+
assert_eq!(array.to_string(), special_chars);
113+
}
114+
115+
#[test]
116+
#[should_panic(expected = "a Display implementation returned an error unexpectedly: Error")]
117+
fn test_fmt_with_null_bytes() {
118+
let with_nulls = "Hello\0World\0Test";
119+
let array = ByteArray::from(with_nulls);
120+
assert_eq!(array.to_string(), with_nulls);
121+
}
122+
123+
#[test]
124+
fn test_fmt_mixed_ascii() {
125+
let mixed = "Hello\tWorld\n123 !@#";
126+
let array = ByteArray::from(mixed);
127+
assert_eq!(array.to_string(), mixed);
128+
}
129+
130+
#[test]
131+
fn test_fmt_with_newlines() {
132+
let with_newlines = "First line\nSecond line\r\nThird line";
133+
let array = ByteArray::from(with_newlines);
134+
assert_eq!(array.to_string(), with_newlines);
135+
}
67136

68-
write!(f, "{full_words_string}{pending_word_string}")
137+
#[test]
138+
fn test_fmt_multiple_newlines() {
139+
let multiple_newlines = "Line1\n\n\nLine2\n\nLine3";
140+
let array = ByteArray::from(multiple_newlines);
141+
assert_eq!(array.to_string(), multiple_newlines);
69142
}
70143
}

crates/conversions/tests/e2e/felt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(test)]
22
mod tests_felt {
3-
use cairo_lang_runner::short_string::as_cairo_short_string;
43
use cairo_vm::utils::PRIME_STR;
54
use conversions::byte_array::ByteArray;
65
use conversions::felt::FromShortString;
@@ -87,7 +86,7 @@ mod tests_felt {
8786
fn test_from_short_string() {
8887
let felt = Felt::from_short_string("abc").unwrap();
8988

90-
assert_eq!("abc", &as_cairo_short_string(&felt).unwrap());
89+
assert_eq!(felt, Felt::from_hex("0x616263").unwrap());
9190
}
9291

9392
#[test]

0 commit comments

Comments
 (0)