Skip to content

Commit

Permalink
fix: do not use serde where it can be avoided
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Feb 8, 2023
1 parent d90f739 commit d1dde78
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/abe_policy/access_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
abe_policy::{policy::Policy, Attribute},
Error,
};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Debug,
Expand All @@ -12,7 +11,7 @@ use std::{
/// An `AccessPolicy` is a boolean expression over attributes.
///
/// Only `positive` literals are allowed (no negation).
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Debug, Clone)]
pub enum AccessPolicy {
Attr(Attribute),
And(Box<AccessPolicy>, Box<AccessPolicy>),
Expand Down
38 changes: 0 additions & 38 deletions src/abe_policy/error.rs

This file was deleted.

15 changes: 6 additions & 9 deletions src/abe_policy/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,16 @@ pub struct Policy {

impl Display for Policy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let json = serde_json::to_string(&self);
match json {
Ok(string) => write!(f, "{string}"),
Err(err) => write!(f, "{err}"),
}
write!(f, "{self:?}")
}
}

impl Policy {
/// Converts the given string into a Policy. Does not fail if the given
/// string uses the legacy format.
pub fn parse_and_convert(bytes: &[u8]) -> Result<Self, Error> {
match serde_json::from_slice(bytes) {
// First try to deserialize the latest `Policy` format
match Policy::try_from(bytes) {
Ok(policy) => Ok(policy),
Err(e) => {
if let Ok(policy) = serde_json::from_slice::<LegacyPolicy>(bytes) {
Expand All @@ -180,9 +177,7 @@ impl Policy {
.collect(),
})
} else {
// Return the `Policy` deserialization error message instead of the
// `LegacyPolicy` one since this is the one that should be used.
Err(Error::DeserializationError(e))
Err(e)
}
}
}
Expand Down Expand Up @@ -577,7 +572,9 @@ mod tests {
//
// create policy
let mut policy = policy()?;
println!("{policy:?}");
policy.rotate(&Attribute::new("Department", "FIN"))?;
println!("{policy:?}");

//
// create access policy
Expand Down
2 changes: 1 addition & 1 deletion tests/non_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl NonRegressionTestVector {
let reg_vectors = Self {
public_key: base64::encode(mpk.try_to_bytes()?),
master_secret_key: base64::encode(msk.try_to_bytes()?),
policy: base64::encode(serde_json::to_vec(&policy)?),
policy: base64::encode(&policy.try_into()?),
//
// Create user decryption keys
top_secret_mkg_fin_key: UserSecretKeyTestVector::new(
Expand Down

0 comments on commit d1dde78

Please sign in to comment.