use std::{io::{Read, stdin}, error::Error, fmt};
mod mangaplus;
use mangaplus::parse_mangaplus_proto;
extern crate serde_json;
use serde_json::to_string;
#[derive(Debug)]
pub struct Failure<T: fmt::Debug>(T);
impl<T: fmt::Debug> fmt::Display for Failure<T> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{:#?}", self.0)
    }
}
impl<T: fmt::Debug> Error for Failure<T> {}
fn main() -> Result<(), Box<dyn Error>> {
    let mut data = Vec::with_capacity(0xffff);
    let len = stdin().read_to_end(&mut data)?;
    match parse_mangaplus_proto(&data[..len]) {
        Ok(msg) => {
            print!("{}", to_string(&msg)?);
            Ok(())
        },
        Err(e) => Err(Box::new(Failure(e)))
    }
}
