Решение на Code Identifier от Даниела Русева

Обратно към всички решения

Към профила на Даниела Русева

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

#[derive(Debug)]
pub struct CodeIdentifier {
identifier: String,
}
fn is_valid_identifier(identifier: &str) -> bool {
if identifier.is_empty() {
return false
}
let characters:Vec<char> = identifier.chars().collect();
if !characters[0].is_alphabetic() {
return false
}
for c in identifier.chars() {
if !(c.is_alphabetic() | c.is_numeric() | (c == '_')) {
return false
}
}
true
}

Напълно смислена helper функция. Само че не е нужно да има pub тук, понеже ние няма да я викаме -- напълно е достатъчно просто fn is_valid_identifier, за да си я ползваш в рамките на този модул. Същото и за normalize по-долу.

fn normalise(identifier: &str) -> String {
String::from(identifier).trim().to_lowercase()
}
impl CodeIdentifier {
pub fn new(identifier: &str) -> Option<Self> {
let normalised = normalise(identifier);
if is_valid_identifier(&normalised) {
Some(CodeIdentifier { identifier: normalised })
} else {
None
}
}
pub fn camelcase(&self) -> String {
let words: Vec<&str> = self.identifier.split('_').collect();
let mut result = String::from(words[0]);
for w in &words[1..] {
let characters:Vec<char> = w.chars().collect();
let uppercase_first: String = characters[0].to_uppercase().collect();
let rest_of_word: String = characters[1..].iter().collect();
result = format!("{}{}{}", result, uppercase_first, rest_of_word);
}
result
}
pub fn titlecase(&self) -> String {
let words: Vec<&str> = self.identifier.split('_').collect();
let mut result = String::from("");
for w in &words {
let characters: Vec<char> = w.chars().collect();
let uppercase_first: String = characters[0].to_uppercase().collect();
let rest_of_word: String = characters[1..].iter().collect();
result = format!("{}{}{}", result, uppercase_first, rest_of_word);
}
result
}
pub fn kebabcase(&self) -> String {
self.identifier.replace("_", "-")
}
pub fn underscore(&self) -> String {
self.identifier.clone()
}
pub fn screaming_snakecase(&self) -> String {
self.identifier.to_uppercase()
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20190123-22631-p3s18z/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 5.70s
     Running target/debug/deps/solution-2e785d603b538f71

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-29808948fb50ed3a

running 15 tests
test solution_test::test_both_static_and_dynamic_strings ... ok
test solution_test::test_camelcase_basic ... ok
test solution_test::test_cyrillic1 ... ok
test solution_test::test_digits1 ... ok
test solution_test::test_digits2 ... ok
test solution_test::test_digits3 ... ok
test solution_test::test_kebabcase_basic ... ok
test solution_test::test_multibyte_uppercase ... ok
test solution_test::test_normalize_case1 ... ok
test solution_test::test_normalize_case2 ... ok
test solution_test::test_screaming_snakecase_basic ... ok
test solution_test::test_titlecase_basic ... ok
test solution_test::test_underscore_basic ... ok
test solution_test::test_validity ... ok
test solution_test::test_whitespace ... ok

test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

История (2 версии и 1 коментар)

Даниела качи първо решение на 23.10.2018 02:19 (преди над 4 години)

Даниела качи решение на 25.10.2018 14:25 (преди над 4 години)

#[derive(Debug)]
pub struct CodeIdentifier {
identifier: String,
}
-pub fn is_valid_identifier(identifier: &str) -> bool {
- let letters:Vec<char> = identifier.chars().collect();
- if !letters[0].is_alphabetic() {
+fn is_valid_identifier(identifier: &str) -> bool {
+ if identifier.is_empty() {
return false
}
+ let characters:Vec<char> = identifier.chars().collect();
+ if !characters[0].is_alphabetic() {
+ return false
+ }
for c in identifier.chars() {
if !(c.is_alphabetic() | c.is_numeric() | (c == '_')) {
return false
}
}
true
}
-pub fn normalise(identifier: &str) -> String {
+fn normalise(identifier: &str) -> String {
String::from(identifier).trim().to_lowercase()
}
impl CodeIdentifier {
pub fn new(identifier: &str) -> Option<Self> {
let normalised = normalise(identifier);
if is_valid_identifier(&normalised) {
Some(CodeIdentifier { identifier: normalised })
} else {
None
}
}
pub fn camelcase(&self) -> String {
let words: Vec<&str> = self.identifier.split('_').collect();
let mut result = String::from(words[0]);
for w in &words[1..] {
- let letters:Vec<char> = w.chars().collect();
- let uppercase_first: String = letters[0].to_uppercase().collect();
- let rest_of_word: String = letters[1..].iter().collect();
- result = result + &uppercase_first + &rest_of_word;
+ let characters:Vec<char> = w.chars().collect();
+ let uppercase_first: String = characters[0].to_uppercase().collect();
+ let rest_of_word: String = characters[1..].iter().collect();
+ result = format!("{}{}{}", result, uppercase_first, rest_of_word);
}
result
}
pub fn titlecase(&self) -> String {
let words: Vec<&str> = self.identifier.split('_').collect();
let mut result = String::from("");
for w in &words {
- let letters: Vec<char> = w.chars().collect();
- let uppercase_first: String = letters[0].to_uppercase().collect();
- let rest_of_word: String = letters[1..].iter().collect();
- result = result + &uppercase_first + &rest_of_word;
+ let characters: Vec<char> = w.chars().collect();
+ let uppercase_first: String = characters[0].to_uppercase().collect();
+ let rest_of_word: String = characters[1..].iter().collect();
+ result = format!("{}{}{}", result, uppercase_first, rest_of_word);
}
result
}
pub fn kebabcase(&self) -> String {
self.identifier.replace("_", "-")
}
pub fn underscore(&self) -> String {
- self.identifier.to_lowercase()
+ self.identifier.clone()
}
pub fn screaming_snakecase(&self) -> String {
self.identifier.to_uppercase()
}
}