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

before not working as expected #342

Open
morgante opened this issue May 18, 2024 · 0 comments
Open

before not working as expected #342

morgante opened this issue May 18, 2024 · 0 comments

Comments

@morgante
Copy link
Contributor

---
level: error
---

# Use CamelCase Serialialization

We primarily use TypeScript on the other end, so all serialized structs should be in CamelCase.

```grit
language rust

`#[derive($derive)]` as $attr where {
  $derive <: contains `Serialize`,
  $attr <: not before `#[serde(rename_all = "camelCase")]`,
  $attr += `#[serde(rename_all = "camelCase")]`
}

Basic Example

Before:

#[cfg_attr(feature = "napi", napi_derive::napi(object))]
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct FileDiff {
    pub old_path: Option<String>,
    pub new_path: Option<String>,
    pub ranges: Vec<RangePair>,
}

Fixed:

#[cfg_attr(feature = "napi", napi_derive::napi(object))]
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FileDiff {
    pub old_path: Option<String>,
    pub new_path: Option<String>,
    pub ranges: Vec<RangePair>,
}

Countercase

If you need to serialize a struct in a different case, you can use the #[serde(rename_all = "camelCase")] attribute.

#[cfg_attr(feature = "napi", napi_derive::napi(object))]
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FileDiff {
    pub oldPath: Option<String>,
    pub newPath: Option<String>,
    pub ranges: Vec<RangePair>,
}
#[cfg_attr(feature = "napi", napi_derive::napi(object))]
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FileDiff {
    pub oldPath: Option<String>,
    pub newPath: Option<String>,
    pub ranges: Vec<RangePair>,
}
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