Решение на упр.04 задача 1 от Милан Стойчев

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

Към профила на Милан Стойчев

Резултати

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

Код

use std::fmt::{Debug, Formatter, Result as FmtResult};
pub trait ToJson {
fn to_json(&self) -> String;
}
#[derive(Debug, Clone)]
pub enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToString for Spec {
fn to_string(&self) -> String {
match self {
Spec::SI => "SI".into(),
Spec::IS => "IS".into(),
Spec::KN => "KN".into(),
Spec::I => "I".into(),
Spec::M => "M".into(),
}
}
}
#[derive(Debug, Clone)]
pub enum Title {
Assistant,
Doctor,
Professor,
}
impl ToString for Title {
fn to_string(&self) -> String {
match self {
Title::Assistant => "Assistant".into(),
Title::Doctor => "Doctor".into(),
Title::Professor => "Professor".into(),
}
}
}
#[derive(Clone)]
pub struct Student {
pub name: String,
pub age: u32,
pub spec: Spec,
}
impl Student {
pub fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
impl Debug for Student {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Student")
.field("name", &self.name)
.field("age", &self.age)
.field("spec", &self.spec)
.finish()
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":\"{}\"}}",
self.name,
self.age,
self.spec.to_string()
)
}
}
#[derive(Clone)]
pub struct Teacher {
pub name: String,
pub age: u32,
pub specs: Vec<Spec>,
pub title: Title,
}
impl Teacher {
pub fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Self {
name,
age,
specs,
title,
}
}
}
impl Debug for Teacher {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Teacher")
.field("name", &self.name)
.field("age", &self.age)
.field("specs", &self.specs)
.field("title", &self.title)
.finish()
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json = self
.specs
.iter()
.map(|s| format!("\"{}\"", s.to_string()))
.collect::<Vec<_>>()
.join(",");
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":[{}],\"title\":\"{}\"}}",
self.name,
self.age,
specs_json,
self.title.to_string()
)
}
}
#[derive(Debug)]
pub struct University<T: ToJson + Debug + Clone> {
pub name: String,
pub students: Vec<Student>,
pub teachers: Vec<T>,
}
impl<T: ToJson + Debug + Clone> University<T> {
pub fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
Self {
name,
students,
teachers,
}
}
fn indent(level: usize) -> String {
" ".repeat(level)
}
}
impl<T: ToJson + Debug + Clone> ToJson for University<T> {
fn to_json(&self) -> String {
let students_lines = self
.students
.iter()
.map(|s| {
format!(
"{}{{\"name\": \"{}\", \"age\": {}, \"spec\": \"{}\"}}",
University::<T>::indent(2),
s.name,
s.age,
s.spec.to_string(),
)
})
.collect::<Vec<_>>()
.join(",\n");
let teachers_lines = self
.teachers
.iter()
.map(|t| {
let compact = t.to_json();
format!("{}{}", University::<T>::indent(2), compact)
})
.collect::<Vec<_>>()
.join(",\n");
format!(
"{{\n{}\"name\": \"{}\",\n{}\"students\": [\n{}\n{}],\n{}\"teachers\": [\n{}\n{}]\n}}",
University::<T>::indent(1),
self.name,
University::<T>::indent(1),
students_lines,
University::<T>::indent(1),
University::<T>::indent(1),
teachers_lines,
University::<T>::indent(1),
)
}
}

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

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.41
   Compiling futures-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling syn v2.0.109
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1gphgud/solution)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: variants `KN`, `I`, and `M` are never constructed
  --> tests/../src/lib.rs:11:5
   |
