Skip to content

Commit

Permalink
Fixed Utf8Error raise.
Browse files Browse the repository at this point in the history
More shit...
  • Loading branch information
gerdoe-jr committed May 30, 2021
1 parent 33d68cf commit 73b9d40
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tw-econ"
version = "0.1.2"
version = "0.2.0"
authors = ["Vlad <gerdoexx@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
61 changes: 31 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ impl EconMessage {
}
}
}

pub fn to_string(&self) -> String {
format!("[{}][{}]: {}", self.timestamp, self.category, self.content)
}

// requires a good stable runtime-formatter
// wish i will find it
// or do it myself...
pub fn to_string_fmt(&self, format: String) -> String {
unimplemented!();
}
// pub fn to_string_fmt(&self, format: String) -> String {
// unimplemented!();
// }

pub fn get_timestamp(&self) -> NaiveTime {
self.timestamp
Expand Down Expand Up @@ -116,35 +116,36 @@ impl EconConnection {

loop {
let mut buffer: [u8; 1024] = [0; 1024];

match stream.read(&mut buffer) {
Ok(_) => match std::str::from_utf8(&buffer).unwrap() {
words => {
for word in words.to_string().split('\n') {
let word = word.replace("\u{0}", "");
let msg = match EconMessage::from_string(&word) {
Some(m) => {
Some(m)
},
_ => {
if !word.is_empty() {
Some(EconMessage::from_string_with_current_time(&word))
unsafe {
match stream.read(&mut buffer) {
Ok(_) => match std::str::from_utf8_unchecked(&buffer) {
words => {
for word in words.to_string().split('\n') {
let word = word.replace("\u{0}", "");
let msg = match EconMessage::from_string(&word) {
Some(m) => {
Some(m)
},
_ => {
if !word.is_empty() {
Some(EconMessage::from_string_with_current_time(&word))
}
else {
None
}
}
else {
None
}
}
};
};

if let Some(m) = msg {
tx.send(m).expect(&format!("Can't write streambuffer for address: {:}", addr));
if let Some(m) = msg {
tx.send(m).expect(&format!("Can't write streambuffer for address: {:}", addr));
}
}
}
}
},
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => {},
Err(_) => {}
};
},
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => {},
Err(_) => {}
};
}

if let Ok(received) = srx.try_recv() {
match received.as_str() {
Expand All @@ -168,7 +169,7 @@ impl EconConnection {
}
}

pub fn disconnect(&mut self) {
pub fn disconnect(&self) {
let _ = self.stx.send(String::from(":disconnect!"));
let _ = self.tx.send(EconMessage::from_string_with_current_time(&format!("[tw-econ]: Disconnected from '{}'", self.address)));
}
Expand Down
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ struct Arguments {
#[structopt(long, default_value = "127.0.0.1:8303")]
address: String,
#[structopt(long)]
password: String
password: String,
#[structopt(long)]
standard: bool
}

fn main() {
Expand All @@ -19,11 +21,20 @@ fn main() {

loop {
if let Ok(msg) = conn.recv() {
println!("{} : {} ::: {}", msg.get_timestamp(), msg.get_category(), msg.get_content());
if args.standard {
println!("{}", msg.to_string());
}
else {
println!("{} : {} ::: {}", msg.get_timestamp(), msg.get_category(), msg.get_content());
}
}

if let Ok(received) = stdin.try_recv() {
let _ = conn.send(received);
let _ = conn.send(received.clone());

if received == ":q" {
conn.disconnect();
}
}
}
}
Expand Down

0 comments on commit 73b9d40

Please sign in to comment.