//! Crate for changing case of Rust identifiers. //! //! # Features //! * Supports `snake_case`, `lowercase`, `camelCase`, //! `PascalCase`, `SCREAMING_SNAKE_CASE`, and `kebab-case` //! * Rename variants, and fields //! //! # Examples //! ```rust //! use ident_case::RenameRule; //! //! assert_eq!("helloWorld", RenameRule::CamelCase.apply_to_field("hello_world")); //! //! assert_eq!("i_love_serde", RenameRule::SnakeCase.apply_to_variant("ILoveSerde")); //! ```
// Copyright 2017 Serde Developers // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
use std::ascii::AsciiExt; use std::str::FromStr;
useself::RenameRule::*;
/// A casing rule for renaming Rust identifiers. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pubenum RenameRule { /// No-op rename rule.
None, /// Rename direct children to "lowercase" style.
LowerCase, /// Rename direct children to "PascalCase" style, as typically used for enum variants.
PascalCase, /// Rename direct children to "camelCase" style.
CamelCase, /// Rename direct children to "snake_case" style, as commonly used for fields.
SnakeCase, /// Rename direct children to "SCREAMING_SNAKE_CASE" style, as commonly used for constants.
ScreamingSnakeCase, /// Rename direct children to "kebab-case" style.
KebabCase,
}
impl RenameRule { /// Change case of a `PascalCase` variant. pubfn apply_to_variant<S: AsRef<str>>(&self, variant: S) -> String {
let variant = variant.as_ref(); match *self {
None | PascalCase => variant.to_owned(),
LowerCase => variant.to_ascii_lowercase(),
CamelCase => variant[..1].to_ascii_lowercase() + &variant[1..],
SnakeCase => { letmut snake = String::new(); for (i, ch) in variant.char_indices() { if i > 0 && ch.is_uppercase() {
snake.push('_');
}
snake.push(ch.to_ascii_lowercase());
}
snake
}
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
}
}
/// Change case of a `snake_case` field. pubfn apply_to_field<S: AsRef<str>>(&self, field: S) -> String {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.