Adds a new entry function to parse
Adds more badges and a benchmark
This commit is contained in:
parent
9647655f69
commit
530f8e6736
|
|
@ -17,6 +17,7 @@ uuid = { version = "1.11.0", features = ["v4", "v7", "serde", "fast-rng"] }
|
|||
simplelog = { version = "0.12.2", features = ["paris"] }
|
||||
log = { version = "0.4.22", features = ["std"] }
|
||||
nom = "7.1.3"
|
||||
criterion = "0.5"
|
||||
|
||||
[profile.dev]
|
||||
debug = 0
|
||||
|
|
@ -38,3 +39,11 @@ path = "src/main.rs"
|
|||
[lib]
|
||||
name = "idlefox"
|
||||
path = "src/lib.rs"
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
|
||||
[[bench]]
|
||||
name = "parser_benchmark"
|
||||
harness = false
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# idlefox
|
||||

|
||||
  
|
||||
|
||||
IdleFox is a feature-rich Discord bot designed to streamline the process of finding meeting dates, organizing events, and making decisions among multiple options. With its user-friendly commands and robust command system, IdleFox helps facilitate effective communication within server communities.
|
||||
|
||||
|
|
|
|||
3
TODO.md
3
TODO.md
|
|
@ -26,9 +26,10 @@
|
|||
|
||||
# Tasks
|
||||
## Dan
|
||||
* [ ] Finish testing to reach 100% coverage
|
||||
* [x] Finish testing to reach 100% coverage
|
||||
* [ ] Implement Resilience & Recovery
|
||||
* [x] Parse date mini language
|
||||
* [ ] Migrate the todo into gitea
|
||||
|
||||
## Vky
|
||||
* [x] Write documentation for all functions in IO.rs
|
||||
|
|
|
|||
23
badge.svg
23
badge.svg
|
|
@ -1,23 +0,0 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="93" height="20">
|
||||
<linearGradient id="smooth" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
</linearGradient>
|
||||
|
||||
<mask id="round">
|
||||
<rect width="93" height="20" rx="3" fill="#fff"/>
|
||||
</mask>
|
||||
|
||||
<g mask="url(#round)">
|
||||
<rect width="64" height="20" fill="#555"/>
|
||||
<rect x="64" width="29" height="20" fill="#3c1"/>
|
||||
<rect width="93" height="20" fill="url(#smooth)"/>
|
||||
</g>
|
||||
|
||||
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
|
||||
<text x="32" y="15" fill="#010101" fill-opacity=".3">coverage</text>
|
||||
<text x="32" y="14">coverage</text>
|
||||
<text x="78" y="15" fill="#010101" fill-opacity=".3">100</text>
|
||||
<text x="78" y="14">100</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 885 B |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="94" height="18" role="img" aria-label="release: v0.1.0"><title>release: v0.1.0</title><linearGradient id="bms-162829b957040378" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-162829b957040378"><rect width="94" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-162829b957040378)"><rect width="49" height="18" fill="#555"/><rect x="49" width="45" height="18" fill="#fe7d37"/><rect width="94" height="18" fill="url(#bms-162829b957040378)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="255" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="390">release</text><text x="255" y="130" transform="scale(.1)" fill="#fff" textLength="390">release</text><text aria-hidden="true" x="705" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="350">v0.1.0</text><text x="705" y="130" transform="scale(.1)" fill="#fff" textLength="350">v0.1.0</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="88" height="18" role="img" aria-label="build: passing"><title>build: passing</title><linearGradient id="bms-de44f1c0747536f7" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-de44f1c0747536f7"><rect width="88" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-de44f1c0747536f7)"><rect width="37" height="18" fill="#555"/><rect x="37" width="51" height="18" fill="#97ca00"/><rect width="88" height="18" fill="url(#bms-de44f1c0747536f7)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="195" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">build</text><text x="195" y="130" transform="scale(.1)" fill="#fff" textLength="270">build</text><text aria-hidden="true" x="615" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">passing</text><text x="615" y="130" transform="scale(.1)" fill="#fff" textLength="410">passing</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="130" height="18" role="img" aria-label="test-coverage: 100%"><title>test-coverage: 100%</title><linearGradient id="bms-0d308125ce4bf4b9" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-0d308125ce4bf4b9"><rect width="130" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-0d308125ce4bf4b9)"><rect width="87" height="18" fill="#555"/><rect x="87" width="43" height="18" fill="#97ca00"/><rect width="130" height="18" fill="url(#bms-0d308125ce4bf4b9)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1075" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="330">100%</text><text x="1075" y="130" transform="scale(.1)" fill="#fff" textLength="330">100%</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1,23 @@
|
|||
use chrono::NaiveDate;
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use idlefox::date::parse_date_list;
|
||||
use std::hint::black_box;
|
||||
|
||||
fn complex_date_parse(input: &str) -> usize {
|
||||
let context = NaiveDate::from_ymd_opt(2023, 10, 1);
|
||||
let list = parse_date_list(input, context).unwrap();
|
||||
list.len()
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("complex_date_parse", |b| {
|
||||
b.iter(|| {
|
||||
complex_date_parse(black_box(
|
||||
"Oct 5, Nov 15 - 18, sat, mon , tue-fri and some other text",
|
||||
))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
criterion_main!(benches);
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
//! Utilities to handle date and relative date selection
|
||||
|
||||
use crate::error_handling::IdleError;
|
||||
use chrono::{Datelike, Duration, Local, Month, Months, NaiveDate, Weekday};
|
||||
|
||||
use super::date_parse::{DateObject, DatePoint};
|
||||
use super::{
|
||||
date_parse::{DateObject, DatePoint},
|
||||
parse_str_into_date,
|
||||
};
|
||||
|
||||
/// Returns the date corresponding to the implicit Weekday
|
||||
///
|
||||
|
|
@ -146,11 +150,19 @@ pub enum NaiveDateObject {
|
|||
|
||||
/// A collection of [NaiveDateObject]s, allowing for sequential and random access to their start dates.
|
||||
///
|
||||
/// This struct can be used to iterate over a list of dates in order or access any date by its index.
|
||||
/// This struct provides an abstraction over a list of date objects, enabling both sequential iteration
|
||||
/// over the dates in order or direct access to any date by its index. The structure is designed to handle
|
||||
/// different sources of date information, allowing for flexible data manipulation based on the provided dates.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq, Debug))]
|
||||
pub struct NaiveDateList {
|
||||
/// The inner vector
|
||||
inner: Vec<NaiveDateObject>,
|
||||
/// The leftover, unparsed string
|
||||
unparsed: Option<String>,
|
||||
}
|
||||
|
||||
impl DateObject {
|
||||
|
|
@ -193,8 +205,11 @@ impl DateObject {
|
|||
|
||||
impl NaiveDateList {
|
||||
/// Creates a new [NaiveDateList] from an existing vector of [NaiveDateObject]s.
|
||||
pub fn new(inner: Vec<NaiveDateObject>) -> Self {
|
||||
Self { inner }
|
||||
pub fn new(inner: Vec<NaiveDateObject>, unparsed: Option<&str>) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
unparsed: unparsed.map(|v| v.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of dates in this [NaiveDateList].
|
||||
|
|
@ -230,29 +245,79 @@ impl NaiveDateList {
|
|||
/// month: Some(chrono::Month::January),
|
||||
/// };
|
||||
/// let next_valid_date_object = DateObject::Single(date_point);
|
||||
/// let list = NaiveDateList::from_context(vec![next_valid_date_object], Some(today));
|
||||
/// let list = NaiveDateList::from_context(vec![next_valid_date_object], Some(today), None);
|
||||
/// assert!(list.is_ok());
|
||||
/// ```
|
||||
pub fn from_context(
|
||||
inner: Vec<DateObject>,
|
||||
context: Option<NaiveDate>,
|
||||
unparsed: Option<&str>,
|
||||
) -> Result<Self, IdleError> {
|
||||
let mut new = Vec::new();
|
||||
for elem in inner {
|
||||
new.push(elem.to_naive_date_object(context)?);
|
||||
}
|
||||
Ok(Self { inner: new })
|
||||
Ok(Self {
|
||||
inner: new,
|
||||
unparsed: unparsed.map(|v| v.to_owned()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// // TODO: If needed, make an iterator over all dates that this list of date encompasses
|
||||
// impl IntoIterator for NaiveDateList {
|
||||
// type Item = NaiveDate;
|
||||
// type Item = NaiveDateObject;
|
||||
// type IntoIter = std::vec::IntoIter<Self::Item>;
|
||||
|
||||
// fn into_iter(self) -> Self::IntoIter {
|
||||
// self.inner.into_iter()
|
||||
// }
|
||||
// }
|
||||
|
||||
/// This function parses a given input string representing date information and returns a `NaiveDateList`,
|
||||
/// which is a collection of sequential or random accessible date objects. If there are any unparsed parts
|
||||
/// left after processing the input, they can be retrieved using the `unparsed` method on the returned `Result`.
|
||||
///
|
||||
/// The function uses the context provided to resolve any relative dates within the input string.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `input` - A string slice containing the date information to be parsed. This string may include various date formats,
|
||||
/// single or range of dates, and possibly unparsed text.
|
||||
/// * `context` - An optional NaiveDate that provides a context for resolving relative dates within the input. If no context
|
||||
/// is provided, the function will attempt to use today's date as a fallback.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result containing a `NaiveDateList` on success or an `IdleError` if parsing fails. The `Result` also includes any
|
||||
/// unparsed text that was left over after processing the input string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use idlefox::date::parse_date_list;
|
||||
/// # use chrono::NaiveDate;
|
||||
/// let input = "Oct 5, Nov 15 - 18, sat, mon , tue-fry and some other text";
|
||||
/// let context = NaiveDate::from_ymd_opt(2023, 10, 1);
|
||||
/// let result = parse_date_list(input, context);
|
||||
/// assert!(result.is_ok());
|
||||
/// match result {
|
||||
/// Ok(date_list) => println!("Parsed dates: {}", date_list.len()),
|
||||
/// Err(e) => eprintln!("Error parsing dates: {}", e),
|
||||
/// }
|
||||
/// ```
|
||||
pub fn parse_date_list(
|
||||
input: &str,
|
||||
context: Option<NaiveDate>,
|
||||
) -> Result<NaiveDateList, IdleError> {
|
||||
let (leftover, objects) = match parse_str_into_date(input)? {
|
||||
("", objects) => (None, objects),
|
||||
(leftover, objects) => (Some(leftover), objects),
|
||||
};
|
||||
|
||||
NaiveDateList::from_context(objects, context, leftover)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
|
||||
|
|
@ -370,7 +435,7 @@ pub(crate) mod tests {
|
|||
}
|
||||
);
|
||||
|
||||
let vec_obj = NaiveDateList::new(vec![date_obj.clone()]);
|
||||
let vec_obj = NaiveDateList::new(vec![date_obj.clone()], None);
|
||||
assert_eq!(vec_obj.len(), 1);
|
||||
assert!(!vec_obj.is_empty());
|
||||
assert_eq!(vec_obj.get(0).unwrap(), &date_obj);
|
||||
|
|
@ -378,6 +443,7 @@ pub(crate) mod tests {
|
|||
assert!(!NaiveDateList::from_context(
|
||||
vec![DateObject::Single(date_a), DateObject::Single(date_a)],
|
||||
context,
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
.is_empty());
|
||||
|
|
@ -390,7 +456,23 @@ pub(crate) mod tests {
|
|||
DateObject::Single(date_a)
|
||||
],
|
||||
context,
|
||||
None,
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_date_list() {
|
||||
use chrono::NaiveDate;
|
||||
let input = "Oct 5, Nov 15 - 18, sat, mon , tue-fri and some other text";
|
||||
let context = NaiveDate::from_ymd_opt(2023, 10, 1);
|
||||
let list = parse_date_list(input, context).unwrap();
|
||||
assert_eq!(list.unparsed, Some("and some other text".to_string()));
|
||||
assert_eq!(list.len(), 5);
|
||||
|
||||
let input = "Oct 5, Nov 15";
|
||||
let list = parse_date_list(input, None).unwrap();
|
||||
assert_eq!(list.unparsed, None);
|
||||
assert_eq!(list.len(), 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,30 @@
|
|||
//! - `date_tools`: This submodule offers utility functions for working with dates.
|
||||
//! It's main goal is do give a context for a subjective and incomplete date to transform it into a [chrono::NaiveDate] type from the chrono crate.
|
||||
//!
|
||||
//! # Functions
|
||||
//!
|
||||
//! The following functions are re-exported from the `date_tools` submodule:
|
||||
//!
|
||||
//! - [parse_date_list]: This function parses a given input string representing date information and returns a `NaiveDateList`,
|
||||
//! which is a collection of sequential or random accessible date objects. If there are any unparsed parts
|
||||
//! left after processing the input, they can be retrieved using the `unparsed` method on the returned `Result`.
|
||||
//!
|
||||
//! The function uses the context provided to resolve any relative dates within the input string.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! # use idlefox::date::parse_date_list;
|
||||
//! # use chrono::NaiveDate;
|
||||
//! let input = "Oct 5, Nov 15 - 18, sat, mon , tue-fry and some other text";
|
||||
//! let context = NaiveDate::from_ymd_opt(2023, 10, 1);
|
||||
//! let result = parse_date_list(input, context);
|
||||
//! assert!(result.is_ok());
|
||||
//! match result {
|
||||
//! Ok(date_list) => println!("Parsed dates: {}", date_list.len()),
|
||||
//! Err(e) => eprintln!("Error parsing dates: {}", e),
|
||||
//! }
|
||||
//! ```
|
||||
#![allow(dead_code)]
|
||||
|
||||
// All structures
|
||||
|
|
@ -29,6 +53,6 @@ pub mod date_tools;
|
|||
|
||||
// Republishing important functions
|
||||
pub use date_parse::parse_str_into_date;
|
||||
// TODO: Add these final user function
|
||||
pub use date_tools::parse_date_list;
|
||||
// pub use date_tools::naivelistfromstr;
|
||||
// pub use date_tools::naivedayfromstr;
|
||||
|
|
|
|||
Loading…
Reference in New Issue