on master help sign in
Remove `FormatError`
commit0b7911d9217f63b0e110923fcddd4534e4d9da38 authorJason Rodney Hansen <jasonrodneyhansen@gmail.com> dateSat 4 Dec 2021, 11:30 (5 years ago) parent31ed91d
1 file changed+29−51
helix-core/src/increment/date_time.rs+29−51
helix-core/src/increment/date_time.rs+29−51
@@ -112,22 +112,22 @@ impl Increment for DateTimeIncrementor {
112112
113113static FORMATS: Lazy<Vec<Format>> = Lazy::new(|| {
114114 vec![
115− Format::new("%Y-%m-%d %H:%M:%S").unwrap(), // 2021-11-24 07:12:23
116− Format::new("%Y/%m/%d %H:%M:%S").unwrap(), // 2021/11/24 07:12:23
117− Format::new("%Y-%m-%d %H:%M").unwrap(), // 2021-11-24 07:12
118− Format::new("%Y/%m/%d %H:%M").unwrap(), // 2021/11/24 07:12
119− Format::new("%Y-%m-%d").unwrap(), // 2021-11-24
120− Format::new("%Y/%m/%d").unwrap(), // 2021/11/24
121− Format::new("%a %b %d %Y").unwrap(), // Wed Nov 24 2021
122− Format::new("%d-%b-%Y").unwrap(), // 24-Nov-2021
123− Format::new("%Y %b %d").unwrap(), // 2021 Nov 24
124− Format::new("%b %d, %Y").unwrap(), // Nov 24, 2021
125− Format::new("%-I:%M:%S %P").unwrap(), // 7:21:53 am
126− Format::new("%-I:%M %P").unwrap(), // 7:21 am
127− Format::new("%-I:%M:%S %p").unwrap(), // 7:21:53 AM
128− Format::new("%-I:%M %p").unwrap(), // 7:21 AM
129− Format::new("%H:%M:%S").unwrap(), // 23:24:23
130− Format::new("%H:%M").unwrap(), // 23:24
115+ Format::new("%Y-%m-%d %H:%M:%S"), // 2021-11-24 07:12:23
116+ Format::new("%Y/%m/%d %H:%M:%S"), // 2021/11/24 07:12:23
117+ Format::new("%Y-%m-%d %H:%M"), // 2021-11-24 07:12
118+ Format::new("%Y/%m/%d %H:%M"), // 2021/11/24 07:12
119+ Format::new("%Y-%m-%d"), // 2021-11-24
120+ Format::new("%Y/%m/%d"), // 2021/11/24
121+ Format::new("%a %b %d %Y"), // Wed Nov 24 2021
122+ Format::new("%d-%b-%Y"), // 24-Nov-2021
123+ Format::new("%Y %b %d"), // 2021 Nov 24
124+ Format::new("%b %d, %Y"), // Nov 24, 2021
125+ Format::new("%-I:%M:%S %P"), // 7:21:53 am
126+ Format::new("%-I:%M %P"), // 7:21 am
127+ Format::new("%-I:%M:%S %p"), // 7:21:53 AM
128+ Format::new("%-I:%M %p"), // 7:21 AM
129+ Format::new("%H:%M:%S"), // 23:24:23
130+ Format::new("%H:%M"), // 23:24
131131 ]
132132});
133133
@@ -140,7 +140,7 @@ struct Format {
140140}
141141
142142impl Format {
143− fn new(fmt: &'static str) -> Result<Self, FormatError> {
143+ fn new(fmt: &'static str) -> Self {
144144 let mut remaining = fmt;
145145 let mut fields = Vec::new();
146146 let mut regex = String::new();
@@ -149,56 +149,34 @@ impl Format {
149149 while let Some(i) = remaining.find('%') {
150150 let after = &remaining[i + 1..];
151151 let mut chars = after.chars();
152− let c = chars
153− .next()
154− .ok_or(FormatError::UnexpectedEndOfFormatString)?;
152+ let c = chars.next().unwrap();
155153
156154 let spec_len = if c == '-' {
157− if let Some(c) = chars.next() {
158− 1 + c.len_utf8()
159− } else {
160− return Err(FormatError::UnexpectedEndOfFormatString);
161− }
155+ 1 + chars.next().unwrap().len_utf8()
162156 } else {
163157 c.len_utf8()
164158 };
165159
166− if i < remaining.len() - spec_len {
167− let specifier = &after[..spec_len];
168− if let Some(field) = DateField::from_specifier(specifier) {
169− fields.push(field);
170− max_len += field.max_len + remaining[..i].len();
171− regex += &remaining[..i];
172− regex += &format!("({})", field.regex);
173− remaining = &after[spec_len..];
174− } else {
175− return Err(FormatError::UnsupportedSpecifier(
176− &remaining[i..i + 1 + spec_len],
177− ));
178− }
179− } else {
180− return Err(FormatError::UnexpectedEndOfFormatString);
181− }
160+ let specifier = &after[..spec_len];
161+ let field = DateField::from_specifier(specifier).unwrap();
162+ fields.push(field);
163+ max_len += field.max_len + remaining[..i].len();
164+ regex += &remaining[..i];
165+ regex += &format!("({})", field.regex);
166+ remaining = &after[spec_len..];
182167 }
183168
184− let regex = Regex::new(&regex).map_err(FormatError::Regex)?;
169+ let regex = Regex::new(&regex).unwrap();
185170
186− Ok(Self {
171+ Self {
187172 fmt,
188173 fields,
189174 regex,
190175 max_len,
191− })
176+ }
192177 }
193178}
194179
195−#[derive(Clone, Debug)]
196−enum FormatError {
197− UnexpectedEndOfFormatString,
198− UnsupportedSpecifier(&'static str),
199− Regex(regex::Error),
200−}
201−
202180impl PartialEq for Format {
203181 fn eq(&self, other: &Self) -> bool {
204182 self.fmt == other.fmt && self.fields == other.fields && self.max_len == other.max_len