Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Serialize struct from crate to ruby #142

Open
calavera opened this issue Jan 17, 2018 · 0 comments
Open

Serialize struct from crate to ruby #142

calavera opened this issue Jan 17, 2018 · 0 comments
Labels

Comments

@calavera
Copy link

Hi,

Sorry if this sounds like a question more than an issue, but I thought it'd be better to have this here so other people can find some information about it if they try to use helix in a similar way. This question might also be driven by my lack of knowledge about Rust, keep that in mind.

I have a crate that I'd like to reuse in several projects, one of them being a Rails application. This crate defines several public structures. I'm copying here the basic information about that crate so you don't have to switch contexts, but you can find it here:
https://github.com/calavera/netlify-toml-rs.

/// Config represents the full configuration within a netlify.toml file.
#[derive(Serialize, Deserialize)]
pub struct Config {
    pub build: Option<Context>,
    pub context: Option<HashMap<String, Context>>,
   // Other fields, not relevant for this example
}

/// Context holds the build variables Netlify uses to build a site before deploying it.
#[derive(Serialize, Deserialize)]
pub struct Context {
    pub base: Option<String>,
    pub publish: Option<String>,
    pub command: Option<String>,
    pub functions: Option<String>,
    pub environment: Option<HashMap<String, String>>,
}

Those structures are used to deserialize a TOML document using toml-rs:

pub fn from_str(io: &str) -> Result<Config, toml::de::Error> {
    toml::from_str::<Config>(io)
}

Ideally, I'd like to serialize that Config into a Ruby class directly, I think I need to implement the ToRuby trait, but I'm not sure how to make that work, or if it'd work. I was considering having something like this:

ruby! {
  class NetlifyToml {
    struct {
      config: netlify_toml::Config
    }
  }
}

But then, I'd also like to reuse some functions that Config implements.

As a last resort, I would be ok with reimplementing that crate with helix classes and struct definitions directly, but when I tried to use the serde's derive annotations, I got a parser error when I did this:

ruby! {
  #[derive(Serialize, Deserialize)]
  class Config {
    ...
  }
}

I'd really appreciate some guidance. Having examples to do this would be awesome, ultimately, I wouldn't mind to contribute them myself if I can make that work.

👋

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants