add some functions for converting structures to string

This commit is contained in:
Horhik 2021-01-12 22:03:38 +02:00
parent 6c443784bc
commit 94f7b83c5a
3 changed files with 118 additions and 73 deletions

View File

@ -0,0 +1 @@

View File

@ -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<String>,
content_type: Option<&'static String>,
identifier: Option<String>,
verbosity: Option<VerbosityPut>,
max_retries: Option<Retry>,
priority_class: Option<i8>,
priority_class: Option<Priority>,
get_chk_only: Option<bool>,
global: Option<bool>,
dont_compress: Option<bool>,
codecs: Option<Vec<String>>,
client_token: Option<String>,
persistence: Option<Persistence>,
target_filename: Option<Box<OsStr>>,
target_filename: Option<String>, // TODO create filename type (not PATH, ONLY SLASHES)
early_encode: Option<bool>,
upload_ffrom: Option<UploadForm>,
target_uri: Option<String>, // cloning uri if does not exists
@ -123,15 +123,15 @@ pub struct ClientPut {
metadata_threshold: Option<i64>,
data: Option<String>, // 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<bool>,

View File

@ -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::<VerbosityPut>(None), "".to_string());
fn is_berbosity_put_converting() {
assert_eq!(fcp_types_unwrap::<VerbosityPut>(None), "".to_string());
assert_eq!(
default_unwrap::<VerbosityPut>(Some(&VerbosityPut::SimpleProgress)),
fcp_types_unwrap::<VerbosityPut>(Some(&VerbosityPut::SimpleProgress)),
"0".to_string()
);
assert_eq!(
default_unwrap::<VerbosityPut>(Some(&VerbosityPut::ExpectedHashes)),
fcp_types_unwrap::<VerbosityPut>(Some(&VerbosityPut::ExpectedHashes)),
"3".to_string()
);
assert_eq!(
default_unwrap::<VerbosityPut>(Some(&VerbosityPut::PutFetchable)),
fcp_types_unwrap::<VerbosityPut>(Some(&VerbosityPut::PutFetchable)),
"8".to_string()
);
assert_eq!(
default_unwrap::<VerbosityPut>(Some(
fcp_types_unwrap::<VerbosityPut>(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::<Persistence>(None), "".to_string());
assert_eq!(fcp_types_unwrap::<Persistence>(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::<Persistence>(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<T: FcpRequest>(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::<Priority>(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<T: FcpRequest>(fcp_type: Option<&T>) -> String {
match fcp_type {
Some(val) => val.parse(),
Some(val) => val.convert(),
None => String::from(""),
}
}
pub fn to_fcp_unwrap<T: FcpRequest>(
prefix: &'static str,
fcp_type: Option<&T>,
postfix: &'static str,
) -> String {
match fcp_type {
Some(val) => val.fcp_wrap(&prefix, &postfix),
None => String::from(""),
}
}