Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(stats): can now use time on WASM targets

Elijah Potter300c5da885f8
diff --git a/Cargo.lock b/Cargo.lock
index a403b67b7..2305cbd3a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -44,6 +44,21 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
+[[package]]
+name = "android-tzdata"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
+
+[[package]]
+name = "android_system_properties"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "anes"
version = "0.1.6"
@@ -251,6 +266,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+[[package]]
+name = "chrono"
+version = "0.4.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
+dependencies = [
+ "android-tzdata",
+ "iana-time-zone",
+ "js-sys",
+ "num-traits",
+ "wasm-bindgen",
+ "windows-link",
+]
+
[[package]]
name = "ciborium"
version = "0.2.2"
@@ -334,6 +363,12 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
+
[[package]]
name = "criterion"
version = "0.5.1"
@@ -818,6 +853,7 @@ dependencies = [
name = "harper-stats"
version = "0.26.0"
dependencies = [
+ "chrono",
"harper-core",
"quickcheck",
"quickcheck_macros",
@@ -901,6 +937,30 @@ version = "1.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946"
+[[package]]
+name = "iana-time-zone"
+version = "0.1.62"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2fd658b06e56721792c5df4475705b6cda790e9298d19d2f8af083457bcd127"
+dependencies = [
+ "android_system_properties",
+ "core-foundation-sys",
+ "iana-time-zone-haiku",
+ "js-sys",
+ "log",
+ "wasm-bindgen",
+ "windows-core",
+]
+
+[[package]]
+name = "iana-time-zone-haiku"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
+dependencies = [
+ "cc",
+]
+
[[package]]
name = "icu_collections"
version = "1.5.0"
@@ -2538,6 +2598,21 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+[[package]]
+name = "windows-core"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows-link"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
+
[[package]]
name = "windows-sys"
version = "0.52.0"
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index 6cd50a6aa..cddb3a75a 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -543,10 +543,7 @@ impl LanguageServer for Backend {
return Ok(None);
};
- let Ok(record) = Record::now(RecordKind::Lint(kind)) else {
- error!("System time error");
- return Ok(None);
- };
+ let record = Record::now(RecordKind::Lint(kind));
let mut stats = self.stats.write().await;
stats.records.push(record);
diff --git a/harper-stats/Cargo.toml b/harper-stats/Cargo.toml
index 05e6a9463..5ee455766 100644
--- a/harper-stats/Cargo.toml
+++ b/harper-stats/Cargo.toml
@@ -8,6 +8,7 @@ serde = { version = "1.0.217", features = ["derive"] }
harper-core = { path = "../harper-core", version = "0.26.0", features = ["concurrent"] }
uuid = { version = "1.12.0", features = ["serde", "v4"] }
serde_json = "1.0.140"
+chrono = "0.4.40"
[dev-dependencies]
quickcheck = "1.0.3"
diff --git a/harper-stats/src/record.rs b/harper-stats/src/record.rs
index 17c215de3..eee140a04 100644
--- a/harper-stats/src/record.rs
+++ b/harper-stats/src/record.rs
@@ -1,5 +1,3 @@
-use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};
-
use harper_core::linting::{LintGroupConfig, LintKind};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@@ -8,18 +6,18 @@ use uuid::Uuid;
pub struct Record {
pub kind: RecordKind,
/// Recorded as seconds from the Unix Epoch
- pub when: u64,
+ pub when: i64,
pub uuid: Uuid,
}
impl Record {
/// Record a new instance at the current system time.
- pub fn now(kind: RecordKind) -> Result<Self, SystemTimeError> {
- Ok(Self {
+ pub fn now(kind: RecordKind) -> Self {
+ Self {
kind,
- when: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(),
+ when: chrono::Utc::now().timestamp(),
uuid: Uuid::new_v4(),
- })
+ }
}
}
@@ -71,7 +69,7 @@ mod tests {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Record {
kind: RecordKind::arbitrary(g),
- when: u64::arbitrary(g),
+ when: i64::arbitrary(g),
uuid: uuid::Builder::from_u128(u128::arbitrary(g)).into_uuid(),
}
}
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 11cb9e917..55afdc6b2 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -299,9 +299,9 @@ impl Linter {
) -> Result<String, String> {
let mut source = lint.source.clone();
- self.stats.records.push(
- Record::now(RecordKind::Lint(lint.inner.lint_kind)).map_err(|err| err.to_string())?,
- );
+ self.stats
+ .records
+ .push(Record::now(RecordKind::Lint(lint.inner.lint_kind)));
suggestion.inner.apply(lint.inner.span, &mut source);