diff --git a/Cargo.lock b/Cargo.lock index cc3f7a8..166c058 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,9 +64,9 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "biscuit-auth" -version = "7.0.0-alpha.1" +version = "7.0.0-alpha.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0596a4c8b69a9538b8b1d160b9f94df2d970f4c0fb2657cb2fdf20239ac7eac7" +checksum = "741b091bed4c4602c7e3d7575f5bd5a9dafb86dff0d2830b5090b5ed78618fda" dependencies = [ "base64 0.22.1", "biscuit-parser", diff --git a/Cargo.toml b/Cargo.toml index 5db434b..1a3bd1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ name = "biscuit_auth" crate-type = ["cdylib"] [dependencies] -biscuit-auth = { version = "7.0.0-alpha.1", features = ["pem"] } +biscuit-auth = { version = "7.0.0-alpha.2", features = ["pem"] } pyo3 = { version = "0.28.2", features = ["extension-module", "chrono"] } hex = "0.4" base64 = "0.13.0" diff --git a/biscuit_test.py b/biscuit_test.py index 67295b2..b77f539 100644 --- a/biscuit_test.py +++ b/biscuit_test.py @@ -50,7 +50,7 @@ def test_biscuit_builder(): 'bool': True, 'bytes': b'\xaa\xbb', 'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc), - 'set': {2, True, "Test", datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc) }, + 'set': {2, 3}, 'array': [2, True, "Test", ["inner"]], 'map': { 'key': [{12: "value"}]} }, @@ -79,7 +79,7 @@ def test_biscuit_builder(): bool(true); bytes(hex:aabb); datetime(2023-04-03T10:00:00Z); -set({2, "Test", 2023-04-29T01:00:00Z, true}); +set({2, 3}); array([2, true, "Test", ["inner"]]); map({"key": [{12: "value"}]}); fact(false); @@ -98,6 +98,10 @@ def test_biscuit_builder(): builder.build(private_key) assert True +def test_heterogeneous_set(): + with pytest.raises(ValueError, match="set elements must have the same type"): + Fact("set({set})", { 'set': {2, "Test"} }) + def test_block_builder(): pubkey = PublicKey("ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") builder = BlockBuilder( diff --git a/docs/datalog.rst b/docs/datalog.rst index 65b12e1..f3b5b21 100644 --- a/docs/datalog.rst +++ b/docs/datalog.rst @@ -116,8 +116,8 @@ time(2023-06-09T00:00:00Z) bytes(hex:aabbff) >>> Fact("bytes({bytes})", {'bytes': [0xaa, 0xbb, 255]}) bytes(hex:aabbff) ->>> Fact("set({set})", {'set': {0, True, "ab", b'\xaa'}}) -set({0, "ab", hex:aa, true}) +>>> Fact("set({set})", {'set': {0, 1, 2}}) +set({0, 1, 2}) Inspecting datalog values ------------------------- diff --git a/src/lib.rs b/src/lib.rs index a6ad30e..a71fad4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,11 +67,16 @@ pub enum PyAlgorithm { Secp256r1, } -impl From for PyAlgorithm { - fn from(value: builder::Algorithm) -> Self { +impl TryFrom for PyAlgorithm { + type Error = PyErr; + + fn try_from(value: builder::Algorithm) -> PyResult { match value { - builder::Algorithm::Ed25519 => Self::Ed25519, - builder::Algorithm::Secp256r1 => Self::Secp256r1, + builder::Algorithm::Ed25519 => Ok(Self::Ed25519), + builder::Algorithm::Secp256r1 => Ok(Self::Secp256r1), + _ => Err(PyValueError::new_err(format!( + "unsupported algorithm: {value:?}" + ))), } } } @@ -1401,11 +1406,20 @@ impl PyTerm { pub fn to_term(&self) -> PyResult { match self { PyTerm::Simple(s) => s.to_term(), - PyTerm::Set(vs) => vs - .iter() - .map(|s| s.to_term()) - .collect::>() - .map(builder::Term::Set), + PyTerm::Set(vs) => { + let mut kinds = vs.iter().map(std::mem::discriminant); + if let Some(first) = kinds.next() { + if kinds.any(|k| k != first) { + return Err(PyValueError::new_err( + "set elements must have the same type", + )); + } + } + vs.iter() + .map(|s| s.to_term()) + .collect::>() + .map(builder::Term::Set) + } PyTerm::Array(vs) => vs .iter() .map(|s| s.to_term())