Решение на Code Identifier от Томислав Николов

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

Към профила на Томислав Николов

Резултати

  • 16 точки от тестове
  • 0 бонус точки
  • 16 точки общо
  • 12 успешни тест(а)
  • 3 неуспешни тест(а)

Код

#[derive(Debug)]
pub struct CodeIdentifier {
id: String,
}
impl CodeIdentifier {
pub fn new(identifier: &str) -> Option<Self> {
let s = identifier.chars();
let mut s1 = String::new();
let mut first: bool = true;
for c in s {
if !c.is_alphabetic() && !c.is_numeric() && c != '_' {
break;
} else if first {
if !c.is_alphabetic() {
break;
}
first = false;
}
s1.push(c);
}
let code = CodeIdentifier {
id: identifier.to_string(),
};
match identifier.len() == s1.len() {
true => Some(code),
false => None,
}

match statement-а е мощен инструмент, но рядко се ползва за булеви стойност -- в тази ситуация, if-клауза ще ти свърши същата работа и ще е по-"класически" метод да branch-неш.

}
pub fn camelcase(&self) -> String {
let mut s = String::new();
let s1 = self.id.chars();
let mut found: bool = false;
for c in s1 {
if found {
if c.is_lowercase() {
let c = c.to_uppercase();
s.push_str(&c.to_string());
} else {
s.push(c);
}
found = false;
} else if c != '_' {
s.push(c);
} else {
found = true;
}
}
s
}
pub fn titlecase(&self) -> String {
let mut s = String::new();
let s1 = self.id.chars();
let mut cap: bool = true;
for c in s1 {
if cap {
if c.is_lowercase() {
let c = c.to_uppercase();
s.push_str(&c.to_string());
} else {
s.push(c);
}
cap = false;
} else if c != '_' {
s.push(c);
} else {
cap = true;
}
}
s
}
pub fn kebabcase(&self) -> String {
let mut s = String::new();
let s1 = self.id.chars();
for c in s1 {
if c == '_' {
s.push('-');
} else {
s.push(c);
}
}
s
}
pub fn underscore(&self) -> String {
let mut s = String::new();
let s1 = self.id.chars();
for c in s1 {
if c.is_alphabetic() && c.is_uppercase() {
let c = c.to_lowercase();
s.push_str(&c.to_string());
} else {
s.push(c);
}
}
s
}
pub fn screaming_snakecase(&self) -> String {
let mut s = String::new();
let s1 = self.id.chars();
for c in s1 {
if c.is_alphabetic() && c.is_lowercase() {
let c = c.to_uppercase();
s.push_str(&c.to_string());
} else {
s.push(c);
}
}
s
}
}

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

Compiling solution v0.1.0 (/tmp/d20190123-22631-rzs3kl/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 4.32s
     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 ... FAILED
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 ... FAILED
test solution_test::test_whitespace ... FAILED

failures:

---- solution_test::test_normalize_case2 stdout ----
thread 'solution_test::test_normalize_case2' panicked at 'assertion failed: `(left == right)`
  left: `"SomeVar"`,
 right: `"someVar"`', tests/solution_test.rs:81:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- solution_test::test_validity stdout ----
thread 'solution_test::test_validity' panicked at 'assertion failed: CodeIdentifier::new(" some_var ").is_some()', tests/solution_test.rs:18:5

---- solution_test::test_whitespace stdout ----
thread 'solution_test::test_whitespace' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:355:21


failures:
    solution_test::test_normalize_case2
    solution_test::test_validity
    solution_test::test_whitespace

test result: FAILED. 12 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--test solution_test'

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

Томислав качи първо решение на 24.10.2018 18:59 (преди почти 7 години)