diff --git a/src/lib.rs b/src/lib.rs index 33285db..b76239b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,20 @@ use types::fcp_types; #[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); diff --git a/src/types/client/fcp_types.rs b/src/types/client/fcp_types.rs index 2ac6483..7523cce 100644 --- a/src/types/client/fcp_types.rs +++ b/src/types/client/fcp_types.rs @@ -1,4 +1,5 @@ use super::types::*; + impl ClientHello { fn new(name: String, exp_ver: f32) -> Self { ClientHello { @@ -6,7 +7,10 @@ impl ClientHello { expected_version: exp_ver, } } - fn string(self) -> String { +} + +impl FcpRequest for ClientHello { + fn parse(&self) -> String { return format!( "ClientHello\n\ Name={}\n\ @@ -26,7 +30,7 @@ pub struct ClientHello { fn client_hello_parses() { let hello = ClientHello::new("user name".to_string(), 2.0); assert_eq!( - hello.string(), + hello.parse(), "ClientHello\nName=user name\nExpectedVersion=2\nEndMessage\n\n" ); } @@ -89,8 +93,9 @@ pub struct GenerateSSK { } pub struct ClientPut { - message_name: String, uri: String, //TODO create key type + data_length: u64, + filename: String, content_type: Option, identifier: Option, verbosity: Option, @@ -104,8 +109,6 @@ pub struct ClientPut { persistence: Option>, early_encode: Option, upload_ffrom: Option, - data_length: u64, - filename: String, target_uri: Option, // cloning uri if does not exists file_hash: Option, //TODO SHAA256 type binary_blob: Option, @@ -119,6 +122,131 @@ pub struct ClientPut { metadata_threshold: Option, data: Option, // Data fromdirect } +/* +impl FcpRequest for ClientPut { + fn parse(self) -> String { + format!( + "ClientPut\n\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + {}\ + EndMessage\n\ + {}\ + ", + self.uri, + self.data_length, + self.filename, + self.content_type.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.verbosity.unwrap_or("".to_string()), + self.max_retries.unwrap_or("".to_string()), + self.priority_class.unwrap_or("".to_string()), + self.get_chk_only.unwrap_or("".to_string()), + self.global.unwrap_or("".to_string()), + self.dont_compress.unwrap_or("".to_string()), + self.codecs.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + self.identifier.unwrap_or("".to_string()), + ) + } +}*/ + +pub fn default_unwrap(fcp_type: Option<&T>) -> String { + match fcp_type { + Some(val) => val.parse(), + None => String::from(""), + } +} +impl FcpRequest for String { + fn parse(&self) -> String { + self.to_string() + } +} + +impl FcpRequest for bool { + fn parse(&self) -> String { + if *self { + "true".to_string() + } else { + "false".to_string() + } + } +} + +impl FcpRequest for VerbosityPut { + fn parse(&self) -> String { + match self { + VerbosityPut::SimpleProgress => 0.to_string(), + VerbosityPut::ExpectedHashes => 3.to_string(), + VerbosityPut::PutFetchable => 8.to_string(), + VerbosityPut::StartedCompressionANDFinishedCompression => 9.to_string(), + } + } +} + +#[test] +fn is_berbosity_put_parsing() { + assert_eq!(default_unwrap::(None), "".to_string()); + assert_eq!( + default_unwrap::(Some(&VerbosityPut::SimpleProgress)), + "0".to_string() + ); + assert_eq!( + default_unwrap::(Some(&VerbosityPut::ExpectedHashes)), + "3".to_string() + ); + assert_eq!( + default_unwrap::(Some(&VerbosityPut::PutFetchable)), + "8".to_string() + ); + assert_eq!( + default_unwrap::(Some( + &VerbosityPut::StartedCompressionANDFinishedCompression + )), + "9".to_string() + ); +} pub struct ClientGet { message_name: String, diff --git a/src/types/client/types.rs b/src/types/client/types.rs index c98ae51..cf95016 100644 --- a/src/types/client/types.rs +++ b/src/types/client/types.rs @@ -71,3 +71,7 @@ pub enum Priority { F, // 5 G, // 6 } + +pub trait FcpRequest { + fn parse(&self) -> String; +}