Skip to content

Commit 0d27837

Browse files
committed
Copilot
1 parent bde3d6a commit 0d27837

3 files changed

Lines changed: 56 additions & 9 deletions

File tree

ucan/src/delegation/policy/predicate.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ impl<'de> Deserialize<'de> for Predicate {
186186
de::Error::invalid_length(2, &"expected an Ipld value")
187187
})?,
188188
)),
189+
"!=" => Ok(Predicate::Not(Box::new(Predicate::Equal(
190+
seq.next_element()?
191+
.ok_or_else(|| de::Error::invalid_length(1, &"expected a selector"))?,
192+
seq.next_element()?.ok_or_else(|| {
193+
de::Error::invalid_length(2, &"expected an Ipld value")
194+
})?,
195+
)))),
189196
">" => Ok(Predicate::GreaterThan(
190197
seq.next_element()?
191198
.ok_or_else(|| de::Error::invalid_length(1, &"expected a selector"))?,
@@ -249,7 +256,8 @@ impl<'de> Deserialize<'de> for Predicate {
249256
_ => Err(de::Error::unknown_variant(
250257
&op,
251258
&[
252-
"==", ">", ">=", "<", "<=", "like", "not", "and", "or", "all", "any",
259+
"==", "!=", ">", ">=", "<", "<=", "like", "not", "and", "or", "all",
260+
"any",
253261
],
254262
)),
255263
}
@@ -1413,4 +1421,36 @@ mod tests {
14131421
Ok(())
14141422
}
14151423
}
1424+
1425+
mod roundtrip {
1426+
use super::*;
1427+
1428+
#[test_log::test]
1429+
fn test_not_equal_dagcbor_roundtrip() -> TestResult {
1430+
let pred = Predicate::Not(Box::new(Predicate::Equal(
1431+
Select::from_str(".foo")?,
1432+
Ipld::Integer(42),
1433+
)));
1434+
1435+
let cbor = serde_ipld_dagcbor::to_vec(&pred)?;
1436+
let back: Predicate = serde_ipld_dagcbor::from_slice(&cbor)?;
1437+
1438+
assert_eq!(back, pred);
1439+
Ok(())
1440+
}
1441+
1442+
#[test_log::test]
1443+
fn test_not_equal_ipld_roundtrip() -> TestResult {
1444+
let pred = Predicate::Not(Box::new(Predicate::Equal(
1445+
Select::from_str(".bar")?,
1446+
Ipld::String("hello".into()),
1447+
)));
1448+
1449+
let ipld: Ipld = pred.clone().into();
1450+
let back = Predicate::try_from(ipld)?;
1451+
1452+
assert_eq!(back, pred);
1453+
Ok(())
1454+
}
1455+
}
14161456
}

ucan/src/delegation/policy/selector/filter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ pub fn parse_try_dot_field(input: &str) -> IResult<&str, Filter> {
189189
}
190190

191191
fn parse_opt_signed_int(i: &str) -> IResult<&str, Option<i32>> {
192-
nom::combinator::opt(
193-
nom::combinator::recognize(preceded(nom::combinator::opt(tag("-")), digit1))
194-
.map(|s: &str| i32::from_str(s).unwrap_or(0)),
195-
)
192+
nom::combinator::opt(map_res(
193+
nom::combinator::recognize(preceded(nom::combinator::opt(tag("-")), digit1)),
194+
i32::from_str,
195+
))
196196
.parse(i)
197197
}
198198

ucan/src/time/timestamp.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Timestamp {
116116
}
117117
}
118118

119-
#[cfg(feature = "wasm")]
119+
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
120120
impl Timestamp {
121121
/// Lift a [`js_sys::Date`] into a Rust [`Timestamp`].
122122
///
@@ -130,11 +130,18 @@ impl Timestamp {
130130
}
131131

132132
/// Lower the [`Timestamp`] to a [`js_sys::Date`].
133-
#[must_use]
134-
pub fn to_date(&self) -> js_sys::Date {
133+
///
134+
/// # Errors
135+
///
136+
/// Returns [`OutOfRangeError`] if the timestamp exceeds the 2⁵³ second
137+
/// bound, which would cause precision loss in the JavaScript `Date`.
138+
pub fn to_date(&self) -> Result<js_sys::Date, OutOfRangeError> {
139+
if self.0 > 0x001F_FFFF_FFFF_FFFF {
140+
return Err(OutOfRangeError::TooLarge(self.0));
141+
}
135142
#[allow(clippy::cast_precision_loss)]
136143
let millis = self.0 as f64 * 1000.0;
137-
js_sys::Date::new(&wasm_bindgen::JsValue::from(millis))
144+
Ok(js_sys::Date::new(&wasm_bindgen::JsValue::from(millis)))
138145
}
139146
}
140147

0 commit comments

Comments
 (0)