1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-13 07:40:05 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
e027f8f713 refactor: fix clippy 2020-09-19 20:12:40 +08:00
20b452656e refactor: ipv4_to_u32 2020-09-19 20:07:49 +08:00
89f45cc354 style: code style format 2020-09-19 20:00:54 +08:00
5 changed files with 20 additions and 40 deletions

View File

@@ -1,7 +1,4 @@
use std::{ use std::{ io::{ self, Error, ErrorKind }, process::Command };
io::{ self, Error, ErrorKind },
process::Command,
};
pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<()> { pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<()> {
cmd.spawn()?.wait()?; cmd.spawn()?.wait()?;

View File

@@ -1,17 +1,5 @@
use std::{ use std::{ env, fs::{ self, File }, io::{ Lines, BufReader }, path::{ Path, PathBuf } };
env, use crate::{ iff, util_os, util_io, util_msg, new_box_ioerror, XResult };
fs::{ self, File },
io::{ Lines, BufReader },
path::{ Path, PathBuf },
};
use crate::{
iff,
util_os,
util_io,
util_msg,
new_box_ioerror,
XResult,
};
pub struct JoinFilesReader { pub struct JoinFilesReader {
files: Vec<String>, files: Vec<String>,
@@ -38,11 +26,7 @@ impl JoinFilesReader {
if !files.is_empty() { if !files.is_empty() {
file_lines = Some(Box::new(open_file_as_lines(&files[0])?)); file_lines = Some(Box::new(open_file_as_lines(&files[0])?));
} }
Ok(Self { Ok(Self { files, file_ptr, file_lines })
files,
file_ptr,
file_lines,
})
} }
} }

View File

@@ -4,7 +4,7 @@ use std::{
time::{ SystemTime, Duration }, time::{ SystemTime, Duration },
}; };
use super::{ XResult, new_box_ioerror, }; use super::{ XResult, new_box_ioerror };
use super::util_size::get_display_size; use super::util_size::get_display_size;
use super::util_msg::print_lastline; use super::util_msg::print_lastline;
use super::util_file::resolve_file_path; use super::util_file::resolve_file_path;

View File

@@ -1,8 +1,4 @@
use std::{ use std::{ env, io::{ self, Write }, sync::{ Arc, Mutex } };
env,
io::{ self, Write },
sync::{ Arc, Mutex },
};
lazy_static! { lazy_static! {
pub static ref IS_ATTY: bool = is_atty(); pub static ref IS_ATTY: bool = is_atty();
@@ -11,7 +7,7 @@ lazy_static! {
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR, } pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR }
impl MessageType { impl MessageType {
pub fn get_u8_value(&self) -> u8 { pub fn get_u8_value(&self) -> u8 {
@@ -26,11 +22,13 @@ impl MessageType {
} }
pub fn get_logger_level() -> MessageType { pub fn get_logger_level() -> MessageType {
if let Some(logger_level) = env::var("LOGGER_LEVEL").ok().or(env::var("LOGGER").ok()).or(env::var("LEVEL").ok()) { if let Some(logger_level) = env::var("LOGGER_LEVEL").ok()
.or_else(|| env::var("LOGGER").ok())
.or_else(|| env::var("LEVEL").ok()) {
match logger_level.trim().to_lowercase().as_str() { match logger_level.trim().to_lowercase().as_str() {
"debug" | "*" => MessageType::DEBUG, "debug" | "*" => MessageType::DEBUG,
"info" | "?" => MessageType::INFO, "info" | "?" => MessageType::INFO,
"ok" => MessageType::OK, "ok" | "#" => MessageType::OK,
"warn" | "!" => MessageType::WARN, "warn" | "!" => MessageType::WARN,
"error" | "^" => MessageType::ERROR, "error" | "^" => MessageType::ERROR,
_ => { _ => {

View File

@@ -1,4 +1,4 @@
use std::fmt::{ Display, Formatter }; use std::fmt::{ self, Display, Formatter };
use std::result::Result; use std::result::Result;
use std::net::SocketAddr; use std::net::SocketAddr;
use crate::XResult; use crate::XResult;
@@ -12,7 +12,7 @@ pub enum IpAddress {
impl IpAddress { impl IpAddress {
pub fn parse_ipv4(addr: &str) -> Option<Self> { pub fn parse_ipv4(addr: &str) -> Option<Self> {
parse_ipv4_addr(addr).map(|parts| IpAddress::Ipv4(parts)) parse_ipv4_addr(addr).map(IpAddress::Ipv4)
} }
pub fn to_address(&self) -> String { pub fn to_address(&self) -> String {
@@ -23,13 +23,13 @@ impl IpAddress {
pub fn is_matches(&self, socket_addr: &SocketAddr) -> bool { pub fn is_matches(&self, socket_addr: &SocketAddr) -> bool {
match self { match self {
IpAddress::Ipv4(self_ipv4_octets) => IpAddressMask::Ipv4(self_ipv4_octets.clone(), 32).is_matches(socket_addr), IpAddress::Ipv4(self_ipv4_octets) => IpAddressMask::Ipv4(*self_ipv4_octets, 32).is_matches(socket_addr),
} }
} }
} }
impl Display for IpAddress { impl Display for IpAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", self.to_address()) write!(f, "{}", self.to_address())
} }
} }
@@ -83,7 +83,7 @@ impl IpAddressMask {
} }
impl Display for IpAddressMask { impl Display for IpAddressMask {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", self.to_address()) write!(f, "{}", self.to_address())
} }
} }
@@ -118,7 +118,7 @@ impl IpAddressMaskGroup {
} }
impl Display for IpAddressMaskGroup { impl Display for IpAddressMaskGroup {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "[{}]", self.ip_address_mask_group.iter().map(|i| format!("{}", i)).collect::<Vec<_>>().join(", ")) write!(f, "[{}]", self.ip_address_mask_group.iter().map(|i| format!("{}", i)).collect::<Vec<_>>().join(", "))
} }
} }
@@ -152,7 +152,7 @@ impl IpAddressAndPort {
} }
impl Display for IpAddressAndPort { impl Display for IpAddressAndPort {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}:{}", self.ip, self.port) write!(f, "{}:{}", self.ip, self.port)
} }
} }
@@ -190,7 +190,8 @@ fn ipv4_mask(mask: u8) -> u32 {
} }
fn ipv4_to_u32(ipv4: &[u8; 4]) -> u32 { fn ipv4_to_u32(ipv4: &[u8; 4]) -> u32 {
((ipv4[0] as u32) << (8 * 3)) + ((ipv4[1] as u32) << (8 * 2)) + ((ipv4[2] as u32) << 8) + (ipv4[3] as u32) u32::from_be_bytes(*ipv4)
// ((ipv4[0] as u32) << (8 * 3)) + ((ipv4[1] as u32) << (8 * 2)) + ((ipv4[2] as u32) << 8) + (ipv4[3] as u32)
} }
fn parse_ipv4_addr(addr: &str) -> Option<[u8; 4]> { fn parse_ipv4_addr(addr: &str) -> Option<[u8; 4]> {