keeper_secrets_manager_core/dto/
field_structs.rs

1// -*- coding: utf-8 -*-
2//  _  __
3// | |/ /___ ___ _ __  ___ _ _ (R)
4// | ' </ -_) -_) '_ \/ -_) '_|
5// |_|\_\___\___| .__/\___|_|
6//              |_|
7//
8// Keeper Secrets Manager
9// Copyright 2024 Keeper Security Inc.
10// Contact: sm@keepersecurity.com
11//
12
13use std::{collections::HashMap, str::FromStr};
14
15use serde::{Deserialize, Serialize};
16use serde_json::Error;
17use serde_json::Value;
18
19use crate::{
20    custom_error::KSMRError,
21    enums::Country,
22    enums::StandardFieldTypeEnum,
23    utils::{self, PasswordOptions},
24};
25
26#[derive(Serialize, Deserialize, Debug, Clone)]
27#[serde(rename_all = "camelCase")]
28pub struct KeeperField {
29    #[serde(rename(serialize = "type", deserialize = "field_type"))]
30    pub field_type: String,
31    #[serde(default = "default_empty_string")]
32    pub label: String,
33    #[serde(default = "default_value")]
34    pub value: Value,
35    #[serde(default = "default_boolean")]
36    pub required: bool,
37    #[serde(default = "default_boolean")]
38    pub privacy_screen: bool,
39}
40
41impl KeeperField {
42    pub fn new(field_type: String, label: Option<String>) -> Self {
43        KeeperField {
44            field_type,
45            label: label.unwrap_or("".to_string()),
46            value: Value::Null,
47            required: false,
48            privacy_screen: false,
49        }
50    }
51
52    pub fn get(&self, key: &str) -> Option<&str> {
53        match key {
54            "field_type" => Some(&self.field_type),
55            "label" => Some(&self.label),
56            _ => None,
57        }
58    }
59}
60
61fn default_boolean() -> bool {
62    false
63}
64
65pub fn default_value() -> Value {
66    Value::Null
67}
68
69fn default_empty_vector<T>() -> Vec<T> {
70    vec![]
71}
72
73fn default_empty_string() -> String {
74    "".to_string()
75}
76
77fn default_empty_number() -> u8 {
78    0
79}
80
81fn default_empty_number_i32() -> i32 {
82    0
83}
84
85fn default_empty_number_i64() -> i64 {
86    0
87}
88
89fn default_empty_vector_value() -> Value {
90    Value::Array(vec![])
91}
92
93pub fn default_empty_option_string() -> Option<String> {
94    Some("".to_string())
95}
96
97pub fn string_to_value_array(val: String) -> Value {
98    Value::Array(vec![Value::String(val)])
99}
100
101pub fn number_value_to_value_array(val: Value) -> Value {
102    Value::Array(vec![val])
103}
104
105pub fn string_to_value(val: String) -> Value {
106    Value::String(val)
107}
108
109pub fn value_to_value_array(val: Value) -> Value {
110    Value::Array(vec![val])
111}
112
113fn _extract_to_option_value(opt: ValueType) -> Option<Vec<Value>> {
114    match opt {
115        ValueType::VecValue(vec) => vec,
116        ValueType::StringValue(str) => Some(vec![serde_json::Value::String(str)]),
117    }
118}
119
120pub enum ValueType {
121    VecValue(Option<Vec<Value>>),
122    StringValue(String),
123}
124
125#[derive(Serialize, Deserialize, Debug)]
126pub struct Login {
127    /// ```ignore
128    ///     use keeper_secrets_manager_core::dto::field_structs;
129    ///     let login_field_entry = field_structs::Login::new("dummy_Email@email.com".to_string(),Some("dummy_login_label".to_string()),None,None);
130    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
131    ///     login_new.append_standard_fields(login_field_entry);
132    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
133    /// ```
134    #[serde(flatten)]
135    keeper_fields: KeeperField,
136    #[serde(default = "default_empty_vector_value")]
137    value: Value,
138    #[serde(default = "default_boolean")]
139    required: bool,
140    #[serde(default = "default_boolean")]
141    privacy_screen: bool,
142}
143
144impl Login {
145    pub fn new_login(value: String) -> KeeperField {
146        let value_parsed = value;
147        Login::new(value_parsed, None, None, None)
148    }
149
150    #[allow(clippy::new_ret_no_self)]
151    pub fn new(
152        value: String,
153        label: Option<String>,
154        required: Option<bool>,
155        privacy_screen: Option<bool>,
156    ) -> KeeperField {
157        let login_value = Value::Array(vec![Value::String(value)]);
158        KeeperField {
159            field_type: StandardFieldTypeEnum::LOGIN.get_type().to_string(),
160            label: label.unwrap_or(StandardFieldTypeEnum::LOGIN.get_type().to_string()),
161            value: login_value,
162            required: required.unwrap_or(false),
163            privacy_screen: privacy_screen.unwrap_or(false),
164        }
165    }
166
167    pub fn as_keeper_field(&self) -> KeeperField {
168        self.keeper_fields.clone()
169    }
170}
171
172#[derive(Serialize, Deserialize, Debug)]
173pub struct PasswordComplexity {
174    #[serde(default = "default_empty_number")]
175    pub length: u8,
176    #[serde(default = "default_empty_number")]
177    pub caps: u8,
178    #[serde(default = "default_empty_number")]
179    pub lower: u8,
180    #[serde(default = "default_empty_number")]
181    pub digits: u8,
182    #[serde(default = "default_empty_number")]
183    pub special: u8,
184}
185
186impl PasswordComplexity {
187    pub fn new(
188        length: Option<u8>,
189        caps: Option<u8>,
190        lower: Option<u8>,
191        digits: Option<u8>,
192        special: Option<u8>,
193    ) -> Self {
194        PasswordComplexity {
195            length: length.unwrap_or(32),
196            caps: caps.unwrap_or(0),
197            lower: lower.unwrap_or(0),
198            digits: digits.unwrap_or(0),
199            special: special.unwrap_or(0),
200        }
201    }
202}
203
204#[derive(Serialize, Deserialize, Debug)]
205pub struct Password {
206    /// ```ignore
207    ///     use keeper_secrets_manager_core::dto::field_structs;
208    ///     let password_field_entry = field_structs::Password::new("".to_string(),Some("dummy_password_label".to_string()),None,Some(true),None,None)?;
209    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
210    ///     login_new.append_standard_fields(password_field_entry);
211    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
212    /// ```
213    #[serde(flatten)]
214    keeper_fields: KeeperField,
215    #[serde(default = "default_empty_vector_value")]
216    value: Value,
217    #[serde(default = "default_boolean")]
218    required: bool,
219    #[serde(default = "default_boolean")]
220    enforce_generation: bool,
221    #[serde(default = "default_boolean")]
222    privacy_screen: bool,
223    complexity: Option<PasswordComplexity>,
224}
225
226impl Password {
227    #[allow(clippy::new_ret_no_self)]
228    pub fn new(
229        value: String,
230        label: Option<String>,
231        required: Option<bool>,
232        enforce_generation: Option<bool>,
233        privacy_screen: Option<bool>,
234        password_complexity: Option<PasswordComplexity>,
235    ) -> Result<KeeperField, KSMRError> {
236        let password_value;
237        if value.is_empty() {
238            let enforce_generation_value = enforce_generation.unwrap_or_default();
239            if enforce_generation_value {
240                let pass_complexity = match password_complexity {
241                    Some(password_complexity) => password_complexity,
242                    None => PasswordComplexity::new(None, None, None, None, None),
243                };
244                let password_options = PasswordOptions::new()
245                    .digits(pass_complexity.digits.into())
246                    .length(pass_complexity.length.into())
247                    .lowercase(pass_complexity.lower.into())
248                    .uppercase(pass_complexity.caps.into())
249                    .special_characters(pass_complexity.special.into());
250                let generated_password_value =
251                    utils::generate_password_with_options(password_options)?;
252                password_value = Value::Array(vec![Value::String(generated_password_value)]);
253            } else {
254                return Err(KSMRError::RecordDataError("Password value is empty and enforce generation is false, please make one or other a true value".to_string()));
255            }
256        } else {
257            password_value = Value::Array(vec![Value::String(value)]);
258        }
259
260        let mut keeper_field = KeeperField::new("password".to_string(), label);
261        keeper_field.value = password_value;
262        keeper_field.required = required.unwrap_or(false);
263        keeper_field.privacy_screen = privacy_screen.unwrap_or(false);
264
265        Ok(keeper_field)
266    }
267
268    pub fn new_password(value: String) -> Result<KeeperField, KSMRError> {
269        Password::new(value, None, None, None, None, None)
270    }
271}
272
273#[derive(Serialize, Deserialize, Debug)]
274pub struct URL {
275    /// ```ignore
276    ///     use keeper_secrets_manager_core::dto::field_structs;
277    ///     let url_field =  field_structs::URL::new("dummy_url.com".to_string(), None, None, None);
278    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
279    ///     login_new.append_standard_fields(url_field);
280    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
281    /// ```
282    #[serde(flatten)]
283    keeper_fields: KeeperField,
284    #[serde(default = "default_empty_vector_value")]
285    value: Value,
286    #[serde(default = "default_boolean")]
287    required: bool,
288    #[serde(default = "default_boolean")]
289    privacy_screen: bool,
290}
291
292impl URL {
293    #[allow(clippy::new_ret_no_self)]
294    pub fn new(
295        value: String,
296        label: Option<String>,
297        required: Option<bool>,
298        privacy_screen: Option<bool>,
299    ) -> KeeperField {
300        let url_value = string_to_value_array(value);
301        let mut keeper_field =
302            KeeperField::new(StandardFieldTypeEnum::URL.get_type().to_string(), label);
303        keeper_field.value = url_value;
304        keeper_field.required = required.unwrap_or(false);
305        keeper_field.privacy_screen = privacy_screen.unwrap_or(false);
306
307        keeper_field
308    }
309
310    pub fn new_url(value: String) -> KeeperField {
311        URL::new(value, None, None, None)
312    }
313}
314
315#[derive(Serialize, Deserialize, Debug)]
316pub struct FileRef {
317    /// ```ignore
318    ///     use keeper_secrets_manager_core::dto::field_structs;
319    ///     let file_ref_field = field_structs::FileRef::new("file::/files.file.co".to_string(), None, None);
320    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
321    ///     login_new.append_standard_fields(file_ref_field);
322    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
323    /// ```
324    #[serde(flatten)]
325    keeper_fields: KeeperField,
326    #[serde(default = "default_empty_vector")]
327    pub value: Vec<Value>,
328    #[serde(default = "default_boolean")]
329    required: bool,
330}
331
332impl FileRef {
333    #[allow(clippy::new_ret_no_self)]
334    pub fn new(value: String, label: Option<String>, required: Option<bool>) -> KeeperField {
335        let file_ref_value = string_to_value_array(value);
336        let mut keeper_field =
337            KeeperField::new(StandardFieldTypeEnum::FILEREF.get_type().to_string(), label);
338        keeper_field.value = file_ref_value;
339        keeper_field.required = required.unwrap_or(false);
340
341        keeper_field
342    }
343
344    pub fn new_file_ref(value: String) -> KeeperField {
345        FileRef::new(value, None, None)
346    }
347}
348
349#[derive(Serialize, Deserialize, Debug)]
350pub struct OneTimePassword {
351    #[serde(flatten)]
352    keeper_fields: KeeperField,
353    #[serde(default = "default_boolean")]
354    required: bool,
355    #[serde(default = "default_boolean")]
356    privacy_screen: bool,
357    #[serde(default = "default_empty_vector")]
358    value: Vec<Value>,
359}
360
361impl OneTimePassword {
362    #[allow(clippy::new_ret_no_self)]
363    pub fn new(
364        value: String,
365        label: Option<String>,
366        required: Option<bool>,
367        privacy_screen: Option<bool>,
368    ) -> KeeperField {
369        let otp_value = string_to_value_array(value);
370        let mut keeper_field = KeeperField::new(
371            StandardFieldTypeEnum::ONETIMECODE.get_type().to_string(),
372            label,
373        );
374        keeper_field.value = otp_value;
375        keeper_field.required = required.unwrap_or(false);
376        keeper_field.privacy_screen = privacy_screen.unwrap_or(true);
377        keeper_field
378    }
379
380    pub fn new_otp(value: String) -> KeeperField {
381        OneTimePassword::new(value, None, None, None)
382    }
383}
384
385#[derive(Serialize, Deserialize, Debug)]
386pub struct Name {
387    #[serde(skip_serializing_if = "Option::is_none")]
388    first: Option<String>,
389    #[serde(skip_serializing_if = "Option::is_none")]
390    middle: Option<String>,
391    #[serde(skip_serializing_if = "Option::is_none")]
392    last: Option<String>,
393}
394
395impl Name {
396    pub fn new(first: Option<String>, middle: Option<String>, last: Option<String>) -> Self {
397        Name {
398            first,
399            middle,
400            last,
401        }
402    }
403
404    pub fn to_json(&self) -> Result<String, KSMRError> {
405        Ok(serde_json::to_string(self)?)
406    }
407}
408
409#[derive(Serialize, Deserialize, Debug)]
410pub struct Names {
411    #[serde(flatten)]
412    keeper_fields: KeeperField,
413    #[serde(default = "default_boolean")]
414    required: bool,
415    #[serde(default = "default_boolean")]
416    privacy_screen: bool,
417    value: Vec<Name>,
418}
419
420impl Names {
421    /// ```ignore
422    ///     use keeper_secrets_manager_core::dto::field_structs;
423    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
424    ///     let name: Name =field_structs::Name::new(Some("Sample".to_string()), None, Some("User".to_string()));
425    ///     let names: Vec<Name> = vec![name];
426    ///     let names_field: KeeperField = field_structs::Names::new(names, None, false, false);
427    ///     login_new.append_standard_fields(names_field);
428    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
429    /// ```
430    #[allow(clippy::new_ret_no_self)]
431    pub fn new(
432        value: Vec<Name>,
433        label: Option<String>,
434        required: bool,
435        privacy_screen: bool,
436    ) -> KeeperField {
437        let mut keeper_field =
438            KeeperField::new(StandardFieldTypeEnum::NAMES.get_type().to_string(), label);
439        keeper_field.value = Names::vec_name_to_names_string(value);
440        keeper_field.required = required;
441        keeper_field.privacy_screen = privacy_screen;
442        keeper_field
443    }
444
445    fn vec_name_to_names_string(mut value: Vec<Name>) -> Value {
446        let names_string: Vec<Value> = value
447            .iter_mut()
448            .map(|name: &mut Name| name.to_json().unwrap())
449            .map(|name: String| {
450                Value::from_str(name.as_str())
451                    .map_err(|err: Error| KSMRError::DeserializationError(err.to_string()))
452                    .unwrap()
453            })
454            .collect::<Vec<Value>>();
455        let names_string_value_array: Value = Value::Array(names_string);
456        names_string_value_array
457    }
458
459    pub fn new_names(value: Vec<Name>) -> KeeperField {
460        Names::new(value, None, false, false)
461    }
462}
463
464#[derive(Serialize, Deserialize, Debug)]
465pub struct Date {
466    #[serde(flatten)]
467    keeper_fields: KeeperField,
468    #[serde(default = "default_boolean")]
469    required: bool,
470    #[serde(default = "default_boolean")]
471    privacy_screen: bool,
472    value: Vec<i64>,
473}
474
475impl Date {
476    #[allow(clippy::new_ret_no_self)]
477    pub fn new(
478        value_in_date_milliseconds: u128,
479        label: Option<String>,
480        required: bool,
481        privacy_screen: bool,
482    ) -> KeeperField {
483        let date_value = number_value_to_value_array(Value::Number(
484            serde_json::Number::from_u128(value_in_date_milliseconds).unwrap(),
485        ));
486
487        let mut keeper_field =
488            KeeperField::new(StandardFieldTypeEnum::NAMES.get_type().to_string(), label);
489        keeper_field.value = date_value;
490        keeper_field.required = required;
491        keeper_field.privacy_screen = privacy_screen;
492        keeper_field
493    }
494
495    pub fn new_date(value: u128) -> KeeperField {
496        Date::new(value, None, false, false)
497    }
498}
499
500#[derive(Serialize, Deserialize, Debug)]
501pub struct BirthDate {
502    #[serde(flatten)]
503    keeper_fields: KeeperField,
504    #[serde(default = "default_boolean")]
505    required: bool,
506    #[serde(default = "default_boolean")]
507    privacy_screen: bool,
508    value: Vec<i64>,
509}
510
511impl BirthDate {
512    ///```ignore
513    ///     let mut birth_certificate = RecordCreate::new(DefaultRecordType::birthCertificate.get_type().to_string(), "birth_certificate".to_string(), Some("dummy_notes_changed".to_string()));
514    ///     let name = field_structs::Name::new(Some("first_name".to_string()), None, Some("last name".to_string()));
515    ///     let names = vec![name];
516    ///     let names_field = field_structs::Names::new(names, Some("some_label".to_string()), false, false);
517    ///     let now = SystemTime::now();
518    /// // Calculate milliseconds since UNIX epoch
519    ///     let millis = now
520    ///         .duration_since(UNIX_EPOCH)
521    ///         .expect("Time went backwards")
522    ///         .as_millis();
523    ///     let date_field = field_structs::BirthDate::new_birth_date(millis);
524    ///     birth_certificate.append_standard_fields(date_field);
525    ///     birth_certificate.append_standard_fields(names_field);
526    ///     let created_record: Result<String, KSMRError> = secrets_manager.create_secret("0fLf6oIA9KY8V4BIbWz0kA".to_string(), birth_certificate);
527    /// ```
528    #[allow(clippy::new_ret_no_self)]
529    pub fn new(
530        value: u128,
531        label: Option<String>,
532        required: bool,
533        privacy_screen: bool,
534    ) -> KeeperField {
535        let mut keeper_field_date = Date::new(value, label, required, privacy_screen);
536        keeper_field_date.field_type = StandardFieldTypeEnum::BIRTHDATE.get_type().to_string();
537        keeper_field_date
538    }
539
540    pub fn new_birth_date(value: u128) -> KeeperField {
541        BirthDate::new(value, None, false, false)
542    }
543}
544
545#[derive(Serialize, Deserialize, Debug)]
546pub struct ExpirationDate {
547    #[serde(flatten)]
548    keeper_fields: KeeperField,
549    #[serde(default = "default_boolean")]
550    required: bool,
551    #[serde(default = "default_boolean")]
552    privacy_screen: bool,
553    value: Vec<u128>,
554}
555
556impl ExpirationDate {
557    ///```ignore
558    ///     let mut birth_certificate = RecordCreate::new(DefaultRecordType::birthCertificate.get_type().to_string(), "birth_certificate".to_string(), Some("dummy_notes_changed".to_string()));
559    ///     let name = field_structs::Name::new(Some("first_name".to_string()), None, Some("last name".to_string()));
560    ///     let names = vec![name];
561    ///     let names_field = field_structs::Names::new(names, Some("some_label".to_string()), false, false);
562    ///     let now = SystemTime::now();
563    /// // Calculate milliseconds since UNIX epoch
564    ///     let millis = now
565    ///         .duration_since(UNIX_EPOCH)
566    ///         .expect("Time went backwards")
567    ///         .as_millis();
568    ///     let date_field = field_structs::ExpirationDate::new_birth_date(millis);
569    ///     birth_certificate.append_standard_fields(date_field);
570    ///     birth_certificate.append_standard_fields(names_field);
571    ///     let created_record: Result<String, KSMRError> = secrets_manager.create_secret("0fLf6oIA9KY8V4BIbWz0kA".to_string(), birth_certificate);
572    /// ```
573    #[allow(clippy::new_ret_no_self)]
574    pub fn new(
575        value: u128,
576        label: Option<String>,
577        required: bool,
578        privacy_screen: bool,
579    ) -> KeeperField {
580        let mut keeper_field = Date::new(value, label, required, privacy_screen);
581        keeper_field.field_type = StandardFieldTypeEnum::EXPIRATIONDATE.get_type().to_string();
582        keeper_field
583    }
584
585    pub fn new_expiration_date(value: u128) -> KeeperField {
586        ExpirationDate::new(value, None, false, false)
587    }
588}
589
590#[derive(Serialize, Deserialize, Debug)]
591pub struct Text {
592    #[serde(flatten)]
593    keeper_fields: KeeperField,
594    #[serde(default = "default_boolean")]
595    required: bool,
596    #[serde(default = "default_boolean")]
597    privacy_screen: bool,
598    value: Vec<String>,
599}
600
601impl Text {
602    /// ```ignore
603    ///     use keeper_secrets_manager_core::dto::field_structs;
604    ///     let text_field =  field_structs::Text::new("dummy_text".to_string(), None, false, false);
605    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
606    ///     login_new.append_standard_fields(text_field);
607    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
608    /// ```
609    #[allow(clippy::new_ret_no_self)]
610    pub fn new(
611        value: String,
612        label: Option<String>,
613        required: bool,
614        privacy_screen: bool,
615    ) -> KeeperField {
616        let text_value = string_to_value_array(value);
617        let mut keeper_field =
618            KeeperField::new(StandardFieldTypeEnum::TEXT.get_type().to_string(), label);
619        keeper_field.value = text_value;
620        keeper_field.required = required;
621        keeper_field.privacy_screen = privacy_screen;
622
623        keeper_field
624    }
625
626    pub fn new_text(value: String) -> KeeperField {
627        Text::new(value, None, false, false)
628    }
629}
630
631#[derive(Serialize, Deserialize, Debug)]
632pub struct SecurityQuestion {
633    question: String,
634    answer: String,
635}
636
637impl SecurityQuestion {
638    pub fn new(question: String, answer: String) -> Self {
639        SecurityQuestion { question, answer }
640    }
641
642    pub fn to_json(&self) -> Result<String, KSMRError> {
643        Ok(serde_json::to_string(self)?)
644    }
645}
646
647#[derive(Serialize, Deserialize, Debug)]
648pub struct SecurityQuestions {
649    #[serde(flatten)]
650    keeper_fields: KeeperField,
651    #[serde(default = "default_boolean")]
652    required: bool,
653    #[serde(default = "default_boolean")]
654    privacy_screen: bool,
655    value: Vec<SecurityQuestion>,
656}
657
658impl SecurityQuestions {
659    #[allow(clippy::new_ret_no_self)]
660    pub fn new(
661        value: Vec<SecurityQuestion>,
662        label: Option<String>,
663        required: bool,
664        privacy_screen: bool,
665    ) -> KeeperField {
666        let mut keeper_field = KeeperField::new(
667            StandardFieldTypeEnum::SECURITYQUESTIONS
668                .get_type()
669                .to_string(),
670            label,
671        );
672        keeper_field.value =
673            SecurityQuestions::vec_security_question_to_security_questions_string(value);
674        keeper_field.required = required;
675        keeper_field.privacy_screen = privacy_screen;
676        keeper_field
677    }
678
679    fn vec_security_question_to_security_questions_string(
680        mut value: Vec<SecurityQuestion>,
681    ) -> Value {
682        let security_questios_string: Vec<Value> = value
683            .iter_mut()
684            .map(|security_question| security_question.to_json().unwrap())
685            .map(|security_question| {
686                Value::from_str(security_question.as_str())
687                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
688                    .unwrap()
689            })
690            .collect::<Vec<Value>>();
691        Value::Array(security_questios_string)
692    }
693}
694
695#[derive(Serialize, Deserialize, Debug)]
696pub struct Multiline {
697    #[serde(flatten)]
698    keeper_fields: KeeperField,
699    #[serde(default = "default_boolean")]
700    required: bool,
701    #[serde(default = "default_boolean")]
702    privacy_screen: bool,
703    value: Vec<String>,
704}
705
706impl Multiline {
707    /// ```ignore
708    ///     let mut new_record = RecordCreate::new(DefaultRecordType::Login.get_type().to_string(), "sample record".to_string(), None);
709    ///     let multiline_field = field_structs::Multiline::new("Hello\nWorld".to_string(), None, true, false);
710    ///     new_record.append_custom_field(multiline_field);
711    ///     let created_record: Result<String, KSMRError> = secrets_manager.create_secret("Yi_OxwTV2tdBWi-_Aegs_w".to_string(), new_record);
712    ///
713    #[allow(clippy::new_ret_no_self)]
714    pub fn new(
715        value: String,
716        label: Option<String>,
717        required: bool,
718        privacy_screen: bool,
719    ) -> KeeperField {
720        let mut keeper_field = KeeperField::new(
721            StandardFieldTypeEnum::MULTILINE.get_type().to_string(),
722            label,
723        );
724        keeper_field.required = required;
725        keeper_field.privacy_screen = privacy_screen;
726        keeper_field.value = string_to_value_array(value);
727        keeper_field
728    }
729}
730
731#[derive(Serialize, Deserialize, Debug)]
732pub struct Email {
733    #[serde(flatten)]
734    keeper_fields: KeeperField,
735    #[serde(default = "default_boolean")]
736    required: bool,
737    #[serde(default = "default_boolean")]
738    privacy_screen: bool,
739    value: Vec<String>,
740}
741
742impl Email {
743    /// ```ignore
744    ///     use keeper_secrets_manager_core::dto::field_structs;
745    ///     let text_field =  field_structs::Text::new("dummy_text".to_string(), None, false, false);
746    ///     let email_field = field_structs::Email::new("sample_email@metron.com".to_string(), None, false, false);
747    ///     login_new.append_standard_fields(email_field);
748    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
749    /// ```
750    #[allow(clippy::new_ret_no_self)]
751    pub fn new(
752        value: String,
753        label: Option<String>,
754        required: bool,
755        privacy_screen: bool,
756    ) -> KeeperField {
757        let mut keeper_field =
758            KeeperField::new(StandardFieldTypeEnum::EMAIL.get_type().to_string(), label);
759        keeper_field.value = string_to_value_array(value);
760        keeper_field.required = required;
761        keeper_field.privacy_screen = privacy_screen;
762        keeper_field
763    }
764
765    pub fn new_email(value: String) -> KeeperField {
766        Email::new(value, None, false, false)
767    }
768}
769
770#[derive(Serialize, Deserialize, Debug)]
771pub struct CardRef {
772    #[serde(flatten)]
773    keeper_fields: KeeperField,
774    #[serde(default = "default_boolean")]
775    required: bool,
776    #[serde(default = "default_boolean")]
777    privacy_screen: bool,
778    value: Vec<String>,
779}
780
781impl CardRef {
782    pub fn new(value: String, label: Option<String>, required: bool, privacy_screen: bool) -> Self {
783        CardRef {
784            keeper_fields: KeeperField::new(
785                StandardFieldTypeEnum::CARDREF.get_type().to_string(),
786                label,
787            ),
788            value: vec![value],
789            required,
790            privacy_screen,
791        }
792    }
793
794    pub fn new_card_ref(value: String) -> Self {
795        CardRef::new(value, None, false, false)
796    }
797}
798
799#[derive(Serialize, Deserialize, Debug)]
800pub struct AddressRef {
801    #[serde(flatten)]
802    keeper_fields: KeeperField,
803    #[serde(default = "default_boolean")]
804    required: bool,
805    #[serde(default = "default_boolean")]
806    privacy_screen: bool,
807    value: Vec<String>,
808}
809
810impl AddressRef {
811    /// ```ignore
812    /// let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
813    /// let mut address_new = RecordCreate::new("address".to_string(), "sampleaddress1".to_string(), Some("dummy_notes_changed".to_string()));
814    /// let address1 = field_structs::Address::new(Some("street1".to_string()), Some("street2".to_string()), Some("city".to_string()), Some("state".to_string()), "IN".to_string(), None)?;
815    /// let addresses= field_structs::Addresses::new_addresses(address1);
816    /// address_new.append_standard_fields(addresses);
817    /// let created_address = secrets_manager.create_secret("0fLf6oIA9KY8V4BIbWz0kA".to_string(), address_new)?;
818    /// let address_ref_field = field_structs::AddressRef::new_address_ref(created_address);
819    /// login_new.append_custom_field(address_ref_field);
820    /// let created_record: Result<String, KSMRError> = secrets_manager.create_secret("parent_folder_uid".to_string(), login_new);
821    /// ```
822    #[allow(clippy::new_ret_no_self)]
823    pub fn new(
824        value: String,
825        label: Option<String>,
826        required: bool,
827        privacy_screen: bool,
828    ) -> KeeperField {
829        let address_ref_value = string_to_value_array(value);
830        let mut keeper_field = KeeperField::new(
831            StandardFieldTypeEnum::ADDRESSREF.get_type().to_string(),
832            label,
833        );
834        keeper_field.value = address_ref_value;
835        keeper_field.required = required;
836        keeper_field.privacy_screen = privacy_screen;
837
838        keeper_field
839    }
840
841    pub fn new_address_ref(value: String) -> KeeperField {
842        AddressRef::new(value, None, false, false)
843    }
844}
845
846#[derive(Serialize, Deserialize, Debug)]
847pub struct PinCode {
848    #[serde(flatten)]
849    keeper_fields: KeeperField,
850    #[serde(default = "default_boolean")]
851    required: bool,
852    #[serde(default = "default_boolean")]
853    privacy_screen: bool,
854    value: Vec<String>,
855}
856
857impl PinCode {
858    /// ```ignore
859    ///     use keeper_secrets_manager_core::dto::field_structs;
860    ///     let pincode_field = field_structs::PinCode::new("233556".to_string(), Some("PINCODE_DUMMY_LABEL".to_string()), false, false);
861    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
862    ///     login_new.append_standard_fields(pincode_field);
863    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
864    /// ```
865    #[allow(clippy::new_ret_no_self)]
866    pub fn new(
867        value: String,
868        label: Option<String>,
869        required: bool,
870        privacy_screen: bool,
871    ) -> KeeperField {
872        let pincode_value = string_to_value_array(value);
873        let mut keeper_field =
874            KeeperField::new(StandardFieldTypeEnum::PINCODE.get_type().to_string(), label);
875        keeper_field.value = pincode_value;
876        keeper_field.required = required;
877        keeper_field.privacy_screen = privacy_screen;
878        keeper_field
879    }
880
881    pub fn new_pin_code(value: String) -> KeeperField {
882        PinCode::new(value, None, false, false)
883    }
884}
885
886#[derive(Serialize, Deserialize, Debug)]
887pub enum PhoneTypeOption {
888    Mobile,
889    Home,
890    Work,
891}
892
893#[derive(Serialize, Deserialize, Debug)]
894pub struct Phone {
895    #[serde(skip_serializing_if = "Option::is_none")]
896    region: Option<String>, // Region code, e.g., US
897    number: String, // Phone number, e.g., 510-222-5555
898    #[serde(skip_serializing_if = "Option::is_none")]
899    ext: Option<String>, // Extension number, e.g., 9987
900    #[serde(rename(serialize = "type", deserialize = "field_type"))]
901    phone_type: Option<PhoneTypeOption>, // Phone type, e.g., Mobile
902}
903
904impl Phone {
905    pub fn new(
906        number: String,
907        region: Option<String>,
908        ext: Option<String>,
909        phone_type: Option<PhoneTypeOption>,
910    ) -> Self {
911        let phone_type_parsed = match phone_type {
912            Some(PhoneTypeOption::Mobile) => Some(PhoneTypeOption::Mobile),
913            Some(PhoneTypeOption::Home) => Some(PhoneTypeOption::Home),
914            Some(PhoneTypeOption::Work) => Some(PhoneTypeOption::Work),
915            None => Some(PhoneTypeOption::Home),
916        };
917        Phone {
918            region,
919            number,
920            ext,
921            phone_type: phone_type_parsed,
922        }
923    }
924
925    pub fn to_json(&self) -> Result<String, KSMRError> {
926        Ok(serde_json::to_string(self)?)
927    }
928}
929
930#[derive(Serialize, Deserialize, Debug)]
931pub struct Phones {
932    #[serde(flatten)]
933    keeper_fields: KeeperField,
934    #[serde(default = "default_boolean")]
935    required: bool,
936    #[serde(default = "default_boolean")]
937    privacy_screen: bool,
938    value: Vec<Phone>,
939}
940
941impl Phones {
942    /// ```ignore
943    ///     use keeper_secrets_manager_core::dto::field_structs;
944    ///     let phone1 = field_structs::Phone::new("1234567890".to_string(), None, None, None);
945    ///     let phone2 = field_structs::Phone::new("1234567891".to_string(), Some("US".to_string()), None, None);
946    ///     let phone3 = field_structs::Phone::new("1234567892".to_string(), None, Some("1".to_string()), None);
947    ///     let phones = vec![phone1, phone2, phone3];
948    ///     let phones_field = field_structs::Phones::new_phones(phones);
949    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
950    ///     login_new.append_standard_fields(phones_field);
951    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
952    /// ```
953    #[allow(clippy::new_ret_no_self)]
954    pub fn new(
955        value: Vec<Phone>,
956        label: Option<String>,
957        required: bool,
958        privacy_screen: bool,
959    ) -> KeeperField {
960        let phones_field = Phones::vec_phone_to_phones_string(value);
961        let mut keeper_field =
962            KeeperField::new(StandardFieldTypeEnum::PHONES.get_type().to_string(), label);
963        keeper_field.value = phones_field;
964        keeper_field.required = required;
965        keeper_field.privacy_screen = privacy_screen;
966        keeper_field
967    }
968
969    fn vec_phone_to_phones_string(mut value: Vec<Phone>) -> Value {
970        let phones_string = value
971            .iter_mut()
972            .map(|phone| phone.to_json().unwrap())
973            .map(|phone| {
974                Value::from_str(phone.as_str())
975                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
976                    .unwrap()
977            })
978            .collect::<Vec<Value>>();
979        Value::Array(phones_string)
980    }
981
982    pub fn new_phones(value: Vec<Phone>) -> KeeperField {
983        Phones::new(value, None, false, false)
984    }
985}
986
987#[derive(Serialize, Deserialize, Debug)]
988pub struct Secret {
989    #[serde(flatten)]
990    keeper_fields: KeeperField,
991    #[serde(default = "default_boolean")]
992    required: bool,
993    #[serde(default = "default_boolean")]
994    privacy_screen: bool,
995    value: Vec<String>,
996}
997
998impl Secret {
999    ///```ignore
1000    ///     use keeper_secrets_manager_core::dto::field_structs;
1001    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
1002    ///     let secret = field_structs::Secret::new("Dummy secret".to_string(), Some("secret".to_string()), true, false);
1003    ///     login_new.append_custom_field(secret);
1004    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
1005    /// ```
1006    #[allow(clippy::new_ret_no_self)]
1007    pub fn new(
1008        value: String,
1009        label: Option<String>,
1010        required: bool,
1011        privacy_screen: bool,
1012    ) -> KeeperField {
1013        let mut keeper_field =
1014            KeeperField::new(StandardFieldTypeEnum::SECRET.get_type().to_string(), label);
1015        keeper_field.value = string_to_value_array(value);
1016        keeper_field.required = required;
1017        keeper_field.privacy_screen = privacy_screen;
1018        keeper_field
1019    }
1020
1021    pub fn new_secret(value: String) -> KeeperField {
1022        Secret::new(value, None, false, false)
1023    }
1024}
1025
1026#[derive(Serialize, Deserialize, Debug)]
1027pub struct SecureNote {
1028    #[serde(flatten)]
1029    keeper_fields: KeeperField,
1030    #[serde(default = "default_boolean")]
1031    required: bool,
1032    #[serde(default = "default_boolean")]
1033    privacy_screen: bool,
1034    value: Vec<String>,
1035}
1036
1037impl SecureNote {
1038    /// ```ignore
1039    ///     use keeper_secrets_manager_core::dto::field_structs;
1040    ///     let mut login_new = RecordCreate::new("login".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
1041    ///     let secret_note = field_structs::SecureNote::new("This is a sample note".to_string(), None, true, false);
1042    ///     login_new.append_standard_fields(secret_note);
1043    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
1044    /// ```
1045    #[allow(clippy::new_ret_no_self)]
1046    pub fn new(
1047        value: String,
1048        label: Option<String>,
1049        required: bool,
1050        privacy_screen: bool,
1051    ) -> KeeperField {
1052        let mut keeper_field = KeeperField::new(
1053            StandardFieldTypeEnum::SECURENOTE.get_type().to_string(),
1054            label,
1055        );
1056        keeper_field.value = string_to_value_array(value);
1057        keeper_field.required = required;
1058        keeper_field.privacy_screen = privacy_screen;
1059        keeper_field
1060    }
1061
1062    pub fn new_secure_note(value: String) -> KeeperField {
1063        SecureNote::new(value, None, false, false)
1064    }
1065}
1066#[derive(Serialize, Deserialize, Debug)]
1067pub struct Note {
1068    #[serde(flatten)]
1069    keeper_fields: KeeperField,
1070    #[serde(default = "default_boolean")]
1071    required: bool,
1072    #[serde(default = "default_boolean")]
1073    privacy_screen: bool,
1074    value: Vec<String>,
1075}
1076
1077impl Note {
1078    #[allow(clippy::new_ret_no_self)]
1079    pub fn new(value: String, required: bool, privacy_screen: bool) -> KeeperField {
1080        let mut keeper_field =
1081            KeeperField::new(StandardFieldTypeEnum::NOTE.get_type().to_string(), None);
1082        keeper_field.value = string_to_value_array(value);
1083        keeper_field.required = required;
1084        keeper_field.privacy_screen = privacy_screen;
1085        keeper_field
1086    }
1087}
1088
1089#[derive(Serialize, Deserialize, Debug)]
1090pub struct AccountNumber {
1091    #[serde(flatten)]
1092    keeper_fields: KeeperField,
1093    value: String,
1094}
1095
1096impl AccountNumber {
1097    #[allow(clippy::new_ret_no_self)]
1098    pub fn new(value: String) -> KeeperField {
1099        let mut keeper_field = KeeperField::new(
1100            StandardFieldTypeEnum::ACCOUNTNUMBER.get_type().to_string(),
1101            None,
1102        );
1103        keeper_field.value = string_to_value_array(value);
1104        keeper_field.required = true;
1105        keeper_field.privacy_screen = false;
1106        keeper_field
1107    }
1108
1109    pub fn new_account_number(value: String) -> KeeperField {
1110        AccountNumber::new(value)
1111    }
1112}
1113
1114#[derive(Serialize, Deserialize, Debug)]
1115#[serde(rename_all = "camelCase")]
1116pub struct PaymentCard {
1117    #[serde(skip_serializing_if = "Option::is_none")]
1118    card_number: Option<String>, // Card number
1119    #[serde(skip_serializing_if = "Option::is_none")]
1120    card_expiration_date: Option<String>, // Expiration date
1121    #[serde(skip_serializing_if = "Option::is_none")]
1122    card_security_code: Option<String>, // Security code
1123}
1124
1125impl PaymentCard {
1126    /// card_expiration_date should be in format of MM/YYYY else it wont reflect correctly in your record.
1127    pub fn new(
1128        card_number: Option<String>,
1129        card_expiration_date: Option<String>,
1130        card_security_code: Option<String>,
1131    ) -> Self {
1132        PaymentCard {
1133            card_number,
1134            card_expiration_date,
1135            card_security_code,
1136        }
1137    }
1138
1139    pub fn to_json(&self) -> Result<String, KSMRError> {
1140        Ok(serde_json::to_string(self)?)
1141    }
1142}
1143
1144#[derive(Serialize, Deserialize, Debug)]
1145pub struct PaymentCards {
1146    #[serde(flatten)]
1147    keeper_fields: KeeperField,
1148    #[serde(default = "default_boolean")]
1149    required: bool,
1150    #[serde(default = "default_boolean")]
1151    privacy_screen: bool,
1152    value: Vec<PaymentCard>,
1153}
1154
1155impl PaymentCards {
1156    /// Note that only one address can be given for a record of address type and if you want more than one address, then you have to give it as addressRef field
1157    /// ```ignore
1158    ///     let mut bank_card_record = RecordCreate::new(DefaultRecordType::BankCard.get_type().to_string(), "samplebankcard1".to_string(), Some("dummy_notes_changed".to_string()));
1159    ///     let payment_card = field_structs::PaymentCard::new(Some("8878881234211432".to_string()), Some("".to_string()), Some("1244".to_string()));
1160    ///     let payment_cards = field_structs::PaymentCards::new_payment_cards(payment_card);
1161    ///     bank_card_record.append_standard_fields(payment_cards);
1162    ///     let created_record = secrets_manager.create_secret("<some_folder_uid>".to_string(), bank_card_record);
1163    /// ```
1164    #[allow(clippy::new_ret_no_self)]
1165    pub fn new(
1166        value: PaymentCard,
1167        label: Option<String>,
1168        required: bool,
1169        privacy_screen: bool,
1170    ) -> KeeperField {
1171        let value_string = Value::from_str(value.to_json().unwrap().as_str()).unwrap();
1172        let cards_field = value_to_value_array(value_string);
1173        let mut keeper_field = KeeperField::new(
1174            StandardFieldTypeEnum::PAYMENTCARDS.get_type().to_string(),
1175            label,
1176        );
1177        keeper_field.value = cards_field;
1178        keeper_field.required = required;
1179        keeper_field.privacy_screen = privacy_screen;
1180        keeper_field
1181    }
1182
1183    pub fn new_payment_cards(value: PaymentCard) -> KeeperField {
1184        PaymentCards::new(value, None, false, false)
1185    }
1186}
1187
1188#[derive(Serialize, Deserialize, Debug, PartialEq)]
1189pub enum AccountType {
1190    Savings,
1191    Checking,
1192    Other,
1193}
1194
1195#[derive(Serialize, Deserialize, Debug)]
1196#[serde(rename_all = "camelCase")]
1197pub struct BankAccount {
1198    account_type: AccountType,  // Account type (e.g., Checking, Savings)
1199    routing_number: String,     // Routing number
1200    account_number: String,     // Account number
1201    other_type: Option<String>, // Other account type
1202}
1203
1204impl BankAccount {
1205    ///let bank_account_field = field_structs::BankAccount::new(AccountType::Savings, "1122334455".to_string(), "33445566778".to_string(), None, Some("Account Field Label".to_string()));
1206    /// let mut bank_account_record = RecordCreate::new(DefaultRecordType::BankAccounts.get_type().to_string(), "Bank Account Reference".to_string(), Some("sum notes".to_string()));
1207    /// bank_account_record.append_standard_fields(bank_account_field);
1208    /// let created_record = secrets_manager.create_secret("folder_uid".to_string(), bank_account_record);
1209    #[allow(clippy::new_ret_no_self)]
1210    pub fn new(
1211        account_type: AccountType,
1212        routing_number: String,
1213        account_number: String,
1214        other_type: Option<String>,
1215        label: Option<String>,
1216    ) -> KeeperField {
1217        let oth_type = match account_type == AccountType::Other {
1218            true => Some(
1219                other_type
1220                    .unwrap_or_else(|| "Other".to_string())
1221                    .to_string(),
1222            ),
1223            false => None,
1224        };
1225        let bank_account = BankAccount {
1226            account_type,
1227            routing_number,
1228            account_number,
1229            other_type: oth_type,
1230        };
1231
1232        // Serialize the BankAccount into a JSON string
1233        let account_json = Value::from_str(bank_account.to_json().unwrap().as_str()).unwrap();
1234
1235        // Convert the JSON string into a value array (assumes implementation of `string_to_value_array`)
1236        let account_value = value_to_value_array(account_json);
1237        let mut keeper_field = KeeperField::new(
1238            StandardFieldTypeEnum::BANKACCOUNT.get_type().to_string(),
1239            label,
1240        );
1241        keeper_field.value = account_value;
1242        keeper_field.required = false;
1243        keeper_field.privacy_screen = false;
1244        keeper_field
1245    }
1246
1247    fn to_json(&self) -> Result<String, KSMRError> {
1248        Ok(serde_json::to_string(self)?)
1249    }
1250}
1251
1252#[derive(Serialize, Deserialize, Debug)]
1253pub struct BankAccounts {
1254    #[serde(flatten)]
1255    keeper_fields: KeeperField,
1256    #[serde(default = "default_boolean")]
1257    required: bool,
1258    #[serde(default = "default_boolean")]
1259    privacy_screen: bool,
1260    value: Vec<BankAccount>,
1261}
1262
1263impl BankAccounts {
1264    pub fn new(
1265        value: BankAccount,
1266        label: Option<String>,
1267        required: bool,
1268        privacy_screen: bool,
1269    ) -> Self {
1270        BankAccounts {
1271            keeper_fields: KeeperField::new(
1272                StandardFieldTypeEnum::BANKACCOUNT.get_type().to_string(),
1273                label,
1274            ),
1275            value: vec![value],
1276            required,
1277            privacy_screen,
1278        }
1279    }
1280
1281    pub fn new_bank_accounts(value: BankAccount) -> Self {
1282        BankAccounts::new(value, None, false, false)
1283    }
1284}
1285
1286#[derive(Serialize, Deserialize, Debug)]
1287#[serde(rename_all = "camelCase")]
1288pub struct KeyPair {
1289    public_key: Option<String>,  // Public key
1290    private_key: Option<String>, // Private key
1291}
1292
1293impl KeyPair {
1294    pub fn new(public_key: Option<String>, private_key: Option<String>) -> Self {
1295        KeyPair {
1296            public_key,
1297            private_key,
1298        }
1299    }
1300
1301    pub fn to_json(&self) -> Result<String, KSMRError> {
1302        Ok(serde_json::to_string(self)?)
1303    }
1304}
1305
1306#[derive(Serialize, Deserialize, Debug)]
1307pub struct KeyPairs {
1308    #[serde(flatten)]
1309    keeper_fields: KeeperField,
1310    #[serde(default = "default_boolean")]
1311    required: bool,
1312    #[serde(default = "default_boolean")]
1313    privacy_screen: bool,
1314    value: Vec<KeyPair>,
1315}
1316
1317impl KeyPairs {
1318    ///```ignore
1319    ///     let mut new_record = RecordCreate::new(DefaultRecordType::SSHKeys.get_type().to_string(), "sample ssh key 1".to_string(), None);
1320    ///     let key_pair = field_structs::KeyPair::new(Some("jkaghdsjabd354afzdc".to_string()), Some("jgFdjavbf34f6f".to_string()));
1321    ///     let key_pair_array = vec![key_pair];
1322    ///     let key_pairs_field = field_structs::KeyPairs::new(key_pair_array, None, true, false);
1323    ///     new_record.append_standard_fields(key_pairs_field);
1324    ///     let created_record: Result<String, KSMRError> = secrets_manager.create_secret("Yi_OxwTV2tdBWi-_Aegs_w".to_string(), new_record);
1325    /// ```
1326    #[allow(clippy::new_ret_no_self)]
1327    pub fn new(
1328        value: Vec<KeyPair>,
1329        label: Option<String>,
1330        required: bool,
1331        privacy_screen: bool,
1332    ) -> KeeperField {
1333        let mut keeper_field = KeeperField::new(
1334            StandardFieldTypeEnum::KEYPAIRS.get_type().to_string(),
1335            label,
1336        );
1337        keeper_field.required = required;
1338        keeper_field.privacy_screen = privacy_screen;
1339        keeper_field.value = KeyPairs::vec_key_pair_to_key_pairs_string(value);
1340        keeper_field
1341    }
1342
1343    fn vec_key_pair_to_key_pairs_string(mut value: Vec<KeyPair>) -> Value {
1344        let key_pairs_string: Vec<Value> = value
1345            .iter_mut()
1346            .map(|key_pair| key_pair.to_json().unwrap())
1347            .map(|key_pair| {
1348                Value::from_str(key_pair.as_str())
1349                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
1350                    .unwrap()
1351            })
1352            .collect::<Vec<Value>>();
1353        Value::Array(key_pairs_string)
1354    }
1355}
1356
1357#[derive(Serialize, Deserialize, Debug)]
1358#[serde(rename_all = "camelCase")]
1359pub struct Host {
1360    host_name: Option<String>, // Hostname
1361    port: Option<String>,      // Port number
1362}
1363
1364impl Host {
1365    pub fn new(host_name: Option<String>, port: Option<String>) -> Self {
1366        Host { host_name, port }
1367    }
1368
1369    pub fn to_json(&self) -> Result<String, KSMRError> {
1370        Ok(serde_json::to_string(self)?)
1371    }
1372}
1373
1374#[derive(Serialize, Deserialize, Debug)]
1375pub struct Hosts {
1376    #[serde(flatten)]
1377    keeper_fields: KeeperField,
1378    #[serde(default = "default_boolean")]
1379    required: bool,
1380    #[serde(default = "default_boolean")]
1381    privacy_screen: bool,
1382    value: Vec<Host>,
1383}
1384
1385impl Hosts {
1386    #[allow(clippy::new_ret_no_self)]
1387    pub fn new(value: Vec<Host>) -> KeeperField {
1388        let mut keeper_field =
1389            KeeperField::new(StandardFieldTypeEnum::HOSTS.get_type().to_string(), None);
1390        keeper_field.value = Hosts::vec_host_to_hosts_string(value);
1391        keeper_field
1392    }
1393
1394    fn vec_host_to_hosts_string(mut value: Vec<Host>) -> Value {
1395        let hosts_string: Vec<Value> = value
1396            .iter_mut()
1397            .map(|host| host.to_json().unwrap())
1398            .map(|host| {
1399                Value::from_str(host.as_str())
1400                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
1401                    .unwrap()
1402            })
1403            .collect::<Vec<Value>>();
1404        Value::Array(hosts_string)
1405    }
1406
1407    pub fn new_hosts(value: Vec<Host>) -> KeeperField {
1408        Hosts::new(value)
1409    }
1410}
1411#[derive(Serialize, Deserialize, Debug)]
1412pub struct Address {
1413    #[serde(default = "default_empty_option_string")]
1414    street1: Option<String>, // Street 1
1415    #[serde(default = "default_empty_option_string")]
1416    street2: Option<String>, // Street 2
1417    #[serde(default = "default_empty_option_string")]
1418    city: Option<String>, // City
1419    #[serde(default = "default_empty_option_string")]
1420    state: Option<String>, // State
1421    country: String, // Country
1422    #[serde(default = "default_empty_option_string")]
1423    zip: Option<String>, // Zip code
1424}
1425
1426impl Address {
1427    pub fn new(
1428        street1: Option<String>,
1429        street2: Option<String>,
1430        city: Option<String>,
1431        state: Option<String>,
1432        country: String,
1433        zip: Option<String>,
1434    ) -> Result<Self, KSMRError> {
1435        let country_parsed: Country =
1436            match Country::from_string(&country) {
1437                Some(country) => country,
1438                None => return Err(KSMRError::RecordDataError(
1439                    "Country is a mandatory field for address dn country has to be a valid field"
1440                        .to_string(),
1441                )),
1442            };
1443        Ok(Address {
1444            street1,
1445            street2,
1446            city,
1447            state,
1448            country: country_parsed.to_string(),
1449            zip,
1450        })
1451    }
1452
1453    fn to_json(&self) -> Result<String, KSMRError> {
1454        Ok(serde_json::to_string(self)?)
1455    }
1456}
1457
1458#[derive(Serialize, Deserialize, Debug)]
1459pub struct Addresses {
1460    #[serde(flatten)]
1461    keeper_fields: KeeperField,
1462    #[serde(default = "default_boolean")]
1463    required: bool,
1464    #[serde(default = "default_boolean")]
1465    privacy_screen: bool,
1466    value: Value,
1467}
1468
1469impl Addresses {
1470    ///```ignore
1471    ///     use keeper_secrets_manager_core::dto::field_structs;
1472    ///     let mut login_new = RecordCreate::new("address".to_string(), "custom_login_new_login_create".to_string(), Some("dummy_notes_changed".to_string()));
1473    ///     let address: Address = field_structs::Address::new(Some("ABC".to_string()), Some("PQR".to_string()), Some("Pune".to_string()), Some("Maharashtra".to_string()), Some("baHrAin".to_string()), Some("411018".to_string()));
1474    ///     let addresses: Vec<Address> = vec![address];
1475    ///     let address_field: KeeperField = field_structs::Addresses::new(addresses, None, false, false);
1476    ///     login_new.append_custom_field(address_field);
1477    ///     let created_record :Result<String, KSMRError> = secrets_manager.create_secret("some_folder_uid".to_string(), login_new);
1478    /// ```
1479    #[allow(clippy::new_ret_no_self)]
1480    pub fn new(
1481        value: Vec<Address>,
1482        label: Option<String>,
1483        required: bool,
1484        privacy_screen: bool,
1485    ) -> KeeperField {
1486        let addresses_value = Addresses::vec_address_to_addresses_string(value);
1487        let mut keeper_field =
1488            KeeperField::new(StandardFieldTypeEnum::ADDRESS.get_type().to_string(), label);
1489        keeper_field.value = addresses_value;
1490        keeper_field.required = required;
1491        keeper_field.privacy_screen = privacy_screen;
1492        keeper_field
1493    }
1494
1495    fn vec_address_to_addresses_string(mut value: Vec<Address>) -> Value {
1496        let addresses_string: Vec<Value> = value
1497            .iter_mut()
1498            .map(|address| address.to_json().unwrap())
1499            .map(|address| {
1500                Value::from_str(address.as_str())
1501                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
1502                    .unwrap()
1503            })
1504            .collect::<Vec<Value>>();
1505        Value::Array(addresses_string)
1506    }
1507
1508    pub fn new_addresses(value: Vec<Address>) -> KeeperField {
1509        Addresses::new(value, None, false, false)
1510    }
1511}
1512
1513#[derive(Serialize, Deserialize, Debug)]
1514pub struct LicenseNumber {
1515    #[serde(flatten)]
1516    keeper_fields: KeeperField,
1517    #[serde(default = "default_boolean")]
1518    required: bool,
1519    #[serde(default = "default_boolean")]
1520    privacy_screen: bool,
1521    value: Vec<String>,
1522}
1523
1524impl LicenseNumber {
1525    #[allow(clippy::new_ret_no_self)]
1526    pub fn new(
1527        value: String,
1528        label: Option<String>,
1529        required: bool,
1530        privacy_screen: bool,
1531    ) -> KeeperField {
1532        let mut keeper_field = KeeperField::new(
1533            StandardFieldTypeEnum::LICENSENUMBER.get_type().to_string(),
1534            label,
1535        );
1536        keeper_field.value = string_to_value_array(value);
1537        keeper_field.required = required;
1538        keeper_field.privacy_screen = privacy_screen;
1539        keeper_field
1540    }
1541}
1542
1543#[derive(Serialize, Deserialize, Debug)]
1544pub struct RecordRef {
1545    #[serde(flatten)]
1546    keeper_fields: KeeperField,
1547    #[serde(default = "default_boolean")]
1548    required: bool,
1549    value: Vec<String>,
1550}
1551
1552impl RecordRef {
1553    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
1554        RecordRef {
1555            keeper_fields: KeeperField::new(
1556                StandardFieldTypeEnum::RECORDREF.get_type().to_string(),
1557                label,
1558            ),
1559            value: vec![value],
1560            required,
1561        }
1562    }
1563
1564    pub fn new_record_ref(value: String) -> Self {
1565        RecordRef::new(value, None, false)
1566    }
1567}
1568
1569#[derive(Serialize, Deserialize, Debug)]
1570pub struct Schedule {
1571    #[serde(default = "default_empty_string")]
1572    schedule_type: String,
1573    #[serde(default = "default_empty_string")]
1574    cron: String,
1575    #[serde(default = "default_empty_string")]
1576    time: String,
1577    #[serde(default = "default_empty_string")]
1578    tz: String,
1579    #[serde(default = "default_empty_string")]
1580    weekday: String,
1581    #[serde(default = "default_empty_number_i32")]
1582    interval_count: i32,
1583}
1584
1585impl Schedule {
1586    pub fn new(
1587        schedule_type: String,
1588        cron: String,
1589        time: String,
1590        tz: String,
1591        weekday: String,
1592        interval_count: i32,
1593    ) -> Self {
1594        Schedule {
1595            schedule_type,
1596            cron,
1597            time,
1598            tz,
1599            weekday,
1600            interval_count,
1601        }
1602    }
1603
1604    pub fn new_schedule(schedule_type: String) -> Self {
1605        Schedule::new(
1606            schedule_type,
1607            String::new(),
1608            String::new(),
1609            String::new(),
1610            String::new(),
1611            0,
1612        )
1613    }
1614}
1615
1616#[derive(Serialize, Deserialize, Debug)]
1617pub struct Schedules {
1618    #[serde(flatten)]
1619    keeper_fields: KeeperField,
1620    #[serde(default = "default_boolean")]
1621    required: bool,
1622    value: Vec<Schedule>,
1623}
1624
1625impl Schedules {
1626    pub fn new(value: Schedule, label: Option<String>, required: bool) -> Self {
1627        Schedules {
1628            keeper_fields: KeeperField::new(
1629                StandardFieldTypeEnum::SCHEDULES.get_type().to_string(),
1630                label,
1631            ),
1632            value: vec![value],
1633            required,
1634        }
1635    }
1636
1637    pub fn new_schedules(value: Schedule) -> Self {
1638        Schedules::new(value, None, false)
1639    }
1640}
1641
1642#[derive(Serialize, Deserialize, Debug)]
1643pub struct DirectoryType {
1644    #[serde(flatten)]
1645    keeper_fields: KeeperField,
1646    #[serde(default = "default_boolean")]
1647    required: bool,
1648    value: Vec<String>,
1649}
1650
1651impl DirectoryType {
1652    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
1653        DirectoryType {
1654            keeper_fields: KeeperField::new(
1655                StandardFieldTypeEnum::DIRECTORYTYPE.get_type().to_string(),
1656                label,
1657            ),
1658            value: vec![value],
1659            required,
1660        }
1661    }
1662
1663    pub fn new_directory_type(value: String) -> Self {
1664        DirectoryType::new(value, None, false)
1665    }
1666}
1667
1668#[derive(Serialize, Deserialize, Debug)]
1669pub struct DatabaseType {
1670    #[serde(flatten)]
1671    keeper_fields: KeeperField,
1672    #[serde(default = "default_boolean")]
1673    required: bool,
1674    value: Vec<String>,
1675}
1676
1677impl DatabaseType {
1678    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
1679        DatabaseType {
1680            keeper_fields: KeeperField::new(
1681                StandardFieldTypeEnum::DATABASETYPE.get_type().to_string(),
1682                label,
1683            ),
1684            value: vec![value],
1685            required,
1686        }
1687    }
1688
1689    pub fn new_database_type(value: String) -> Self {
1690        DatabaseType::new(value, None, false)
1691    }
1692}
1693
1694#[derive(Serialize, Deserialize, Debug)]
1695pub struct PamHostname {
1696    #[serde(flatten)]
1697    keeper_fields: KeeperField,
1698    #[serde(default = "default_boolean")]
1699    required: bool,
1700    #[serde(default = "default_boolean")]
1701    privacy_screen: bool,
1702    value: Vec<Host>,
1703}
1704
1705impl PamHostname {
1706    pub fn new(value: Host, label: Option<String>, required: bool, privacy_screen: bool) -> Self {
1707        PamHostname {
1708            keeper_fields: KeeperField::new(
1709                StandardFieldTypeEnum::PAMHOSTNAME.get_type().to_string(),
1710                label,
1711            ),
1712            required,
1713            privacy_screen,
1714            value: vec![value],
1715        }
1716    }
1717
1718    pub fn new_pam_hostname(value: Host) -> Self {
1719        PamHostname::new(value, None, false, false)
1720    }
1721}
1722
1723#[derive(Serialize, Deserialize, Debug)]
1724pub struct AllowedSettings {
1725    #[serde(default = "default_boolean")]
1726    connections: bool,
1727    #[serde(default = "default_boolean")]
1728    port_forwards: bool,
1729    #[serde(default = "default_boolean")]
1730    rotation: bool,
1731    #[serde(default = "default_boolean")]
1732    session_recording: bool,
1733    #[serde(default = "default_boolean")]
1734    typescript_recording: bool,
1735}
1736
1737impl AllowedSettings {
1738    pub fn new(
1739        connections: bool,
1740        port_forwards: bool,
1741        rotation: bool,
1742        session_recording: bool,
1743        typescript_recording: bool,
1744    ) -> Self {
1745        AllowedSettings {
1746            connections,
1747            port_forwards,
1748            rotation,
1749            session_recording,
1750            typescript_recording,
1751        }
1752    }
1753}
1754
1755#[derive(Serialize, Deserialize, Debug)]
1756pub struct PamResource {
1757    #[serde(default = "default_empty_string")]
1758    controller_uid: String,
1759    #[serde(default = "default_empty_string")]
1760    folder_uid: String,
1761    resource_ref: Vec<String>,
1762    allowed_settings: AllowedSettings,
1763}
1764
1765impl PamResource {
1766    pub fn new(
1767        controller_uid: String,
1768        folder_uid: String,
1769        resource_ref: Vec<String>,
1770        allowed_settings: AllowedSettings,
1771    ) -> Self {
1772        PamResource {
1773            controller_uid,
1774            folder_uid,
1775            resource_ref,
1776            allowed_settings,
1777        }
1778    }
1779}
1780
1781#[derive(Serialize, Deserialize, Debug)]
1782pub struct PamResources {
1783    #[serde(flatten)]
1784    keeper_fields: KeeperField,
1785    #[serde(default = "default_boolean")]
1786    required: bool,
1787    value: Vec<PamResource>,
1788}
1789
1790impl PamResources {
1791    pub fn new(value: PamResource, label: Option<String>, required: bool) -> Self {
1792        PamResources {
1793            keeper_fields: KeeperField::new(
1794                StandardFieldTypeEnum::PAMRESOURCES.get_type().to_string(),
1795                label,
1796            ),
1797            required,
1798            value: vec![value],
1799        }
1800    }
1801
1802    pub fn new_pam_resources(value: PamResource) -> Self {
1803        PamResources::new(value, None, false)
1804    }
1805}
1806
1807#[derive(Serialize, Deserialize, Debug)]
1808pub struct Checkbox {
1809    #[serde(flatten)]
1810    keeper_fields: KeeperField,
1811    #[serde(default = "default_boolean")]
1812    required: bool,
1813    value: Vec<bool>,
1814}
1815
1816impl Checkbox {
1817    pub fn new(value: bool, label: Option<String>, required: bool) -> Self {
1818        Checkbox {
1819            keeper_fields: KeeperField::new(
1820                StandardFieldTypeEnum::CHECKBOX.get_type().to_string(),
1821                label,
1822            ),
1823            required,
1824            value: vec![value],
1825        }
1826    }
1827
1828    pub fn new_checkbox(value: bool) -> Self {
1829        Checkbox::new(value, None, false)
1830    }
1831}
1832
1833#[derive(Serialize, Deserialize, Debug)]
1834pub struct Script {
1835    #[serde(default = "default_empty_string")]
1836    file_ref: String,
1837    #[serde(default = "default_empty_string")]
1838    command: String,
1839    record_ref: Vec<String>,
1840}
1841
1842impl Script {
1843    pub fn new(file_ref: String, command: String, record_ref: Vec<String>) -> Self {
1844        Script {
1845            file_ref,
1846            command,
1847            record_ref,
1848        }
1849    }
1850}
1851
1852#[derive(Serialize, Deserialize, Debug)]
1853pub struct Scripts {
1854    #[serde(flatten)]
1855    keeper_fields: KeeperField,
1856    #[serde(default = "default_boolean")]
1857    required: bool,
1858    #[serde(default = "default_boolean")]
1859    privacy_screen: bool,
1860    value: Vec<Script>,
1861}
1862
1863impl Scripts {
1864    pub fn new(value: Script, label: Option<String>, required: bool, privacy_screen: bool) -> Self {
1865        Scripts {
1866            keeper_fields: KeeperField::new(
1867                StandardFieldTypeEnum::SCRIPTS.get_type().to_string(),
1868                label,
1869            ),
1870            required,
1871            privacy_screen,
1872            value: vec![value],
1873        }
1874    }
1875
1876    pub fn new_scripts(value: Script) -> Self {
1877        Scripts::new(value, None, false, false)
1878    }
1879}
1880
1881#[derive(Serialize, Deserialize, Debug, Default)]
1882pub struct PasskeyPrivateKey {
1883    #[serde(default = "default_empty_string")]
1884    crv: String,
1885    #[serde(default = "default_empty_string")]
1886    d: String,
1887    #[serde(default = "default_boolean")]
1888    ext: bool,
1889    #[serde(default)]
1890    key_ops: Vec<String>,
1891    #[serde(default = "default_empty_string")]
1892    kty: String,
1893    #[serde(default = "default_empty_string")]
1894    x: String,
1895    #[serde(default = "default_empty_number_i64")]
1896    y: i64,
1897}
1898
1899impl PasskeyPrivateKey {
1900    pub fn new(
1901        crv: String,
1902        d: String,
1903        ext: bool,
1904        key_ops: Vec<String>,
1905        kty: String,
1906        x: String,
1907        y: i64,
1908    ) -> Self {
1909        PasskeyPrivateKey {
1910            crv,
1911            d,
1912            ext,
1913            key_ops,
1914            kty,
1915            x,
1916            y,
1917        }
1918    }
1919}
1920
1921#[derive(Serialize, Deserialize, Debug)]
1922pub struct Passkey {
1923    #[serde(default)]
1924    private_key: PasskeyPrivateKey,
1925    #[serde(default = "default_empty_string")]
1926    credential_id: String,
1927    #[serde(default = "default_empty_number_i64")]
1928    sign_count: i64,
1929    #[serde(default = "default_empty_string")]
1930    user_id: String,
1931    #[serde(default = "default_empty_string")]
1932    relying_party: String,
1933    #[serde(default = "default_empty_string")]
1934    username: String,
1935    #[serde(default = "default_empty_number_i64")]
1936    created_date: i64,
1937}
1938
1939impl Passkey {
1940    pub fn new(
1941        private_key: PasskeyPrivateKey,
1942        credential_id: String,
1943        sign_count: i64,
1944        user_id: String,
1945        relying_party: String,
1946        username: String,
1947        created_date: i64,
1948    ) -> Self {
1949        Passkey {
1950            private_key,
1951            credential_id,
1952            sign_count,
1953            user_id,
1954            relying_party,
1955            username,
1956            created_date,
1957        }
1958    }
1959}
1960
1961// impl Default for PasskeyPrivateKey {
1962//     fn default() -> Self {
1963//         PasskeyPrivateKey {
1964//             crv: String::new(),
1965//             d: String::new(),
1966//             ext: false,
1967//             key_ops: Vec::new(),
1968//             kty: String::new(),
1969//             x: String::new(),
1970//             y: 0,
1971//         }
1972//     }
1973// }
1974
1975#[derive(Serialize, Deserialize, Debug)]
1976pub struct Passkeys {
1977    #[serde(flatten)]
1978    keeper_fields: KeeperField,
1979    #[serde(default = "default_boolean")]
1980    required: bool,
1981    value: Vec<Passkey>,
1982}
1983
1984impl Passkeys {
1985    pub fn new(value: Passkey, label: Option<String>, required: bool) -> Self {
1986        Passkeys {
1987            keeper_fields: KeeperField::new(
1988                StandardFieldTypeEnum::PASSKEYS.get_type().to_string(),
1989                label,
1990            ),
1991            required,
1992            value: vec![value],
1993        }
1994    }
1995
1996    pub fn new_passkeys(value: Passkey) -> Self {
1997        Passkeys::new(value, None, false)
1998    }
1999}
2000
2001#[derive(Serialize, Deserialize, Debug)]
2002pub struct IsSsidHidden {
2003    #[serde(flatten)]
2004    pub keeper_fields: KeeperField,
2005    #[serde(default = "default_boolean")]
2006    pub required: bool,
2007    #[serde(default)]
2008    pub value: Vec<bool>,
2009}
2010
2011impl IsSsidHidden {
2012    pub fn new(value: bool, label: Option<String>, required: bool) -> Self {
2013        IsSsidHidden {
2014            keeper_fields: KeeperField::new(
2015                StandardFieldTypeEnum::ISSSIDHIDDEN.get_type().to_string(),
2016                label,
2017            ),
2018            required,
2019            value: vec![value],
2020        }
2021    }
2022
2023    pub fn new_is_ssid_hidden(value: bool) -> Self {
2024        IsSsidHidden::new(value, None, false)
2025    }
2026}
2027
2028#[derive(Serialize, Deserialize, Debug)]
2029pub struct WifiEncryption {
2030    #[serde(flatten)]
2031    pub keeper_fields: KeeperField,
2032    #[serde(default = "default_boolean")]
2033    pub required: bool,
2034    #[serde(default)]
2035    pub value: Vec<String>,
2036}
2037
2038impl WifiEncryption {
2039    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
2040        WifiEncryption {
2041            keeper_fields: KeeperField::new(
2042                StandardFieldTypeEnum::WIFIENCRYPTION.get_type().to_string(),
2043                label,
2044            ),
2045            required,
2046            value: vec![value],
2047        }
2048    }
2049
2050    pub fn new_wifi_encryption(value: String) -> Self {
2051        WifiEncryption::new(value, None, false)
2052    }
2053}
2054
2055#[derive(Serialize, Deserialize, Debug)]
2056pub struct Dropdown {
2057    #[serde(flatten)]
2058    pub keeper_fields: KeeperField,
2059    #[serde(default = "default_boolean")]
2060    pub required: bool,
2061    #[serde(default = "default_empty_vector")]
2062    pub value: Vec<String>,
2063}
2064
2065impl Dropdown {
2066    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
2067        Dropdown {
2068            keeper_fields: KeeperField::new(
2069                StandardFieldTypeEnum::DROPDOWN.get_type().to_string(),
2070                label,
2071            ),
2072            required,
2073            value: vec![value],
2074        }
2075    }
2076
2077    pub fn new_dropdown(value: String) -> Self {
2078        Dropdown::new(value, None, false)
2079    }
2080}
2081
2082#[derive(Serialize, Deserialize, Debug)]
2083pub struct RbiUrl {
2084    #[serde(flatten)]
2085    pub keeper_fields: KeeperField,
2086    #[serde(default = "default_boolean")]
2087    pub required: bool,
2088    #[serde(default = "default_empty_vector")]
2089    pub value: Vec<String>,
2090}
2091
2092impl RbiUrl {
2093    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
2094        RbiUrl {
2095            keeper_fields: KeeperField::new(
2096                StandardFieldTypeEnum::RBIURL.get_type().to_string(),
2097                label,
2098            ),
2099            required,
2100            value: vec![value],
2101        }
2102    }
2103
2104    pub fn new_rbi_url(value: String) -> Self {
2105        RbiUrl::new(value, None, false)
2106    }
2107}
2108
2109#[derive(Serialize, Deserialize, Debug)]
2110#[serde(rename_all = "camelCase")]
2111pub struct AppFiller {
2112    #[serde(default = "default_empty_option_string")]
2113    pub application_title: Option<String>,
2114    #[serde(default = "default_empty_option_string")]
2115    pub content_filter: Option<String>,
2116    #[serde(default = "default_empty_option_string")]
2117    pub macro_sequence: Option<String>,
2118}
2119
2120impl AppFiller {
2121    pub fn new(
2122        application_title: Option<String>,
2123        content_filter: Option<String>,
2124        macro_sequence: Option<String>,
2125    ) -> Self {
2126        AppFiller {
2127            application_title,
2128            content_filter,
2129            macro_sequence,
2130        }
2131    }
2132
2133    fn to_json(&self) -> Result<String, KSMRError> {
2134        Ok(serde_json::to_string(self)?)
2135    }
2136}
2137
2138#[derive(Serialize, Deserialize, Debug)]
2139pub struct AppFillers {
2140    #[serde(flatten)]
2141    pub keeper_fields: KeeperField,
2142    #[serde(default = "default_boolean")]
2143    pub required: bool,
2144    #[serde(default = "default_boolean")]
2145    pub privacy_screen: bool,
2146    #[serde(default = "default_empty_vector")]
2147    pub value: Vec<AppFiller>,
2148}
2149
2150impl AppFillers {
2151    #[allow(clippy::new_ret_no_self)]
2152    pub fn new(
2153        value: Vec<AppFiller>,
2154        label: Option<String>,
2155        required: bool,
2156        privacy_screen: bool,
2157    ) -> KeeperField {
2158        let mut keeper_field = KeeperField::new(
2159            StandardFieldTypeEnum::APPFILLERS.get_type().to_string(),
2160            label,
2161        );
2162        keeper_field.required = required;
2163        keeper_field.privacy_screen = privacy_screen;
2164        keeper_field.value = AppFillers::vec_app_filler_to_app_fillers_string(value);
2165        keeper_field
2166    }
2167
2168    fn vec_app_filler_to_app_fillers_string(mut value: Vec<AppFiller>) -> Value {
2169        let app_fillers_string: Vec<Value> = value
2170            .iter_mut()
2171            .map(|app_filler| app_filler.to_json().unwrap())
2172            .map(|app_filler| {
2173                Value::from_str(app_filler.as_str())
2174                    .map_err(|err| KSMRError::DeserializationError(err.to_string()))
2175                    .unwrap()
2176            })
2177            .collect::<Vec<Value>>();
2178        Value::Array(app_fillers_string)
2179    }
2180}
2181
2182#[derive(Serialize, Deserialize, Debug)]
2183pub struct PamRbiConnection {
2184    #[serde(default)]
2185    pub protocol: Option<String>,
2186    #[serde(default)]
2187    pub user_records: Vec<String>,
2188    #[serde(default)]
2189    pub allow_url_manipulation: bool,
2190    #[serde(default)]
2191    pub allowed_url_patterns: Option<String>,
2192    #[serde(default)]
2193    pub allowed_resource_url_patterns: Option<String>,
2194    #[serde(default)]
2195    pub http_credentials_uid: Option<String>,
2196    #[serde(default)]
2197    pub autofill_configuration: Option<String>,
2198}
2199
2200impl PamRbiConnection {
2201    pub fn new(
2202        protocol: Option<String>,
2203        user_records: Vec<String>,
2204        allow_url_manipulation: bool,
2205        allowed_url_patterns: Option<String>,
2206        allowed_resource_url_patterns: Option<String>,
2207        http_credentials_uid: Option<String>,
2208        autofill_configuration: Option<String>,
2209    ) -> Self {
2210        PamRbiConnection {
2211            protocol,
2212            user_records,
2213            allow_url_manipulation,
2214            allowed_url_patterns,
2215            allowed_resource_url_patterns,
2216            http_credentials_uid,
2217            autofill_configuration,
2218        }
2219    }
2220}
2221
2222#[derive(Serialize, Deserialize, Debug)]
2223pub struct PamRemoteBrowserSetting {
2224    #[serde(default)]
2225    pub connection: Option<PamRbiConnection>,
2226}
2227
2228#[derive(Serialize, Deserialize, Debug)]
2229pub struct PamRemoteBrowserSettings {
2230    pub keeper_fields: KeeperField,
2231    #[serde(default)]
2232    pub required: bool,
2233    #[serde(default)]
2234    pub value: Vec<PamRemoteBrowserSetting>,
2235}
2236
2237impl PamRemoteBrowserSettings {
2238    pub fn new(value: PamRemoteBrowserSetting, label: Option<String>, required: bool) -> Self {
2239        PamRemoteBrowserSettings {
2240            keeper_fields: KeeperField::new(
2241                StandardFieldTypeEnum::PAMREMOTEBROWSERSETTINGS
2242                    .get_type()
2243                    .to_string(),
2244                label,
2245            ),
2246            required,
2247            value: vec![value],
2248        }
2249    }
2250
2251    pub fn new_pam_remote_browser_settings(value: PamRemoteBrowserSetting) -> Self {
2252        PamRemoteBrowserSettings::new(value, None, false)
2253    }
2254}
2255
2256#[derive(Serialize, Deserialize, Debug)]
2257pub struct PamSettingsPortForward {
2258    #[serde(default = "default_boolean")]
2259    pub reuse_port: bool,
2260    #[serde(default = "default_empty_string")]
2261    pub port: String,
2262}
2263
2264impl PamSettingsPortForward {
2265    pub fn new(port: String, reuse_port: bool) -> Self {
2266        PamSettingsPortForward { reuse_port, port }
2267    }
2268
2269    pub fn new_pam_settings_port_forward(port: String) -> Self {
2270        PamSettingsPortForward::new(port, false)
2271    }
2272}
2273
2274#[derive(Serialize, Deserialize, Debug)]
2275pub struct PamSettingsConnection {
2276    #[serde(default = "default_empty_option_string")]
2277    pub protocol: Option<String>,
2278    #[serde(default = "default_empty_vector")]
2279    pub user_records: Vec<String>,
2280    #[serde(default = "default_empty_option_string")]
2281    pub security: Option<String>,
2282    #[serde(default = "default_boolean")]
2283    pub ignore_cert: bool,
2284    #[serde(default = "default_empty_option_string")]
2285    pub resize_method: Option<String>,
2286    #[serde(default = "default_empty_option_string")]
2287    pub color_scheme: Option<String>,
2288}
2289
2290impl PamSettingsConnection {
2291    pub fn new(
2292        protocol: Option<String>,
2293        user_records: Vec<String>,
2294        security: Option<String>,
2295        ignore_cert: bool,
2296        resize_method: Option<String>,
2297        color_scheme: Option<String>,
2298    ) -> Self {
2299        PamSettingsConnection {
2300            protocol,
2301            user_records,
2302            security,
2303            ignore_cert,
2304            resize_method,
2305            color_scheme,
2306        }
2307    }
2308}
2309
2310#[derive(Serialize, Deserialize, Debug)]
2311pub struct PamSetting {
2312    pub port_forward: Vec<PamSettingsPortForward>,
2313    pub connection: Vec<PamSettingsConnection>,
2314}
2315
2316impl PamSetting {
2317    pub fn new(
2318        port_forward: Vec<PamSettingsPortForward>,
2319        connection: Vec<PamSettingsConnection>,
2320    ) -> Self {
2321        PamSetting {
2322            port_forward,
2323            connection,
2324        }
2325    }
2326}
2327
2328#[derive(Serialize, Deserialize, Debug)]
2329pub struct PamSettings {
2330    #[serde(flatten)]
2331    pub keeper_field: KeeperField,
2332    #[serde(default)]
2333    pub required: bool,
2334    #[serde(default)]
2335    pub value: Vec<PamSetting>,
2336}
2337
2338impl PamSettings {
2339    pub fn new(value: PamSetting, label: Option<String>, required: bool) -> Self {
2340        PamSettings {
2341            keeper_field: KeeperField::new(
2342                StandardFieldTypeEnum::PAMSETTINGS.get_type().to_string(),
2343                label,
2344            ),
2345            required,
2346            value: vec![value],
2347        }
2348    }
2349
2350    pub fn new_pam_settings(value: PamSetting) -> Self {
2351        PamSettings::new(value, None, false)
2352    }
2353}
2354
2355#[derive(Serialize, Deserialize, Debug)]
2356pub struct TrafficEncryptionSeed {
2357    #[serde(flatten)]
2358    pub keeper_field: KeeperField,
2359    #[serde(default)]
2360    pub required: bool,
2361    #[serde(default)]
2362    pub value: Vec<String>,
2363}
2364
2365impl TrafficEncryptionSeed {
2366    pub fn new(value: String, label: Option<String>, required: bool) -> Self {
2367        TrafficEncryptionSeed {
2368            keeper_field: KeeperField::new(
2369                StandardFieldTypeEnum::TRAFFICENCRYPTIONSEED
2370                    .get_type()
2371                    .to_string(),
2372                label,
2373            ),
2374            required,
2375            value: vec![value],
2376        }
2377    }
2378}
2379
2380pub fn struct_to_map<T>(data: &T) -> Result<HashMap<String, Value>, KSMRError>
2381where
2382    T: Serialize,
2383{
2384    // Try to serialize the struct into a serde_json::Value
2385    let value =
2386        serde_json::to_value(data).map_err(|e| KSMRError::SerializationError(e.to_string()))?;
2387
2388    // Try to convert the Value into a HashMap
2389    match value.as_object() {
2390        Some(obj) => {
2391            // Clone the object into a HashMap and return
2392            Ok(obj.clone().into_iter().collect())
2393        }
2394        None => Err(KSMRError::DataConversionError(
2395            "Expected an object but found something else".to_string(),
2396        )),
2397    }
2398}
2399
2400#[derive(Serialize, Deserialize, Debug)]
2401pub struct RecordField {
2402    #[serde(rename(serialize = "type", deserialize = "field_type"))]
2403    pub field_type: String,
2404    pub value: Value,
2405    pub required: bool,
2406    pub privacy_screen: bool,
2407    pub label: Option<String>,
2408}
2409
2410impl RecordField {
2411    pub fn new(
2412        field_type: String,
2413        value: Value,
2414        label: Option<String>,
2415        required: bool,
2416        privacy_screen: bool,
2417    ) -> KeeperField {
2418        let arrayed_value = Value::Array(vec![value]);
2419
2420        match label {
2421            Some(val) => KeeperField {
2422                field_type,
2423                value: arrayed_value,
2424                required,
2425                privacy_screen,
2426                label: val,
2427            },
2428            None => KeeperField {
2429                field_type,
2430                value: arrayed_value,
2431                required,
2432                privacy_screen,
2433                label: "".to_string(),
2434            },
2435        }
2436    }
2437
2438    pub fn new_record_field(
2439        field_type: &str,
2440        value: Value,
2441        label: Option<String>,
2442    ) -> KeeperField {
2443        Self::new(field_type.to_string(), value, label, false, false)
2444    }
2445
2446    pub fn new_record_field_with_options(
2447        field_type: &str,
2448        value: Value,
2449        label: Option<String>,
2450        required: bool,
2451        privacy_screen: bool,
2452    ) -> KeeperField {
2453        Self::new(field_type.to_string(), value, label, required, privacy_screen)
2454    }
2455}