style: updated main.rs & Readme

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2023-10-02 22:04:41 +03:00
parent cea4e569bc
commit 6a7579e18b
2 changed files with 42 additions and 7 deletions

View file

@ -4,10 +4,12 @@ TCP File Transfer Server and Client in Rust
☑ File transfer over network. ☑ File transfer over network.
☐ Remove memory boundaries. ☐ Remove memory limitations.
☐ Folder transfer. ☐ Folder transfer.
☐ Remember where it stopped. ☐ Remember where it stopped.
☐ Reach over NAT (peer to peer). ☐ Reach over NAT (peer to peer).
☐ Async.

View file

@ -1,4 +1,4 @@
use std::fs::File; use std::fs::{File, Metadata, self};
use std::time::Instant; use std::time::Instant;
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write, self}; use std::io::{Read, Write, self};
@ -10,21 +10,51 @@ struct FileInfo
location:String, location:String,
bytes:Vec<u8>, bytes:Vec<u8>,
size:usize, size:usize,
metadata:Option<Metadata>,
} }
impl FileInfo impl FileInfo
{ {
fn reading_operations(&mut self) fn reading_operations(&mut self)
{
self.read_metadata();
match self.metadata
{
Some(ref mut metadata) =>
{
if Metadata::is_file(metadata)
{ {
self.read_file(); self.read_file();
}
else if Metadata::is_symlink(metadata)
{
self.read_file();
}
else
{
//path recognition and creation on the other side
//std:path
panic!("\n\tError: Folder Transfers've not Supported yet\n")
}
}
None =>
{
}
}
self.file_to_byte(); self.file_to_byte();
} }
fn writing_operations(&mut self) fn writing_operations(&mut self)
{ {
self.write_file(); self.write_file();
} }
fn read_metadata(&mut self)
{
self.metadata = Some(fs::metadata(&self.location).expect("Error: Read Metadata"));
}
fn read_file(&mut self) fn read_file(&mut self)
{ {
self.file = Some(File::open(&self.location).expect("Error: Open File")) self.file = Some(File::open(&self.location).expect("Error: Open File"));
} }
fn file_to_byte(&mut self) fn file_to_byte(&mut self)
{ {
@ -81,7 +111,7 @@ impl Connection
{ {
fn server(self, file_info:&mut FileInfo) fn server(self, file_info:&mut FileInfo)
{ {
print!("Server: "); print!("Server -> ");
let ip:String; let ip:String;
let port:String; let port:String;
let address:String; let address:String;
@ -143,7 +173,7 @@ impl Connection
} }
fn client(self, file_info:&mut FileInfo) fn client(self, file_info:&mut FileInfo)
{ {
print!("Client: "); print!("Client -> ");
let ip:String; let ip:String;
let port:String; let port:String;
let address:String; let address:String;
@ -185,7 +215,7 @@ fn take_string(output:String) -> String
{ {
let mut input = String::new(); let mut input = String::new();
println!("{}", output); println!("{}", output);
io::stdin().read_line(&mut input).expect("Failed to Read from Console"); io::stdin().read_line(&mut input).expect("Error: Failed to Read from Console");
input input
} }
fn take_arg() -> String fn take_arg() -> String
@ -197,6 +227,8 @@ fn take_arg() -> String
fn main() fn main()
{ {
//DONT FORGET
//First we should check folder structure and validation then make connection.
println!("Hello, world!"); println!("Hello, world!");
let bytes:Vec<u8> = vec![]; let bytes:Vec<u8> = vec![];
@ -207,6 +239,7 @@ fn main()
location:take_arg(), location:take_arg(),
bytes:bytes, bytes:bytes,
size:size, size:size,
metadata:None,
}; };
match &take_string("Input: Server 's', Client 'c'".to_string())[0..1] match &take_string("Input: Server 's', Client 'c'".to_string())[0..1]
{ {