Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move common types under radius-sdk crate. #81

Open
Kanet1105 opened this issue Dec 9, 2024 · 0 comments
Open

Move common types under radius-sdk crate. #81

Kanet1105 opened this issue Dec 9, 2024 · 0 comments
Assignees

Comments

@Kanet1105
Copy link
Member

Kanet1105 commented Dec 9, 2024

Describe the feature

RPC types and primitives such as Platform, ServiceProvider, ValidationServiceProvider that are used across multiple crates need to be under radius-sdk/types and enable std::collection::HashMap serialization.

Solution

With "default" feature implemented:

use std::hash::Hash;

use serde::{Deserialize, Serialize};

#[cfg(any(feature = "default", feature = "full"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(try_from = "String", into = "String")]
pub enum Platform {
    Ethereum,
    Local,
}

impl TryFrom<String> for Platform {
    type Error = PlatformError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        match value.as_str() {
            "Ethereum" | "ethereum" => Ok(Self::Ethereum),
            "Local" | "local" => Ok(Self::Local),
            _others => Err(PlatformError::InvalidPlatform(value)),
        }
    }
}

impl From<Platform> for String {
    fn from(value: Platform) -> Self {
        match value {
            Platform::Ethereum => "Ethereum".to_owned(),
            Platform::Local => "Local".to_owned(),
        }
    }
}

#[derive(Debug)]
pub enum PlatformError {
    InvalidPlatform(String),
}

impl std::fmt::Display for PlatformError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for PlatformError {}
@Kanet1105 Kanet1105 self-assigned this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant