Skip to content

Commit

Permalink
Update deps (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
omid authored Oct 18, 2024
1 parent 6c27b30 commit 801d22c
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 66 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
lints:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Format
run: |
cargo fmt -- --check
Expand All @@ -34,16 +34,15 @@ jobs:
- { name: macOS, os: macos-latest, triple: x86_64-apple-darwin }
- { name: Windows, os: windows-2022, triple: x86_64-pc-windows-msvc }
version:
- 1.59.0 # MSRV
- 1.73.0 # MSRV
- stable
- nightly
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install ${{ matrix.version }}
uses: actions-rs/toolchain@v1
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: ${{ matrix.version }}-${{ matrix.target.triple }}
profile: minimal
override: true
- name: Run tests
run: |
Expand All @@ -53,7 +52,7 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Audit
run: |
cargo update
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tag_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
name: publish ${{ matrix.target }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
override: true
- uses: katyo/publish-crates@v1
- uses: katyo/publish-crates@v2
with:
path: './formula'
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
4 changes: 2 additions & 2 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
generate_readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install Cargo readme
run: cargo install cargo-readme
- name: Run Cargo readme
run: cd formula && cargo readme --no-title --no-license > ../README.md
- name: Commit the changes
uses: stefanzweifel/git-auto-commit-action@v4
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update README.md
file_pattern: README.md
11 changes: 1 addition & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
[workspace]
members = ["formula", "formula-wasm"]

[profile.dev]
debug = 0

[profile.release]
lto = true
opt-level = 'z'
codegen-units = 1
panic = 'abort'
strip = true
resolver = "2"

[patch.crates-io]
formula = { path = "formula" }
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ check:
@cargo update --dry-run
@cargo outdated -wR
@cargo +nightly udeps --all-targets
@cargo readme --no-title --no-license > README.md
@cd formula && cargo readme --no-title --no-license > ../README.md

check_nightly: check
check_nightly:
@cargo +nightly clippy --fix --allow-dirty --allow-staged

check_strictly:
@cargo +nightly clippy --fix --allow-dirty --allow-staged --all-features --all-targets -- -W clippy::all -W clippy::pedantic -W clippy::cargo -A clippy::missing_errors_doc -A clippy::extra_unused_lifetimes -A clippy::cast_sign_loss -A clippy::cast_possible_truncation -A clippy::missing-panics-doc -A clippy::module_name_repetitions -A clippy::cast_precision_loss -A clippy::cast_possible_wrap -A clippy::used_underscore_binding -A clippy::multiple_crate_versions -A clippy::option_option -A clippy::let_underscore_drop
@cargo +nightly clippy --fix --allow-dirty --allow-staged --all-features --all-targets -- -W clippy::all -W clippy::pedantic -W clippy::cargo -A clippy::missing_errors_doc -A clippy::extra_unused_lifetimes -A clippy::cast_sign_loss -A clippy::cast_possible_truncation -A clippy::missing-panics-doc -A clippy::module_name_repetitions -A clippy::cast_precision_loss -A clippy::cast_possible_wrap -A clippy::used_underscore_binding -A clippy::multiple_crate_versions -A clippy::option_option -A let_underscore_drop

check_very_strictly:
@cargo +nightly clippy --fix --allow-dirty --allow-staged --all-features --all-targets -- -W clippy::all -W clippy::pedantic -W clippy::cargo -A clippy::cast_sign_loss -A clippy::cast_possible_truncation -A clippy::cast_precision_loss
Expand All @@ -32,4 +32,4 @@ wasm_pack_and_publish:
cargo build --release; \
wasm-pack build --release; \
wasm-pack pack; \
wasm-pack publish
wasm-pack publish
6 changes: 3 additions & 3 deletions formula-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ readme = "README.md"
documentation = "https://docs.rs/formula"
repository = "https://github.com/omid/formula"
edition = "2021"
rust-version = "1.59"
rust-version = "1.73"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2.83"
formula = { version = "0.1.0" }
wasm-bindgen = "0.2"
formula = { version = "0.1" }
8 changes: 4 additions & 4 deletions formula-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn array_to_string(arr: Expr) -> String {
}
format!("[{}]", strings.join(","))
}
Expr::String(v) => format!("\"{}\"", v),
Expr::Date(v) => format!("\"{}\"", v),
Expr::Datetime(v) => format!("\"{}\"", v),
Expr::Time(v) => format!("\"{}\"", v),
Expr::String(v) => format!("\"{v}\""),
Expr::Date(v) => format!("\"{v}\""),
Expr::Datetime(v) => format!("\"{v}\""),
Expr::Time(v) => format!("\"{v}\""),
Expr::Number(v) => v.to_string(),
Expr::Bool(v) => v.to_string(),
Expr::Null => "null".to_string(),
Expand Down
22 changes: 11 additions & 11 deletions formula/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ readme = "README.md"
documentation = "https://docs.rs/formula"
repository = "https://github.com/omid/formula"
edition = "2021"
rust-version = "1.59"
rust-version = "1.73"

[dependencies]
chrono = "0.4.22"
pest = "2.4.0"
pest_derive = "2.4.0"
rand = "0.8.5"
thiserror = "1.0.34"
urlencoding = "2.1.2"
#sxd-xpath = "0.4.2"
#sxd-document = "0.3.2"
chrono = "0.4"
pest = "2.7"
pest_derive = "2.7"
rand = "0.8"
thiserror = "1.0"
urlencoding = "2.1"
#sxd-xpath = "0.4"
#sxd-document = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = { version = "0.11.11", features = ["blocking"] }
reqwest = { version = "0.12", features = ["blocking"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.7", features = ["js"] }
getrandom = { version = "0.2", features = ["js"] }
54 changes: 34 additions & 20 deletions formula/src/parsers/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,21 @@ impl Formula<'_> {
let date = Self::datestring_to_naivedate(&date, &rule_name)?;
let date = Self::shift_months(date, num);
let last_day = Self::last_day_of_month(date.year(), date.month());
NaiveDate::from_ymd(date.year(), date.month(), last_day)
NaiveDate::from_ymd_opt(date.year(), date.month(), last_day)
}
(Expr::Date(date), Expr::Number(num)) => {
let date = Self::shift_months(date, num);
let last_day = Self::last_day_of_month(date.year(), date.month());
NaiveDate::from_ymd(date.year(), date.month(), last_day)
NaiveDate::from_ymd_opt(date.year(), date.month(), last_day)
}
_ => return Err(Error::Parser(rule_name)),
};

let date = match date {
None => return Err(Error::Parser(rule_name)),
Some(v) => v,
};

Ok(Expr::Date(date))
}

