serde-macros: Add M_MATRIX_ERROR_CASE casing rename rule
This commit is contained in:
parent
461f856b5a
commit
f16d7b67c3
@ -208,72 +208,40 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Add `M_FOO_BAR` as a naming scheme in StringEnum and remove rename attributes.
|
|
||||||
#[derive(FromString, DeserializeFromCowStr)]
|
#[derive(FromString, DeserializeFromCowStr)]
|
||||||
|
#[ruma_enum(rename_all = "M_MATRIX_ERROR_CASE")]
|
||||||
enum ErrCode {
|
enum ErrCode {
|
||||||
#[ruma_enum(rename = "M_FORBIDDEN")]
|
|
||||||
Forbidden,
|
Forbidden,
|
||||||
#[ruma_enum(rename = "M_UNKNOWN_TOKEN")]
|
|
||||||
UnknownToken,
|
UnknownToken,
|
||||||
#[ruma_enum(rename = "M_MISSING_TOKEN")]
|
|
||||||
MissingToken,
|
MissingToken,
|
||||||
#[ruma_enum(rename = "M_BAD_JSON")]
|
|
||||||
BadJson,
|
BadJson,
|
||||||
#[ruma_enum(rename = "M_NOT_JSON")]
|
|
||||||
NotJson,
|
NotJson,
|
||||||
#[ruma_enum(rename = "M_NOT_FOUND")]
|
|
||||||
NotFound,
|
NotFound,
|
||||||
#[ruma_enum(rename = "M_LIMIT_EXCEEDED")]
|
|
||||||
LimitExceeded,
|
LimitExceeded,
|
||||||
#[ruma_enum(rename = "M_UNKNOWN")]
|
|
||||||
Unknown,
|
Unknown,
|
||||||
#[ruma_enum(rename = "M_UNRECOGNIZED")]
|
|
||||||
Unrecognized,
|
Unrecognized,
|
||||||
#[ruma_enum(rename = "M_UNAUTHORIZED")]
|
|
||||||
Unauthorized,
|
Unauthorized,
|
||||||
#[ruma_enum(rename = "M_USER_DEACTIVATED")]
|
|
||||||
UserDeactivated,
|
UserDeactivated,
|
||||||
#[ruma_enum(rename = "M_USER_IN_USE")]
|
|
||||||
UserInUse,
|
UserInUse,
|
||||||
#[ruma_enum(rename = "M_INVALID_USERNAME")]
|
|
||||||
InvalidUsername,
|
InvalidUsername,
|
||||||
#[ruma_enum(rename = "M_ROOM_IN_USE")]
|
|
||||||
RoomInUse,
|
RoomInUse,
|
||||||
#[ruma_enum(rename = "M_INVALID_ROOM_STATE")]
|
|
||||||
InvalidRoomState,
|
InvalidRoomState,
|
||||||
#[ruma_enum(rename = "M_THREEPID_IN_USE")]
|
|
||||||
ThreepidInUse,
|
ThreepidInUse,
|
||||||
#[ruma_enum(rename = "M_THREEPID_NOT_FOUND")]
|
|
||||||
ThreepidNotFound,
|
ThreepidNotFound,
|
||||||
#[ruma_enum(rename = "M_THREEPID_AUTH_FAILED")]
|
|
||||||
ThreepidAuthFailed,
|
ThreepidAuthFailed,
|
||||||
#[ruma_enum(rename = "M_THREEPID_DENIED")]
|
|
||||||
ThreepidDenied,
|
ThreepidDenied,
|
||||||
#[ruma_enum(rename = "M_SERVER_NOT_TRUSTED")]
|
|
||||||
ServerNotTrusted,
|
ServerNotTrusted,
|
||||||
#[ruma_enum(rename = "M_UNSUPPORTED_ROOM_VERSION")]
|
|
||||||
UnsupportedRoomVersion,
|
UnsupportedRoomVersion,
|
||||||
#[ruma_enum(rename = "M_INCOMPATIBLE_ROOM_VERSION")]
|
|
||||||
IncompatibleRoomVersion,
|
IncompatibleRoomVersion,
|
||||||
#[ruma_enum(rename = "M_BAD_STATE")]
|
|
||||||
BadState,
|
BadState,
|
||||||
#[ruma_enum(rename = "M_GUEST_ACCESS_FORBIDDEN")]
|
|
||||||
GuestAccessForbidden,
|
GuestAccessForbidden,
|
||||||
#[ruma_enum(rename = "M_CAPTCHA_NEEDED")]
|
|
||||||
CaptchaNeeded,
|
CaptchaNeeded,
|
||||||
#[ruma_enum(rename = "M_CAPTCHA_INVALID")]
|
|
||||||
CaptchaInvalid,
|
CaptchaInvalid,
|
||||||
#[ruma_enum(rename = "M_MISSING_PARAM")]
|
|
||||||
MissingParam,
|
MissingParam,
|
||||||
#[ruma_enum(rename = "M_INVALID_PARAM")]
|
|
||||||
InvalidParam,
|
InvalidParam,
|
||||||
#[ruma_enum(rename = "M_TOO_LARGE")]
|
|
||||||
TooLarge,
|
TooLarge,
|
||||||
#[ruma_enum(rename = "M_EXCLUSIVE")]
|
|
||||||
Exclusive,
|
Exclusive,
|
||||||
#[ruma_enum(rename = "M_RESOURCE_LIMIT_EXCEEDED")]
|
|
||||||
ResourceLimitExceeded,
|
ResourceLimitExceeded,
|
||||||
#[ruma_enum(rename = "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM")]
|
|
||||||
CannotLeaveServerNoticeRoom,
|
CannotLeaveServerNoticeRoom,
|
||||||
_Custom(String),
|
_Custom(String),
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,9 @@ pub enum RenameRule {
|
|||||||
KebabCase,
|
KebabCase,
|
||||||
/// Rename direct children to "SCREAMING-KEBAB-CASE" style.
|
/// Rename direct children to "SCREAMING-KEBAB-CASE" style.
|
||||||
ScreamingKebabCase,
|
ScreamingKebabCase,
|
||||||
|
/// Rename direct children to "M_MATRIX_ERROR_CASE" style, as used for responses with error in
|
||||||
|
/// Matrix spec.
|
||||||
|
MatrixErrorCase,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenameRule {
|
impl RenameRule {
|
||||||
@ -56,6 +59,7 @@ impl RenameRule {
|
|||||||
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
|
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
|
||||||
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-"),
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||||
|
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_variant(variant),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +91,7 @@ impl RenameRule {
|
|||||||
ScreamingSnakeCase => field.to_ascii_uppercase(),
|
ScreamingSnakeCase => field.to_ascii_uppercase(),
|
||||||
KebabCase => field.replace('_', "-"),
|
KebabCase => field.replace('_', "-"),
|
||||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
||||||
|
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_field(field),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,6 +109,7 @@ impl FromStr for RenameRule {
|
|||||||
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
|
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
|
||||||
"kebab-case" => Ok(KebabCase),
|
"kebab-case" => Ok(KebabCase),
|
||||||
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
|
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
|
||||||
|
"M_MATRIX_ERROR_CASE" => Ok(MatrixErrorCase),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,8 +117,19 @@ impl FromStr for RenameRule {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_variants() {
|
fn rename_variants() {
|
||||||
for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab) in &[
|
for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab, matrix_error) in
|
||||||
("Outcome", "outcome", "OUTCOME", "outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
&[
|
||||||
|
(
|
||||||
|
"Outcome",
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"outcome",
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"M_OUTCOME",
|
||||||
|
),
|
||||||
(
|
(
|
||||||
"VeryTasty",
|
"VeryTasty",
|
||||||
"verytasty",
|
"verytasty",
|
||||||
@ -122,10 +139,12 @@ fn rename_variants() {
|
|||||||
"VERY_TASTY",
|
"VERY_TASTY",
|
||||||
"very-tasty",
|
"very-tasty",
|
||||||
"VERY-TASTY",
|
"VERY-TASTY",
|
||||||
|
"M_VERY_TASTY",
|
||||||
),
|
),
|
||||||
("A", "a", "A", "a", "a", "A", "a", "A"),
|
("A", "a", "A", "a", "a", "A", "a", "A", "M_A"),
|
||||||
("Z42", "z42", "Z42", "z42", "z42", "Z42", "z42", "Z42"),
|
("Z42", "z42", "Z42", "z42", "z42", "Z42", "z42", "Z42", "M_Z42"),
|
||||||
] {
|
]
|
||||||
|
{
|
||||||
assert_eq!(None.apply_to_variant(original), original);
|
assert_eq!(None.apply_to_variant(original), original);
|
||||||
assert_eq!(LowerCase.apply_to_variant(original), lower);
|
assert_eq!(LowerCase.apply_to_variant(original), lower);
|
||||||
assert_eq!(Uppercase.apply_to_variant(original), upper);
|
assert_eq!(Uppercase.apply_to_variant(original), upper);
|
||||||
@ -135,13 +154,14 @@ fn rename_variants() {
|
|||||||
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
|
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
|
||||||
assert_eq!(KebabCase.apply_to_variant(original), kebab);
|
assert_eq!(KebabCase.apply_to_variant(original), kebab);
|
||||||
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
|
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
|
||||||
|
assert_eq!(MatrixErrorCase.apply_to_variant(original), matrix_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_fields() {
|
fn rename_fields() {
|
||||||
for &(original, upper, pascal, camel, screaming, kebab, screaming_kebab) in &[
|
for &(original, upper, pascal, camel, screaming, kebab, screaming_kebab, matrix_error) in &[
|
||||||
("outcome", "OUTCOME", "Outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
("outcome", "OUTCOME", "Outcome", "outcome", "OUTCOME", "outcome", "OUTCOME", "M_OUTCOME"),
|
||||||
(
|
(
|
||||||
"very_tasty",
|
"very_tasty",
|
||||||
"VERY_TASTY",
|
"VERY_TASTY",
|
||||||
@ -150,9 +170,10 @@ fn rename_fields() {
|
|||||||
"VERY_TASTY",
|
"VERY_TASTY",
|
||||||
"very-tasty",
|
"very-tasty",
|
||||||
"VERY-TASTY",
|
"VERY-TASTY",
|
||||||
|
"M_VERY_TASTY",
|
||||||
),
|
),
|
||||||
("a", "A", "A", "a", "A", "a", "A"),
|
("a", "A", "A", "a", "A", "a", "A", "M_A"),
|
||||||
("z42", "Z42", "Z42", "z42", "Z42", "z42", "Z42"),
|
("z42", "Z42", "Z42", "z42", "Z42", "z42", "Z42", "M_Z42"),
|
||||||
] {
|
] {
|
||||||
assert_eq!(None.apply_to_field(original), original);
|
assert_eq!(None.apply_to_field(original), original);
|
||||||
assert_eq!(Uppercase.apply_to_field(original), upper);
|
assert_eq!(Uppercase.apply_to_field(original), upper);
|
||||||
@ -162,5 +183,6 @@ fn rename_fields() {
|
|||||||
assert_eq!(ScreamingSnakeCase.apply_to_field(original), screaming);
|
assert_eq!(ScreamingSnakeCase.apply_to_field(original), screaming);
|
||||||
assert_eq!(KebabCase.apply_to_field(original), kebab);
|
assert_eq!(KebabCase.apply_to_field(original), kebab);
|
||||||
assert_eq!(ScreamingKebabCase.apply_to_field(original), screaming_kebab);
|
assert_eq!(ScreamingKebabCase.apply_to_field(original), screaming_kebab);
|
||||||
|
assert_eq!(MatrixErrorCase.apply_to_field(original), matrix_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user