From c3962d8808b501806b67b3c00431714399b26e40 Mon Sep 17 00:00:00 2001 From: Horhik Date: Fri, 12 Mar 2021 07:49:51 +0300 Subject: [PATCH] change file structure --- .#README.md | 1 + README.md | 5 ++ src/{types => }/client/fcp_request.rs | 0 src/{types => }/client/fcp_types.rs | 73 +++++++++++---------------- src/{types => }/client/mod.rs | 0 src/{types => }/client/types.rs | 34 +------------ src/lib.rs | 28 ++-------- src/{types => }/node/mod.rs | 0 src/types/mod.rs | 57 ++++++++++++++++++++- src/types/traits.rs | 28 ++++++++++ 10 files changed, 122 insertions(+), 104 deletions(-) create mode 120000 .#README.md rename src/{types => }/client/fcp_request.rs (100%) rename src/{types => }/client/fcp_types.rs (93%) rename src/{types => }/client/mod.rs (100%) rename src/{types => }/client/types.rs (88%) rename src/{types => }/node/mod.rs (100%) create mode 100644 src/types/traits.rs diff --git a/.#README.md b/.#README.md new file mode 120000 index 0000000..b37d705 --- /dev/null +++ b/.#README.md @@ -0,0 +1 @@ +horhik@lap.15278:1615285836 \ No newline at end of file diff --git a/README.md b/README.md index 2e816b5..34e4a69 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,8 @@ Library for freenet FCPv2 protocol It's set of structures from [official FCPv2 wiki](https://github.com/freenet/wiki/wiki/FCPv2), their implementations, parse and convert functions +Open for contributig!!! + +---- +**DONT USE IT FOR PRODUCTION!!!** current versions is SO UNSTABLE! + diff --git a/src/types/client/fcp_request.rs b/src/client/fcp_request.rs similarity index 100% rename from src/types/client/fcp_request.rs rename to src/client/fcp_request.rs diff --git a/src/types/client/fcp_types.rs b/src/client/fcp_types.rs similarity index 93% rename from src/types/client/fcp_types.rs rename to src/client/fcp_types.rs index e6a49f7..11df456 100644 --- a/src/types/client/fcp_types.rs +++ b/src/client/fcp_types.rs @@ -1,4 +1,6 @@ use super::types::*; +use crate::types::traits::*; +use crate::types::{SSKKeypair, SSK, ReturnType}; use regex::Regex; impl ClientHello { @@ -32,7 +34,7 @@ fn client_hello_converts() { let hello = ClientHello::new("user name".to_string(), 2.0); assert_eq!( hello.convert(), - "ClientHello\nName=user name\nExpectedVersion=2\nEndMessage\n\n" + "ClientHello\nName=user name\nExpectedVersion=2.0\nEndMessage\n\n" ); } @@ -104,44 +106,6 @@ impl FcpRequest for GenerateSSK { } } -#[derive(Debug, PartialEq)] -pub struct SSK { - sign_key: String, - decrypt_key: String, - settings: Option, -} -#[derive(Debug, PartialEq)] -pub struct USK { - ssk: SSK, - index: i32, -} - -#[derive(Debug, PartialEq)] -pub struct SSKKeypair { - insert_uri: SSK, - request_uri: SSK, - identifier: String, -} - -impl FcpParser for SSKKeypair { - fn parse(plain: &str) -> Option { - let reg = Regex::new( - r"^SSKKeypair\nIdentifier=(.*)\nInsertURI=(.*)\nRequestURI=(.*)\nEndMessage", - ) - .unwrap(); - println!("{:?}", reg); - let res = reg.captures(plain).unwrap(); - let identifier = res[1].to_string(); - let insert_uri = SSK::parse(&res[2]).unwrap(); - let request_uri = SSK::parse(&res[3]).unwrap(); - return Some(SSKKeypair { - insert_uri: insert_uri, - request_uri: request_uri, - identifier: identifier, - }); - } -} - //SSK@Rgt0qM8D24DltliV2-JE9tYLcrgGAKeDwkz41I3JBPs,p~c8c7FXcJjhcf2vA-Xm0Mjyw1o~xn7L2-T8zlBA1IU,AQECAAE/ //SSK@uKTwaQIXNgsCYKLekb51t3pZ6A~PTP7nuCxRVZEMtCQ,p~c8c7FXcJjhcf2vA-Xm0Mjyw1o~xn7L2-T8zlBA1IU,AQACAAE/ /* @@ -176,9 +140,6 @@ fn is_keypair_parsing() { ) } -pub trait FcpParser { - fn parse(palin: &str) -> Option; -} impl FcpParser for SSK { fn parse(plain: &str) -> Option { let reg1 = Regex::new(r".*?SSK@([a-zA-z0-9~-]*),([a-zA-Z0-9-~]*),([A-Z]*)").unwrap(); @@ -454,7 +415,6 @@ fn is_client_put_converting() { } pub struct ClientGet { - message_name: String, ignore_ds: Option, ds_only: Option, uri: String, //FIXME freenet uri type @@ -496,10 +456,35 @@ BinaryBlob=false FilterData=true EndMessage */ +impl ClientGet { + pub fn new_default(uri: SSK, identifier: &str, return_type: ReturnType) -> ClientGet { + ClientGet { + ignore_ds: None, + ds_only: None, + uri: uri.convert(), //FIXME freenet uri type + identifier: identifier.to_string(), + verbosity: None, + max_size: None, + max_temp_size: None, + max_retries: None, + priority_class: None, + persistence: None, + client_token: None, + global: None, + return_type: Some(return_type), + binary_blob: None, + filter_data: None, + allowed_mime_types: None, + filename: None, + temp_filename: None, + real_time_flag: None, + initial_metadata_data_length: None, + } + } +} impl FcpRequest for ClientGet { fn convert(&self) -> String { - unimplemented!(); format!( "ClientGet\n\ {}\ diff --git a/src/types/client/mod.rs b/src/client/mod.rs similarity index 100% rename from src/types/client/mod.rs rename to src/client/mod.rs diff --git a/src/types/client/types.rs b/src/client/types.rs similarity index 88% rename from src/types/client/types.rs rename to src/client/types.rs index e9c637d..0bc7662 100644 --- a/src/types/client/types.rs +++ b/src/client/types.rs @@ -1,3 +1,4 @@ +use crate::types::traits::*; pub use std::ffi::OsStr; pub use std::net::Ipv4Addr; pub use std::path::Path; @@ -153,19 +154,6 @@ fn is_upload_from_converting() { ); assert_eq!(fcp_types_unwrap::(None), "".to_string()); } - -pub enum ReturnType { - Direct, - None, - Disk, -} - -impl FcpRequest for ReturnType { - fn convert(&self) -> String { - unimplemented!(); - } -} - pub enum NumOrNone { None, Num(u32), @@ -257,23 +245,3 @@ impl FcpRequest for bool { } } } - -pub trait FcpRequest { - fn convert(&self) -> String; - - fn fcp_wrap(&self, prefix: &str, postfix: &str) -> String { - format!("{}{}{}", prefix, self.convert(), postfix) - } -} -pub fn fcp_types_unwrap(fcp_type: Option<&T>) -> String { - match fcp_type { - Some(val) => val.convert(), - None => String::from(""), - } -} -pub fn to_fcp_unwrap(prefix: &str, fcp_type: &Option, postfix: &str) -> String { - match fcp_type { - Some(val) => val.fcp_wrap(&prefix, &postfix), - None => String::from(""), - } -} diff --git a/src/lib.rs b/src/lib.rs index 59035d0..0839fed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,25 +1,3 @@ -mod types; -pub use types::client; -pub use types::node; - -#[cfg(test)] -mod tests { - - #[macro_export] - macro_rules! vec { - ( $( $x:expr ),* ) => { - { - let mut temp_vec = Vec::new(); - $( - temp_vec.push($x); - )* - temp_vec - } - }; -} - - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +pub mod types; +pub mod client; +pub mod node; diff --git a/src/types/node/mod.rs b/src/node/mod.rs similarity index 100% rename from src/types/node/mod.rs rename to src/node/mod.rs diff --git a/src/types/mod.rs b/src/types/mod.rs index ac7f17d..90ba3f9 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,2 +1,55 @@ -pub mod client; -pub mod node; +pub mod traits; +use traits::*; +use regex::Regex; + +#[derive(Debug, PartialEq)] +pub struct SSK { + pub sign_key: String, + pub decrypt_key: String, + pub settings: Option, +} +#[derive(Debug, PartialEq)] +pub struct USK { + pub ssk: SSK, + pub index: i32, +} + +#[derive(Debug, PartialEq)] +pub struct SSKKeypair { + pub insert_uri: SSK, + pub request_uri: SSK, + pub identifier: String, +} + +impl FcpParser for SSKKeypair { + fn parse(plain: &str) -> Option { + let reg = Regex::new( + r"^SSKKeypair\nIdentifier=(.*)\nInsertURI=(.*)\nRequestURI=(.*)\nEndMessage", + ) + .unwrap(); + println!("{:?}", reg); + let res = reg.captures(plain).unwrap(); + let identifier = res[1].to_string(); + let insert_uri = SSK::parse(&res[2]).unwrap(); + let request_uri = SSK::parse(&res[3]).unwrap(); + return Some(SSKKeypair { + insert_uri: insert_uri, + request_uri: request_uri, + identifier: identifier, + }); + } +} + + +pub enum ReturnType { + Direct, + None, + Disk, +} + +impl FcpRequest for ReturnType { + fn convert(&self) -> String { + unimplemented!(); + } +} + diff --git a/src/types/traits.rs b/src/types/traits.rs new file mode 100644 index 0000000..367d5b2 --- /dev/null +++ b/src/types/traits.rs @@ -0,0 +1,28 @@ + +pub trait FcpRequest { + fn convert(&self) -> String; + + fn fcp_wrap(&self, prefix: &str, postfix: &str) -> String { + format!("{}{}{}", prefix, self.convert(), postfix) + } +} + + +pub trait FcpParser { + fn parse(palin: &str) -> Option; +} + +pub fn fcp_types_unwrap(fcp_type: Option<&T>) -> String { + match fcp_type { + Some(val) => val.convert(), + None => String::from(""), + } +} +pub fn to_fcp_unwrap(prefix: &str, fcp_type: &Option, postfix: &str) -> String { + match fcp_type { + Some(val) => val.fcp_wrap(&prefix, &postfix), + None => String::from(""), + } +} + +