Expand Down Expand Up @@ -180,7 +185,7 @@ impl Formula<'_> {
_ => return Err(Error::Parser(rule_name)),
};

Ok(Expr::Number(days as f64))
Ok(Expr::Number(days))
}

pub(crate) fn parse_day(pair: Pair<Rule>) -> Result<Expr> {
Expand Down Expand Up @@ -227,7 +232,11 @@ impl Formula<'_> {
let second = Self::get_formula(&mut args, &rule_name)?;
let time = match (hour, minute, second) {
(Expr::Number(hour), Expr::Number(minute), Expr::Number(second)) => {
Expr::Time(NaiveTime::from_hms(hour as u32, minute as u32, second as u32))
let time = match NaiveTime::from_hms_opt(hour as u32, minute as u32, second as u32) {
None => return Err(Error::Parser(rule_name)),
Some(v) => v,
};
Expr::Time(time)
}
_ => return Err(Error::Parser(rule_name)),
};
Expand All @@ -242,7 +251,11 @@ impl Formula<'_> {
let day = Self::get_formula(&mut args, &rule_name)?;
let date = match (year, month, day) {
(Expr::Number(year), Expr::Number(month), Expr::Number(day)) => {
Expr::Date(NaiveDate::from_ymd(year as i32, month as u32, day as u32))
let date = match NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32) {
None => return Err(Error::Parser(rule_name)),
Some(v) => v,
};
Expr::Date(date)
}
_ => return Err(Error::Parser(rule_name)),
};
Expand All @@ -256,7 +269,7 @@ impl Formula<'_> {
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|_| Error::Parser(rule_name.to_owned()))?;
#[allow(clippy::cast_possible_wrap)]
Ok(NaiveDate::from_ymd(mdy[2] as i32, mdy[0], mdy[1]))
NaiveDate::from_ymd_opt(mdy[2] as i32, mdy[0], mdy[1]).ok_or_else(|| Error::Parser(rule_name.to_owned()))
}

fn timestring_to_naivetime(timestring: &str, rule_name: &str) -> Result<NaiveTime> {
Expand All @@ -267,14 +280,15 @@ impl Formula<'_> {
.map(str::parse)
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|_| Error::Parser(rule_name.to_owned()))?;
Ok(NaiveTime::from_hms(hms[0], hms[1], hms[2]))
NaiveTime::from_hms_opt(hms[0], hms[1], hms[2]).ok_or_else(|| Error::Parser(rule_name.to_owned()))
}