8  | pub enum Spec {
   |          ---- variants in this enum
...
11 |     KN,
   |     ^^
12 |     I,
   |     ^
13 |     M,
   |     ^
   |
   = note: `Spec` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

warning: variants `Assistant` and `Doctor` are never constructed
  --> tests/../src/lib.rs:30:5
   |
29 | pub enum Title {
   |          ----- variants in this enum
30 |     Assistant,
   |     ^^^^^^^^^
31 |     Doctor,
   |     ^^^^^^
   |
   = note: `Title` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis

warning: `solution` (test "solution_test") generated 2 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.13s
     Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)

running 2 tests
test solution_test::test_basic2 ... ok
test solution_test::test_basic ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

Милан качи първо решение на 04.11.2025 23:44 (преди около 1 месеца)

Милан качи решение на 04.11.2025 23:45 (преди около 1 месеца)

use std::fmt::{Debug, Formatter, Result as FmtResult};
pub trait ToJson {
fn to_json(&self) -> String;
}
-// ---------------- ENUM Spec ----------------
-
#[derive(Debug, Clone)]
pub enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToString for Spec {
fn to_string(&self) -> String {
match self {
Spec::SI => "SI".into(),
Spec::IS => "IS".into(),
Spec::KN => "KN".into(),
Spec::I => "I".into(),
Spec::M => "M".into(),
}
}
}
-// ---------------- ENUM Title ----------------
-
#[derive(Debug, Clone)]
pub enum Title {
Assistant,
Doctor,
Professor,
}
impl ToString for Title {
fn to_string(&self) -> String {
match self {
Title::Assistant => "Assistant".into(),
Title::Doctor => "Doctor".into(),
Title::Professor => "Professor".into(),
}
}
}
-
-// ---------------- STRUCT Student ----------------
#[derive(Clone)]
pub struct Student {
pub name: String,
pub age: u32,
pub spec: Spec,
}
impl Student {
pub fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
impl Debug for Student {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Student")
.field("name", &self.name)
.field("age", &self.age)
.field("spec", &self.spec)
.finish()
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":\"{}\"}}",
self.name,
self.age,
self.spec.to_string()
)
}
}
#[derive(Clone)]
pub struct Teacher {
pub name: String,
pub age: u32,
pub specs: Vec<Spec>,
pub title: Title,
}
impl Teacher {
pub fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Self {
name,
age,
specs,
title,
}
}
}
impl Debug for Teacher {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Teacher")
.field("name", &self.name)
.field("age", &self.age)
.field("specs", &self.specs)
.field("title", &self.title)
.finish()
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json = self
.specs
.iter()
.map(|s| format!("\"{}\"", s.to_string()))
.collect::<Vec<_>>()
.join(",");
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":[{}],\"title\":\"{}\"}}",
self.name,
self.age,
specs_json,
self.title.to_string()
)
}
}
#[derive(Debug)]
pub struct University<T: ToJson + Debug + Clone> {
pub name: String,
pub students: Vec<Student>,
pub teachers: Vec<T>,
}
impl<T: ToJson + Debug + Clone> University<T> {
pub fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
Self {
name,
students,
teachers,
}
}
fn indent(level: usize) -> String {
" ".repeat(level)
}
}
impl<T: ToJson + Debug + Clone> ToJson for University<T> {
fn to_json(&self) -> String {
let students_lines = self
.students
.iter()
.map(|s| {
format!(
"{}{{\"name\": \"{}\", \"age\": {}, \"spec\": \"{}\"}}",
University::<T>::indent(2),
s.name,
s.age,
s.spec.to_string(),
)
})
.collect::<Vec<_>>()
.join(",\n");
let teachers_lines = self
.teachers
.iter()
.map(|t| {
let compact = t.to_json();
format!("{}{}", University::<T>::indent(2), compact)
})
.collect::<Vec<_>>()
.join(",\n");
format!(
"{{\n{}\"name\": \"{}\",\n{}\"students\": [\n{}\n{}],\n{}\"teachers\": [\n{}\n{}]\n}}",
University::<T>::indent(1),
self.name,
University::<T>::indent(1),
students_lines,
University::<T>::indent(1),
University::<T>::indent(1),
teachers_lines,
University::<T>::indent(1),
)
}
}