Skip to content

Commit 0dd5b37

Browse files
authored
feat: TryFrom impl for known chains on relevant types (#97)
1 parent 8fe5dfa commit 0dd5b37

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

crates/constants/src/types/environment.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@ impl SignetEnvironmentConstants {
4444
}
4545
}
4646

47-
impl FromStr for SignetEnvironmentConstants {
48-
type Err = ParseChainError;
47+
impl TryFrom<KnownChains> for SignetEnvironmentConstants {
48+
type Error = ParseChainError;
4949

50-
fn from_str(s: &str) -> Result<Self, Self::Err> {
51-
let chain: KnownChains = s.parse()?;
50+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
5251
match chain {
5352
#[cfg(any(test, feature = "test-utils"))]
5453
KnownChains::Test => Ok(Self::test()),
5554
}
5655
}
5756
}
5857

58+
impl FromStr for SignetEnvironmentConstants {
59+
type Err = ParseChainError;
60+
61+
fn from_str(s: &str) -> Result<Self, Self::Err> {
62+
s.parse::<KnownChains>()?.try_into()
63+
}
64+
}
65+
5966
#[cfg(test)]
6067
mod test {
6168
use super::*;

crates/constants/src/types/mod.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,25 @@ impl SignetSystemConstants {
216216
}
217217
}
218218

219-
impl FromStr for SignetSystemConstants {
220-
type Err = ParseChainError;
219+
impl TryFrom<KnownChains> for SignetSystemConstants {
220+
type Error = ParseChainError;
221221

222-
fn from_str(s: &str) -> Result<Self, Self::Err> {
223-
let chain: KnownChains = s.parse()?;
222+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
224223
match chain {
225224
#[cfg(any(test, feature = "test-utils"))]
226225
KnownChains::Test => Ok(Self::test()),
227226
}
228227
}
229228
}
230229

230+
impl FromStr for SignetSystemConstants {
231+
type Err = ParseChainError;
232+
233+
fn from_str(s: &str) -> Result<Self, Self::Err> {
234+
s.parse::<KnownChains>()?.try_into()
235+
}
236+
}
237+
231238
/// All constants pertaining to the Signet system.
232239
#[derive(Debug, Clone, PartialEq, Eq)]
233240
pub struct SignetConstants {
@@ -273,14 +280,21 @@ impl SignetConstants {
273280
}
274281
}
275282

276-
impl FromStr for SignetConstants {
277-
type Err = ParseChainError;
283+
impl TryFrom<KnownChains> for SignetConstants {
284+
type Error = ParseChainError;
278285

279-
fn from_str(s: &str) -> Result<Self, Self::Err> {
280-
let chain: KnownChains = s.parse()?;
286+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
281287
match chain {
282288
#[cfg(any(test, feature = "test-utils"))]
283289
KnownChains::Test => Ok(Self::test()),
284290
}
285291
}
286292
}
293+
294+
impl FromStr for SignetConstants {
295+
type Err = ParseChainError;
296+
297+
fn from_str(s: &str) -> Result<Self, Self::Err> {
298+
s.parse::<KnownChains>()?.try_into()
299+
}
300+
}

crates/constants/src/types/rollup.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,21 @@ impl RollupConstants {
102102
}
103103
}
104104

105-
impl FromStr for RollupConstants {
106-
type Err = ParseChainError;
105+
impl TryFrom<KnownChains> for RollupConstants {
106+
type Error = ParseChainError;
107107

108-
fn from_str(s: &str) -> Result<Self, Self::Err> {
109-
let chain: KnownChains = s.parse()?;
108+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
110109
match chain {
111110
#[cfg(any(test, feature = "test-utils"))]
112111
KnownChains::Test => Ok(Self::test()),
113112
}
114113
}
115114
}
115+
116+
impl FromStr for RollupConstants {
117+
type Err = ParseChainError;
118+
119+
fn from_str(s: &str) -> Result<Self, Self::Err> {
120+
s.parse::<KnownChains>()?.try_into()
121+
}
122+
}

crates/test-utils/src/specs/host_spec.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,17 +406,24 @@ impl HostBlockSpec {
406406
}
407407
}
408408

409-
impl FromStr for HostBlockSpec {
410-
type Err = ParseChainError;
409+
impl TryFrom<KnownChains> for HostBlockSpec {
410+
type Error = ParseChainError;
411411

412-
fn from_str(s: &str) -> Result<Self, Self::Err> {
413-
let chain: KnownChains = s.parse()?;
412+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
414413
match chain {
415414
KnownChains::Test => Ok(Self::test()),
416415
}
417416
}
418417
}
419418

419+
impl FromStr for HostBlockSpec {
420+
type Err = ParseChainError;
421+
422+
fn from_str(s: &str) -> Result<Self, Self::Err> {
423+
s.parse::<KnownChains>()?.try_into()
424+
}
425+
}
426+
420427
fn to_receipt<T>(address: Address, t: &T) -> ReceiptEnvelope
421428
where
422429
for<'a> &'a T: Into<LogData>,

crates/test-utils/src/specs/ru_spec.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,20 @@ impl RuBlockSpec {
141141
}
142142
}
143143

144-
impl FromStr for RuBlockSpec {
145-
type Err = ParseChainError;
144+
impl TryFrom<KnownChains> for RuBlockSpec {
145+
type Error = ParseChainError;
146146

147-
fn from_str(s: &str) -> Result<Self, Self::Err> {
148-
let chain: KnownChains = s.parse()?;
147+
fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
149148
match chain {
150149
KnownChains::Test => Ok(Self::test()),
151150
}
152151
}
153152
}
153+
154+
impl FromStr for RuBlockSpec {
155+
type Err = ParseChainError;
156+
157+
fn from_str(s: &str) -> Result<Self, Self::Err> {
158+
s.parse::<KnownChains>()?.try_into()
159+
}
160+
}

0 commit comments

Comments
 (0)