From 94f7b83c5ae160d757898f1ecf5765a13e587b1d Mon Sep 17 00:00:00 2001 From: Horhik Date: Tue, 12 Jan 2021 22:03:38 +0200 Subject: [PATCH] add some functions for converting structures to string --- src/types/client/fcp_request.rs | 1 + src/types/client/fcp_types.rs | 69 ++++-------------- src/types/client/types.rs | 121 +++++++++++++++++++++++++++----- 3 files changed, 118 insertions(+), 73 deletions(-) create mode 100644 src/types/client/fcp_request.rs diff --git a/src/types/client/fcp_request.rs b/src/types/client/fcp_request.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/types/client/fcp_request.rs @@ -0,0 +1 @@ + diff --git a/src/types/client/fcp_types.rs b/src/types/client/fcp_types.rs index 0013519..f3409d1 100644 --- a/src/types/client/fcp_types.rs +++ b/src/types/client/fcp_types.rs @@ -10,7 +10,7 @@ impl ClientHello { } impl FcpRequest for ClientHello { - fn parse(&self) -> String { + fn convert(&self) -> String { return format!( "ClientHello\n\ Name={}\n\ @@ -27,10 +27,10 @@ pub struct ClientHello { } #[test] -fn client_hello_parses() { +fn client_hello_converts() { let hello = ClientHello::new("user name".to_string(), 2.0); assert_eq!( - hello.parse(), + hello.convert(), "ClientHello\nName=user name\nExpectedVersion=2\nEndMessage\n\n" ); } @@ -96,18 +96,18 @@ pub struct ClientPut { uri: String, //TODO create key type data_length: u64, filename: String, - content_type: Option, + content_type: Option<&'static String>, identifier: Option, verbosity: Option, max_retries: Option, - priority_class: Option, + priority_class: Option, get_chk_only: Option, global: Option, dont_compress: Option, codecs: Option>, client_token: Option, persistence: Option, - target_filename: Option>, + target_filename: Option, // TODO create filename type (not PATH, ONLY SLASHES) early_encode: Option, upload_ffrom: Option, target_uri: Option, // cloning uri if does not exists @@ -123,15 +123,15 @@ pub struct ClientPut { metadata_threshold: Option, data: Option, // Data fromdirect } -/* impl FcpRequest for ClientPut { - fn parse(self) -> String { + fn convert(&self) -> String { format!( "ClientPut\n\ {}\ {}\ {}\ {}\ + "/* {}\ {}\ {}\ @@ -158,57 +158,14 @@ impl FcpRequest for ClientPut { {}\ 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()), + ",*/, + format!("URI={}\n", self.uri), + format!("DataLength={}\n", self.data_length), + format!("Filename={}\n", self.filename), + to_fcp_unwrap("ContentType", self.content_type, "\n") ) } -}*/ - -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() - } - } -} - pub struct ClientGet { message_name: String, ignore_ds: Option, diff --git a/src/types/client/types.rs b/src/types/client/types.rs index f904f92..c396a8a 100644 --- a/src/types/client/types.rs +++ b/src/types/client/types.rs @@ -26,7 +26,7 @@ pub enum VerbosityPut { } impl FcpRequest for VerbosityPut { - fn parse(&self) -> String { + fn convert(&self) -> String { match self { VerbosityPut::SimpleProgress => 0.to_string(), VerbosityPut::ExpectedHashes => 3.to_string(), @@ -37,22 +37,22 @@ impl FcpRequest for VerbosityPut { } #[test] -fn is_berbosity_put_parsing() { - assert_eq!(default_unwrap::(None), "".to_string()); +fn is_berbosity_put_converting() { + assert_eq!(fcp_types_unwrap::(None), "".to_string()); assert_eq!( - default_unwrap::(Some(&VerbosityPut::SimpleProgress)), + fcp_types_unwrap::(Some(&VerbosityPut::SimpleProgress)), "0".to_string() ); assert_eq!( - default_unwrap::(Some(&VerbosityPut::ExpectedHashes)), + fcp_types_unwrap::(Some(&VerbosityPut::ExpectedHashes)), "3".to_string() ); assert_eq!( - default_unwrap::(Some(&VerbosityPut::PutFetchable)), + fcp_types_unwrap::(Some(&VerbosityPut::PutFetchable)), "8".to_string() ); assert_eq!( - default_unwrap::(Some( + fcp_types_unwrap::(Some( &VerbosityPut::StartedCompressionANDFinishedCompression )), "9".to_string() @@ -81,7 +81,7 @@ pub enum Persistence { } impl FcpRequest for Persistence { - fn parse(&self) -> String { + fn convert(&self) -> String { match *self { Persistence::Connection => "connection".to_string(), Persistence::Reboot => "reboot".to_string(), @@ -91,20 +91,20 @@ impl FcpRequest for Persistence { } #[test] -fn is_persistence_parsing() { +fn is_persistence_converting() { assert_eq!( - default_unwrap(Some(&Persistence::Connection)), + fcp_types_unwrap(Some(&Persistence::Connection)), "connection".to_string() ); assert_eq!( - default_unwrap(Some(&Persistence::Reboot)), + fcp_types_unwrap(Some(&Persistence::Reboot)), "reboot".to_string() ); assert_eq!( - default_unwrap(Some(&Persistence::Forever)), + fcp_types_unwrap(Some(&Persistence::Forever)), "forever".to_string() ); - assert_eq!(default_unwrap::(None), "".to_string()); + assert_eq!(fcp_types_unwrap::(None), "".to_string()); } pub enum UploadForm { @@ -112,6 +112,32 @@ pub enum UploadForm { Disk, Redirect, } +impl FcpRequest for UploadForm { + fn convert(&self) -> String { + match *self { + UploadForm::Direct => "direct".to_string(), + UploadForm::Disk => "disk".to_string(), + UploadForm::Redirect => "redirect".to_string(), + } + } +} + +#[test] +fn is_upload_from_converting() { + assert_eq!( + fcp_types_unwrap(Some(&UploadForm::Direct)), + "direct".to_string() + ); + assert_eq!( + fcp_types_unwrap(Some(&UploadForm::Disk)), + "disk".to_string() + ); + assert_eq!( + fcp_types_unwrap(Some(&UploadForm::Redirect)), + "redirect".to_string() + ); + assert_eq!(fcp_types_unwrap::(None), "".to_string()); +} pub enum ReturnType { Direct, @@ -134,12 +160,73 @@ pub enum Priority { G, // 6 } -pub trait FcpRequest { - fn parse(&self) -> String; +impl FcpRequest for Priority { + fn convert(&self) -> String { + match *self { + Priority::A => "0".to_string(), + Priority::B => "1".to_string(), + Priority::C => "2".to_string(), + Priority::D => "3".to_string(), + Priority::E => "4".to_string(), + Priority::F => "5".to_string(), + Priority::G => "6".to_string(), + } + } } -pub fn default_unwrap(fcp_type: Option<&T>) -> String { +#[test] +fn is_priority_converting() { + assert_eq!(fcp_types_unwrap(Some(&Priority::A)), "0".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::B)), "1".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::C)), "2".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::D)), "3".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::E)), "4".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::F)), "5".to_string()); + assert_eq!(fcp_types_unwrap(Some(&Priority::G)), "6".to_string()); + assert_eq!(fcp_types_unwrap::(None), "".to_string()); +} + +impl FcpRequest for u32 { + fn convert(&self) -> String { + self.to_string() + } +} + +impl FcpRequest for String { + fn convert(&self) -> String { + self.to_string() + } +} + +impl FcpRequest for bool { + fn convert(&self) -> String { + if *self { + "true".to_string() + } else { + "false".to_string() + } + } +} + +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.parse(), + Some(val) => val.convert(), + None => String::from(""), + } +} +pub fn to_fcp_unwrap( + prefix: &'static str, + fcp_type: Option<&T>, + postfix: &'static str, +) -> String { + match fcp_type { + Some(val) => val.fcp_wrap(&prefix, &postfix), None => String::from(""), } }