fn last_day_of_month(year: i32, month: u32) -> u32 {
NaiveDate::from_ymd_opt(year, month + 1, 1)
.unwrap_or_else(|| NaiveDate::from_ymd(year + 1, 1, 1))
.pred()
.day()
.ok_or_else(|| NaiveDate::from_ymd_opt(year + 1, 1, 1))
.map(|v| v.pred_opt().map(|v| v.day()))
.expect("A valid date")
.expect("A valid date")
}

fn shift_months(date: NaiveDate, months: f64) -> NaiveDate {
Expand All @@ -287,7 +301,7 @@ impl Formula<'_> {
let last_day = Self::last_day_of_month(year, month);
let day = if day > last_day { last_day } else { day };

NaiveDate::from_ymd(year, month, day)
NaiveDate::from_ymd_opt(year, month, day).expect("A valid date")
}
}

Expand All @@ -301,11 +315,11 @@ mod tests {
fn test_parse_date_and_time_types() {
let formula = Formula::new("=DATE(2020,2,3)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2020, 2, 3)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2020, 2, 3).unwrap()));

let formula = Formula::new("=DATE(YEAR('1/30/2020'),MONTH('1/30/2020'),DAY('1/30/2020'))".trim()).unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2020, 1, 30)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2020, 1, 30).unwrap()));

let formula = Formula::new("=YEAR('1/30/2020')").unwrap();
let value = formula.parse().unwrap();
Expand All @@ -321,7 +335,7 @@ mod tests {

let formula = Formula::new("=DATEVALUE('1/30/2020')").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2020, 1, 30)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2020, 1, 30).unwrap()));

let formula = Formula::new("=DAYS('3/30/2020', '1/30/2020')").unwrap();
let value = formula.parse().unwrap();
Expand All @@ -337,19 +351,19 @@ mod tests {

let formula = Formula::new("=EDATE('1/30/2020', 5)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2020, 6, 30)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()));

let formula = Formula::new("=EDATE(DATE(2020,8,25),27)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2022, 11, 25)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2022, 11, 25).unwrap()));

let formula = Formula::new("=EOMONTH('1/20/2020', 5)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2020, 6, 30)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()));

let formula = Formula::new("=EOMONTH(DATE(2020,8,31),27)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Date(NaiveDate::from_ymd(2022, 11, 30)));
assert_eq!(value, Expr::Date(NaiveDate::from_ymd_opt(2022, 11, 30).unwrap()));

let formula = Formula::new("=HOUR('02:30:00')").unwrap();
let value = formula.parse().unwrap();
Expand All @@ -369,11 +383,11 @@ mod tests {

let formula = Formula::new("=TIME(2,30,0)").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Time(NaiveTime::from_hms(2, 30, 0)));
assert_eq!(value, Expr::Time(NaiveTime::from_hms_opt(2, 30, 0).unwrap()));

let formula = Formula::new("=TIMEVALUE('02:30:00')").unwrap();
let value = formula.parse().unwrap();
assert_eq!(value, Expr::Time(NaiveTime::from_hms(2, 30, 0)));
assert_eq!(value, Expr::Time(NaiveTime::from_hms_opt(2, 30, 0).unwrap()));

let formula = Formula::new("=WEEKDAY(DATE(2020,1,1))").unwrap();
let value = formula.parse().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions formula/src/parsers/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Formula<'_> {
};

let decimals = decimals as usize;
let mut text = format!("{:.*}", decimals, number);
let mut text = format!("{number:.decimals$}");

if !no_commas {
// separate text with commas
Expand Down Expand Up @@ -342,7 +342,7 @@ impl Formula<'_> {

match num_and_decimal.get(1) {
Some(decimals) => {
text = format!("{}.{}", t, decimals);
text = format!("{t}.{decimals}");
}
None => {
text = t;
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Formula<'_> {
}
let start_text = text.chars().take((start as usize) - 1).collect::<String>();
let end_text = text.chars().skip((start + len) as usize - 1).collect::<String>();
format!("{}{}{}", start_text, new_text, end_text)
format!("{start_text}{new_text}{end_text}")
}
_ => return Err(Error::Parser(rule_name)),
};
Expand Down

0 comments on commit 801d22c

Please sign in to comment.