Commit a6056d3d authored by Chunchi Che's avatar Chunchi Che

add deck.rs

parent fc97e8fb
......@@ -11,3 +11,5 @@ warp = "0.3"
anyhow = "1"
log = "0.4"
pretty_env_logger = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
#created by ygopro2
#main
14124483
9411399
9411399
18094166
18094166
18094166
40044918
40044918
59392529
50720316
50720316
27780618
27780618
16605586
16605586
22865492
22865492
23434538
23434538
14558127
14558127
13650422
83965310
81439173
8949584
8949584
32807846
52947044
45906428
24094653
21143940
21143940
21143940
48130397
24224830
24224830
12071500
24299458
24299458
10045474
#extra
86165817
41209827
29095552
40854197
60461804
60461804
22908820
58481572
58481572
89870349
46759931
63813056
1948619
58004362
58004362
!side
27204311
34267821
34267821
43534808
43534808
94145021
94145021
18144506
54693926
54693926
43898403
43898403
65681983
23002292
//! Deck reader and struct
use std::{
fs::File,
io::{BufRead, BufReader},
path::Path,
};
#[derive(serde::Serialize, Default, Debug)]
pub struct Deck {
pub main: Vec<i32>,
pub extra: Vec<i32>,
pub side: Vec<i32>,
}
impl Deck {
pub fn from_path(p: impl AsRef<Path>) -> anyhow::Result<Self> {
let mut deck = Deck::default();
let f = File::open(p)?;
let reader = BufReader::new(f);
let mut flag = -1;
for line in reader.lines() {
let line = line?;
match line.as_str() {
"#main" => {
flag = 1;
}
"#extra" => {
flag = 2;
}
"!side" => {
flag = 3;
}
_ => {
if let Ok(code) = line.parse::<i32>() {
if code > 100 {
match flag {
1 => deck.main.push(code),
2 => deck.extra.push(code),
3 => deck.side.push(code),
_ => {}
}
}
}
}
}
}
Ok(deck)
}
}
use std::{net::SocketAddr, str::FromStr};
use warp::Filter;
mod deck;
const ADDR: &str = "127.0.0.1:3030";
#[tokio::main]
......@@ -14,11 +16,15 @@ async fn main() -> anyhow::Result<()> {
.allow_header("content-type")
.allow_method("GET");
let hello = warp::path!("hello" / String)
.map(|name| format!("hello, {}", name))
.with(cors);
let deck = warp::path!("deck" / String).map(deck_service).with(cors);
warp::serve(hello).run(SocketAddr::from_str(ADDR)?).await;
warp::serve(deck).run(SocketAddr::from_str(ADDR)?).await;
Ok(())
}
fn deck_service(param: String) -> String {
let deck = deck::Deck::from_path(format!("deck/{}", param)).unwrap_or_default();
let json = serde_json::to_string(&deck).unwrap_or_default();
json
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment