Skip to content

Commit

Permalink
Preserve casing by default (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Feb 10, 2022
1 parent 195c40f commit b2c3f49
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub type Body = serde_bytes::ByteBuf;
pub type ComplexAlias = ComplexGuestToHost;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ComplexGuestToHost {
pub simple: Simple,
pub map: BTreeMap<String, Simple>,
Expand Down Expand Up @@ -34,19 +33,16 @@ pub struct ComplexHostToGuest {
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExplicitedlyImportedType {
pub you_will_see_this: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupImportedType1 {
pub you_will_see_this: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupImportedType2 {
pub you_will_see_this: bool,
}
Expand All @@ -65,7 +61,7 @@ pub struct HttpRequestOptions {
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "PascalCase")]
pub struct Point<T> {
pub value: T,
}
Expand All @@ -88,7 +84,7 @@ pub enum RequestError {
response: Body,
},
/// Misc.
#[serde(rename = "other/misc", rename_all = "camelCase")]
#[serde(rename = "other/misc")]
Other { reason: String },
}

Expand Down Expand Up @@ -124,15 +120,13 @@ pub struct Response {
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Simple {
pub foo: i32,
pub bar: String,
}

/// Tagged dynamic value.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde()]
pub enum Value {
Integer(i64),
Float(f64),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub type Body = serde_bytes::ByteBuf;
pub type ComplexAlias = ComplexGuestToHost;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ComplexGuestToHost {
pub simple: Simple,
pub map: BTreeMap<String, Simple>,
Expand Down Expand Up @@ -38,19 +37,16 @@ pub struct ComplexHostToGuest {
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExplicitedlyImportedType {
pub you_will_see_this: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupImportedType1 {
pub you_will_see_this: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupImportedType2 {
pub you_will_see_this: bool,
}
Expand All @@ -69,7 +65,7 @@ pub struct HttpRequestOptions {
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "PascalCase")]
pub struct Point<T> {
pub value: T,
}
Expand All @@ -92,20 +88,18 @@ pub enum RequestError {
response: Body,
},
/// Misc.
#[serde(rename = "other/misc", rename_all = "camelCase")]
#[serde(rename = "other/misc")]
Other { reason: String },
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Simple {
pub foo: i32,
pub bar: String,
}

/// Tagged dynamic value.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde()]
pub enum Value {
Integer(i64),
Float(f64),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export type ComplexHostToGuest = {
};

export type ExplicitedlyImportedType = {
youWillSeeThis: boolean;
you_will_see_this: boolean;
};

export type GroupImportedType1 = {
youWillSeeThis: boolean;
you_will_see_this: boolean;
};

export type GroupImportedType2 = {
youWillSeeThis: boolean;
you_will_see_this: boolean;
};

/**
Expand All @@ -68,7 +68,7 @@ export type Method =
| "TRACE";

export type Point<T> = {
value: T;
Value: T;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions example/example-protocol/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct DeadCode {
}

#[derive(Serializable)]
#[fp(rename_all = "PascalCase")]
pub struct Point<T> {
pub value: T,
}
Expand All @@ -26,6 +27,7 @@ pub struct Simple {
/// Multi-line doc comment with complex characters
/// & " , \ ! '
#[derive(Serializable)]
#[fp(rename_all = "camelCase")]
pub struct ComplexHostToGuest {
pub simple: Simple,
pub list: Vec<f64>,
Expand Down Expand Up @@ -73,6 +75,7 @@ pub struct RequestOptions {

/// Similar to the `RequestOptions` struct, but using types from the `http` crate.
#[derive(Clone, Debug, Serializable)]
#[fp(rename_all = "camelCase")]
pub struct HttpRequestOptions {
pub url: Uri,
pub method: Method,
Expand Down
4 changes: 4 additions & 0 deletions fp-bindgen/src/casing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::convert::TryFrom;
pub enum Casing {
Original,
CamelCase,
PascalCase,
SnakeCase,
ScreamingSnakeCase,
}
Expand All @@ -14,6 +15,7 @@ impl Casing {
match self {
Self::Original => None,
Self::CamelCase => Some("camelCase"),
Self::PascalCase => Some("PascalCase"),
Self::SnakeCase => Some("snake_case"),
Self::ScreamingSnakeCase => Some("SCREAMING_SNAKE_CASE"),
}
Expand All @@ -23,6 +25,7 @@ impl Casing {
match self {
Self::Original => string.to_owned(),
Self::CamelCase => string.to_camel_case(),
Self::PascalCase => string.to_pascal_case(),
Self::SnakeCase => string.to_snake_case(),
Self::ScreamingSnakeCase => string.to_screaming_snake_case(),
}
Expand All @@ -41,6 +44,7 @@ impl TryFrom<&str> for Casing {
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"camelCase" => Ok(Self::CamelCase),
"PascalCase" => Ok(Self::PascalCase),
"snake_case" => Ok(Self::SnakeCase),
"SCREAMING_SNAKE_CASE" => Ok(Self::ScreamingSnakeCase),
other => Err(format!("Unrecognized case format: {}", other)),
Expand Down
34 changes: 22 additions & 12 deletions fp-bindgen/src/generators/rust_plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,6 @@ fn create_enum_definition(ty: &Enum, types: &TypeMap) -> String {
let fields = fields.join(" ");
format!(" {} ", &fields.trim_end_matches(','))
};
if !serde_attrs
.iter()
.any(|attr| attr.starts_with("rename_all ="))
{
serde_attrs.push("rename_all = \"camelCase\"".to_owned());
}
format!("{} {{{}}},", variant.ident.name, fields)
}
Type::Tuple(items) => {
Expand Down Expand Up @@ -407,14 +401,22 @@ fn create_enum_definition(ty: &Enum, types: &TypeMap) -> String {
.collect::<Vec<_>>()
.join("\n");

let serde_annotation = {
let attrs = ty.options.to_serde_attrs();
if attrs.is_empty() {
"".to_owned()
} else {
format!("#[serde({})]\n", attrs.join(", "))
}
};

format!(
"{}#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]\n\
#[serde({})]\n\
"{}#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]\n{}\
pub enum {} {{\n\
{}\n\
}}",
format_docs(&ty.doc_lines),
ty.options.to_serde_attrs().join(", "),
serde_annotation,
ty.ident,
variants
)
Expand Down Expand Up @@ -442,14 +444,22 @@ fn create_struct_definition(ty: &Struct, types: &TypeMap) -> String {
.collect::<Vec<_>>()
.join("\n");

let serde_annotation = {
let attrs = ty.options.to_serde_attrs();
if attrs.is_empty() {
"".to_owned()
} else {
format!("#[serde({})]\n", attrs.join(", "))
}
};

format!(
"{}#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]\n\
#[serde({})]\n\
"{}#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]\n{}\
pub struct {} {{\n\
{}\n\
}}",
format_docs(&ty.doc_lines),
ty.options.to_serde_attrs().join(", "),
serde_annotation,
ty.ident,
fields.trim_start_matches('\n')
)
Expand Down
3 changes: 0 additions & 3 deletions fp-bindgen/src/types/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ impl StructOptions {
);
}
}
if opts.native_modules.is_empty() && opts.field_casing == Casing::default() {
opts.field_casing = Casing::CamelCase;
}
opts
}

Expand Down

0 comments on commit b2c3f49

Please sign in to comment.