json
unknown
rust
3 years ago
3.6 kB
5
Indexable
extern crate serde_json;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use serde_json::Value as JsonValue;
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
struct GPS{
lat: f64,
lon: f64,
}
enum employee_type {
Teacher,
Doctor,
Police
}
#[derive(Serialize, Deserialize)]
struct Teacher{
name: String,
age: i64,
location: GPS,
student: Vec<String>,
}
struct Police{
name: String,
age: i64,
location: GPS,
weapon: Vec<String>,
}
struct Doctor{
name: String,
age: i64,
location: GPS,
patient: Vec<String>,
}
trait Employee{
fn find_location(&self) -> GPS;
fn find_name(&self) -> &String;
fn find_age(&self) -> i64;
fn find_properties(&self) -> &Vec<String>;
}
impl Employee for Teacher{
fn find_location(&self) -> GPS{
self.location
}
fn find_name(&self) -> &String{
&self.name
}
fn find_age(&self) -> i64{
self.age
}
fn find_properties(&self) -> &Vec<String>{
&self.student
}
}
impl Employee for Police{
fn find_location(&self) -> GPS{
self.location
}
fn find_name(&self) -> &String{
&self.name
}
fn find_age(&self) -> i64{
self.age
}
fn find_properties(&self) -> &Vec<String>{
&self.weapon
}
}
impl Employee for Doctor{
fn find_location(&self) -> GPS{
self.location
}
fn find_name(&self) -> &String{
&self.name
}
fn find_age(&self) -> i64{
self.age
}
fn find_properties(&self) -> &Vec<String>{
&self.patient
}
}
impl fmt::Display for Teacher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " Name: {}\n Age: {}\n Student: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon)
}
}
impl fmt::Display for Police {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " Name: {}\n Age: {}\n Weapon: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon)
}
}
impl fmt::Display for Doctor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " Name: {}\n Age: {}\n Patient: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon)
}
}
fn main() {
// let a = Teacher{name: "John".to_string(), age: 65, student: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }};
// println!("{}", a);
// let b = Police{name: "John".to_string(), age: 65, weapon: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }};
// println!("{}", b);
// let c = Doctor{name: "John".to_string(), age: 65, patient: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }};
// println!("{}", c);
let json_str = r#"
{
"name" : "John",
"age" : 65,
"location" : GPS { lat: 8.15154, lon: -584.55 },
"student" : ["a","b","c"]
}
"#;
let res = serde_json::from_str(json_str);
if res.is_ok(){
let p: JsonValue = res.unwrap();
println!("{}", p["name"]);
}else {
println!("error");
}
}Editor is loading...