Add diff applying error & improve CI (#32)

* Use stable rust

* Add From impls

* Revert to nightly

* Improve dev env & CI setup

* Update lock

* Add thiserror

* Add diff error

* Fix tests

* Lint

* Rename NumberOrString

* Format

* Fix lint script
This commit is contained in:
Andras Schmelczer 2025-12-06 21:54:08 +00:00 committed by GitHub
parent e03b9147df
commit 88d48afce3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 195 additions and 1192 deletions

View file

@ -1,4 +1,4 @@
use std::fmt::Debug;
use std::{borrow::Cow, fmt::Debug};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -12,18 +12,18 @@ const INTEGRAL_LIMIT: f64 = (1u64 << 53) as f64;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
#[derive(Debug, Clone, PartialEq)]
pub enum NumberOrString {
pub enum NumberOrText {
Number(i64),
Text(String),
}
#[cfg(feature = "wasm")]
impl TryFrom<JsValue> for NumberOrString {
impl TryFrom<JsValue> for NumberOrText {
type Error = DeserialisationError;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
if let Ok(num) = value.clone().try_into() {
return Ok(NumberOrString::Number(num));
return Ok(NumberOrText::Number(num));
}
if let Some(num) = value.clone().as_f64() {
@ -34,11 +34,11 @@ impl TryFrom<JsValue> for NumberOrString {
}
#[allow(clippy::cast_possible_truncation)]
return Ok(NumberOrString::Number(num.round() as i64));
return Ok(NumberOrText::Number(num.round() as i64));
}
if let Ok(text) = value.try_into() {
return Ok(NumberOrString::Text(text));
return Ok(NumberOrText::Text(text));
}
Err(DeserialisationError::new(
@ -48,15 +48,31 @@ impl TryFrom<JsValue> for NumberOrString {
}
#[cfg(feature = "wasm")]
impl From<NumberOrString> for JsValue {
fn from(value: NumberOrString) -> Self {
impl From<NumberOrText> for JsValue {
fn from(value: NumberOrText) -> Self {
match value {
NumberOrString::Number(num) => JsValue::from(num),
NumberOrString::Text(text) => JsValue::from(text),
NumberOrText::Number(num) => JsValue::from(num),
NumberOrText::Text(text) => JsValue::from(text),
}
}
}
impl From<i64> for NumberOrText {
fn from(value: i64) -> Self { NumberOrText::Number(value) }
}
impl From<String> for NumberOrText {
fn from(value: String) -> Self { NumberOrText::Text(value) }
}
impl From<&str> for NumberOrText {
fn from(value: &str) -> Self { NumberOrText::Text(value.to_owned()) }
}
impl<'a> From<Cow<'a, str>> for NumberOrText {
fn from(value: Cow<'a, str>) -> Self { NumberOrText::Text(value.into_owned()) }
}
/// Error type for deserialisation failures
#[cfg(feature = "wasm")]
#[derive(Debug, Clone)]