diff --git a/Cargo.toml b/Cargo.toml index 511f992..a54e4da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ edition = "2021" env_logger = "0.11.3" log = "0.4.22" sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] } +serde = {version="1.0.204",features=["derive"]} +serde_json = "1.0.120" # serde = "1.0.204" # serde_json = "1.0.120" tokio = {version = "1.38.1",features = ["full"]} diff --git a/src/main.rs b/src/main.rs index ba3e802..594e69d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ -use std::error::Error; +use std::{error::Error, process::exit}; use start_db::Starter; use log::{error, info}; +use word_entity::Model as WordModel; +use parse_json::json_object::Word as WordJsonObject; // use serde_json::json; use tokio; pub mod word_entity; pub mod start_db; +pub mod parse_json; #[tokio::main] async fn main() -> Result<(), Box> { @@ -17,9 +20,22 @@ async fn main() -> Result<(), Box> { db_info.create_table().await.unwrap_or_else(|e: Box|{ info!("The table have been created. Don't care this Error!!!!!:{}",e); }); - word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e|{ + word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e: Box|{ error!("Cannot write data into database.The details are as follows:\n{}",e); }); + let parsed_object: Vec = match parse_json::parse("word.json").await { + Ok(o)=>{ + o + } + Err(e)=>{ + error!("Error to parse the object"); + error!("{}",e); + exit(1); + } + }; - Ok(()) + info!("parsed object Ok!"); + info!("object[0]={:?}",parsed_object[0]); + + return Ok(()); } diff --git a/src/parse_json.rs b/src/parse_json.rs new file mode 100644 index 0000000..fecc7a2 --- /dev/null +++ b/src/parse_json.rs @@ -0,0 +1,17 @@ +use std::error::Error; +use log::info; +use serde::de::DeserializeOwned; +use tokio::{fs::File, io::AsyncReadExt}; + +pub mod json_object; + +pub async fn parse(path: &str) -> Result> +where + T: DeserializeOwned, +{ + let mut json_string: String = String::new(); + let json_size: usize = File::open(path).await?.read_to_string(&mut json_string).await?; + info!("From file {path} read {json_size} bytes"); + let json_object: T = serde_json::from_str(&json_string)?; + Ok(json_object) +} diff --git a/src/parse_json/json_object.rs b/src/parse_json/json_object.rs new file mode 100644 index 0000000..80f1f07 --- /dev/null +++ b/src/parse_json/json_object.rs @@ -0,0 +1,11 @@ +use serde::{Serialize,Deserialize}; + +#[derive(Clone, Debug,Serialize,Deserialize)] +pub struct Word{ + #[serde(skip)] + pub id: i32, + pub bsm: String, + pub hz:String, + #[serde(alias="pyJs")] + pub py_js: Vec, +} \ No newline at end of file diff --git a/src/word_entity.rs b/src/word_entity.rs index fadb289..1f0db79 100644 --- a/src/word_entity.rs +++ b/src/word_entity.rs @@ -1,13 +1,16 @@ use sea_orm::entity::prelude::*; +use serde::{Deserialize,Serialize}; pub mod tests; -#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel,Serialize,Deserialize)] #[sea_orm(table_name = "words")] pub struct Model{ #[sea_orm(primary_key)] + #[serde(skip)] pub id: i32, pub bsm: String, pub hz:String, + #[serde(alias="pyJs")] pub py_js: String, }