change file structure

This commit is contained in:
Horhik 2021-03-12 07:49:51 +03:00
parent 1627ccf362
commit c3962d8808
10 changed files with 122 additions and 104 deletions

1
.#README.md Symbolic link
View File

@ -0,0 +1 @@
horhik@lap.15278:1615285836

View File

@ -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!

View File

@ -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<String>,
}
#[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<SSKKeypair> for SSKKeypair {
fn parse(plain: &str) -> Option<SSKKeypair> {
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<T> {
fn parse(palin: &str) -> Option<T>;
}
impl FcpParser<SSK> for SSK {
fn parse(plain: &str) -> Option<SSK> {
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<bool>,
ds_only: Option<bool>,
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\
{}\

View File

@ -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::<Persistence>(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<T: FcpRequest>(fcp_type: Option<&T>) -> String {
match fcp_type {
Some(val) => val.convert(),
None => String::from(""),
}
}
pub fn to_fcp_unwrap<T: FcpRequest>(prefix: &str, fcp_type: &Option<T>, postfix: &str) -> String {
match fcp_type {
Some(val) => val.fcp_wrap(&prefix, &postfix),
None => String::from(""),
}
}

View File

@ -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;

View File

@ -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<String>,
}
#[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<SSKKeypair> for SSKKeypair {
fn parse(plain: &str) -> Option<SSKKeypair> {
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!();
}
}

28
src/types/traits.rs Normal file
View File

@ -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<T> {
fn parse(palin: &str) -> Option<T>;
}
pub fn fcp_types_unwrap<T: FcpRequest>(fcp_type: Option<&T>) -> String {
match fcp_type {
Some(val) => val.convert(),
None => String::from(""),
}
}
pub fn to_fcp_unwrap<T: FcpRequest>(prefix: &str, fcp_type: &Option<T>, postfix: &str) -> String {
match fcp_type {
Some(val) => val.fcp_wrap(&prefix, &postfix),
None => String::from(""),
}
}