text_file_sort/
field.rs

1use crate::field_type::FieldType;
2
3/// Defines a field in a line record.
4///
5/// The Field definition is eventually used in comparison.
6///
7/// # Examples
8/// ```
9/// // specify that the second field of the record is a String and that its blanks to be stripped
10/// // and case ignored for comparison
11/// use text_file_sort::field::Field;
12/// use text_file_sort::field_type::FieldType;
13/// let field = Field::new(2, FieldType::String)
14///     .with_ignore_blanks(true)
15///     .with_ignore_case(true);
16/// ```
17#[derive(Clone, Debug)]
18pub struct Field {
19    name: String,
20    index: usize,
21    field_type: FieldType,
22    ignore_blanks: bool,
23    ignore_case: bool,
24    random: bool,
25}
26
27impl Field {
28    /// Create a new [Field]
29    ///
30    /// # Arguments
31    /// * `index` - the index of the field, starting at 1. Index of 0 treats the complete line as a
32    /// field
33    /// * `field_type` - the type of the field. See [FieldType] for supported types
34    ///
35    /// # Examples
36    /// ```
37    /// use text_file_sort::field::Field;
38    /// use text_file_sort::field_type::FieldType;
39    /// let field = Field::new(1, FieldType::Integer);
40    /// ```
41    pub fn new(
42        index: usize,
43        field_type: FieldType,
44    ) -> Field {
45        Field {
46            name: String::new(),
47            index,
48            field_type,
49            ignore_blanks: false,
50            ignore_case: false,
51            random: false,
52        }
53    }
54
55    /// Get the name for this field.
56    pub fn name(&self) -> &String {
57        &self.name
58    }
59
60    /// Get the index for this field.
61    pub fn index(&self) -> usize {
62        self.index
63    }
64
65    /// Get the [FieldType] for this field.
66    pub fn field_type(&self) -> &FieldType {
67        &self.field_type
68    }
69
70    /// Get the ignore blanks setting for this field
71    pub fn ignore_blanks(&self) -> bool {
72        self.ignore_blanks
73    }
74
75    /// Get the ignore case setting for this field.
76    pub fn ignore_case(&self) -> bool {
77        self.ignore_case
78    }
79
80    /// Get the random setting for this field.
81    pub fn random(&self) -> bool {
82        self.random
83    }
84
85    /// Specify a name for this field
86    pub fn with_name(mut self, name: String) -> Field {
87        self.name = name;
88        self
89    }
90
91    /// Specify a name for this field as &str
92    pub fn with_str_name(mut self, name: &str) -> Field {
93        self.name = name.to_string();
94        self
95    }
96
97    /// Specify the index for this field starting at 1. Specifying index of 0 treats the complete
98    /// line as a field.
99    pub fn with_index(mut self, index: usize) -> Field {
100        self.index = index;
101        self
102    }
103
104    /// Specify the field type for this field. See [FieldType] for supported types.
105    pub fn with_field_type(mut self, field_type: FieldType) -> Field {
106        self.field_type = field_type;
107        self
108    }
109
110    /// Specify whether to ignore blanks for comparison. When true the field will be trimmed before
111    /// comparison.
112    pub fn with_ignore_blanks(mut self, ignore_blanks: bool) -> Field {
113        self.ignore_blanks = ignore_blanks;
114        self
115    }
116
117    /// Specify whether to ignore case for comparison.
118    pub fn with_ignore_case(mut self, ignore_case: bool) -> Field {
119        self.ignore_case = ignore_case;
120        self
121    }
122
123    /// Specify whether to generate a random field value. Specifying true will cause the file to
124    /// be randomly shuffled.
125    pub fn with_random(mut self, random: bool) -> Field {
126        self.random = random;
127        self
128    }
129}