Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:

- name: "Build all feature combinations"
run: |
cargo hack build --package ${{ matrix.package }} --feature-powerset
cargo hack build --package ${{ matrix.package }} --feature-powerset --no-dev-deps --depth 3
1 change: 1 addition & 0 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ prime-bigint = ["dep:lazy_static"]
num-traits = []
papyrus-serialization = ["std"]
secret-felt = ["alloc", "dep:zeroize", "dep:subtle", "subtle/const-generics", "subtle/core_hint_black_box", "dep:rand", "rand/alloc"]
devnet = ["alloc"]

[dev-dependencies]
proptest = { version = "1.5", default-features = false, features = [
Expand Down
191 changes: 191 additions & 0 deletions crates/starknet-types-core/src/chain_id/alloc_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#[cfg(not(feature = "std"))]
pub extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};

use crate::short_string;
use crate::short_string::ShortString;

use super::{ChainId, SN_MAIN_STR, SN_SEPOLIA_STR};

impl From<ChainId> for ShortString {
fn from(value: ChainId) -> Self {
match value {
ChainId::Mainnet => short_string!("SN_MAIN"),
ChainId::Sepolia => short_string!("SN_SEPOLIA"),
#[cfg(feature = "devnet")]
ChainId::Devnet(ss) => ss,
}
}
}

#[cfg(feature = "devnet")]
impl From<ShortString> for ChainId {
fn from(value: ShortString) -> Self {
if value.as_ref() == SN_MAIN_STR {
ChainId::Mainnet
} else if value.as_ref() == SN_SEPOLIA_STR {
ChainId::Sepolia
} else {
ChainId::Devnet(value)
}
}
}

#[cfg(not(feature = "devnet"))]
mod try_chain_id_from_short_string {
use crate::chain_id::{SN_MAIN_STR, SN_SEPOLIA_STR};

use super::*;

#[derive(Debug, Clone)]
pub struct TryChainIdFromShortStringError(ShortString);

impl core::fmt::Display for TryChainIdFromShortStringError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "unknown chain id: {}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for TryChainIdFromShortStringError {}

impl TryFrom<ShortString> for ChainId {
type Error = TryChainIdFromShortStringError;

fn try_from(value: ShortString) -> Result<Self, Self::Error> {
if value.as_ref() == SN_MAIN_STR {
Ok(ChainId::Mainnet)
} else if value.as_ref() == SN_SEPOLIA_STR {
Ok(ChainId::Sepolia)
} else {
Err(TryChainIdFromShortStringError(value))
}
}
}
}
#[cfg(not(feature = "devnet"))]
pub use try_chain_id_from_short_string::*;

// String

impl From<ChainId> for String {
fn from(value: ChainId) -> Self {
match value {
ChainId::Mainnet => SN_MAIN_STR.to_string(),
ChainId::Sepolia => SN_SEPOLIA_STR.to_string(),
#[cfg(feature = "devnet")]
ChainId::Devnet(ss) => ss.to_string(),
}
}
}

#[cfg(not(feature = "devnet"))]
impl From<ChainId> for &str {
fn from(value: ChainId) -> Self {
match value {
ChainId::Mainnet => SN_MAIN_STR,
ChainId::Sepolia => SN_SEPOLIA_STR,
}
}
}

#[derive(Debug, Clone, Copy)]
#[cfg(feature = "devnet")]
pub struct TryChainIdFromStringError(pub(super) crate::short_string::TryShortStringFromStringError);

#[derive(Debug, Clone)]
#[cfg(not(feature = "devnet"))]
pub struct TryChainIdFromStringError(pub(super) String);

impl core::fmt::Display for TryChainIdFromStringError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[cfg(feature = "devnet")]
write!(f, "failed to parse string as ShortString: {}", self.0)?;

#[cfg(not(feature = "devnet"))]
write!(f, "unknown chain id: {}", self.0)?;

Ok(())
}
}

#[cfg(feature = "std")]
impl std::error::Error for TryChainIdFromStringError {}

impl TryFrom<String> for ChainId {
type Error = TryChainIdFromStringError;

fn try_from(value: String) -> Result<Self, Self::Error> {
if value == SN_MAIN_STR {
return Ok(ChainId::Mainnet);
} else if value == SN_SEPOLIA_STR {
return Ok(ChainId::Sepolia);
}

#[cfg(feature = "devnet")]
match ShortString::try_from(value) {
Ok(ss) => Ok(ChainId::Devnet(ss)),
Err(e) => Err(TryChainIdFromStringError(e)),
}

#[cfg(not(feature = "devnet"))]
Err(TryChainIdFromStringError(value))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn short_string_and_chain_id_round_trip() {
let ss = short_string!("SN_MAIN");
let chain_id = ChainId::try_from(ss.clone()).unwrap();
assert_eq!(chain_id.to_string(), ss.to_string());

let ss = short_string!("SN_SEPOLIA");
let chain_id = ChainId::try_from(ss.clone()).unwrap();
assert_eq!(chain_id.to_string(), ss.to_string());

#[cfg(not(feature = "devnet"))]
{
let ss = short_string!("SN_DEVNET");
assert!(ChainId::try_from(ss).is_err());
}
#[cfg(feature = "devnet")]
{
let ss = short_string!("SN_DEVNET");
let chain_id = ChainId::try_from(ss.clone()).unwrap();
assert_eq!(ss.to_string(), chain_id.to_string());
}
}

#[test]
fn string_and_chain_id_round_trip() {
let s = String::from(SN_MAIN_STR);
let chain_id = ChainId::try_from(s.clone()).unwrap();
assert_eq!(chain_id.to_string(), s.to_string());

let s = String::from(SN_SEPOLIA_STR);
let chain_id = ChainId::try_from(s.clone()).unwrap();
assert_eq!(chain_id.to_string(), s.to_string());

#[cfg(not(feature = "devnet"))]
{
let s = String::from("SN_DEVNET");
assert!(ChainId::try_from(s).is_err());
}
#[cfg(feature = "devnet")]
{
let s = String::from("SN_DEVNET");
let chain_id = ChainId::try_from(s.clone()).unwrap();
assert_eq!(s, chain_id.to_string());

let s = String::from("SN_DEVNET_LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG");
assert!(ChainId::try_from(s).is_err());
let s = String::from("SN_DEVNET_🌟");
assert!(ChainId::try_from(s).is_err());
}
}
}
Loading