Skip to content

Commit

Permalink
chore: Fix clippy warnings about non-local impl definition
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Marbach <[email protected]>
  • Loading branch information
Johennes committed Apr 9, 2024
1 parent 7d51bd6 commit b68fc06
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 208 deletions.
73 changes: 39 additions & 34 deletions src/megolm/group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,46 +140,51 @@ impl GroupSession {
pub fn from_pickle(pickle: GroupSessionPickle) -> Self {
pickle.into()
}
}

#[cfg(feature = "libolm-compat")]
pub fn from_libolm_pickle(
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use matrix_pickle::Decode;
use zeroize::Zeroize;

use crate::{
megolm::libolm::LibolmRatchetPickle,
utilities::{unpickle_libolm, LibolmEd25519Keypair},
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
ratchet: LibolmRatchetPickle,
ed25519_keypair: LibolmEd25519Keypair,
}
#[cfg(feature = "libolm-compat")]
mod libolm_compat {
use matrix_pickle::Decode;
use zeroize::Zeroize;

use super::GroupSession;
use crate::{
megolm::{libolm::LibolmRatchetPickle, SessionConfig},
utilities::{unpickle_libolm, LibolmEd25519Keypair},
Ed25519Keypair,
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
ratchet: LibolmRatchetPickle,
ed25519_keypair: LibolmEd25519Keypair,
}

impl TryFrom<Pickle> for GroupSession {
type Error = crate::LibolmPickleError;
impl TryFrom<Pickle> for GroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let ratchet = (&pickle.ratchet).into();
let signing_key =
Ed25519Keypair::from_expanded_key(&pickle.ed25519_keypair.private_key)?;
fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let ratchet = (&pickle.ratchet).into();
let signing_key =
Ed25519Keypair::from_expanded_key(&pickle.ed25519_keypair.private_key)?;

Ok(Self { ratchet, signing_key, config: SessionConfig::version_1() })
}
Ok(Self { ratchet, signing_key, config: SessionConfig::version_1() })
}
}

const PICKLE_VERSION: u32 = 1;

unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
impl GroupSession {
pub fn from_libolm_pickle(
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
const PICKLE_VERSION: u32 = 1;
unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}
}

Expand Down
95 changes: 51 additions & 44 deletions src/megolm/inbound_group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use hmac::digest::MacError;
use serde::{Deserialize, Serialize};
use subtle::ConstantTimeEq;
use thiserror::Error;
use zeroize::Zeroize;

use super::{
default_config,
Expand Down Expand Up @@ -365,53 +364,61 @@ impl InboundGroupSession {
pub fn from_pickle(pickle: InboundGroupSessionPickle) -> Self {
Self::from(pickle)
}
}

#[cfg(feature = "libolm-compat")]
pub fn from_libolm_pickle(
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use matrix_pickle::Decode;

use super::libolm::LibolmRatchetPickle;
use crate::utilities::unpickle_libolm;

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
initial_ratchet: LibolmRatchetPickle,
latest_ratchet: LibolmRatchetPickle,
signing_key: [u8; 32],
signing_key_verified: bool,
}
#[cfg(feature = "libolm-compat")]
mod libolm_compat {
use matrix_pickle::Decode;
use zeroize::Zeroize;

impl TryFrom<Pickle> for InboundGroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let initial_ratchet = (&pickle.initial_ratchet).into();
#[allow(clippy::needless_borrow)]
let latest_ratchet = (&pickle.latest_ratchet).into();
let signing_key = Ed25519PublicKey::from_slice(&pickle.signing_key)?;
let signing_key_verified = pickle.signing_key_verified;

Ok(Self {
initial_ratchet,
latest_ratchet,
signing_key,
signing_key_verified,
config: SessionConfig::version_1(),
})
}
}
use super::InboundGroupSession;
use crate::{
megolm::{libolm::LibolmRatchetPickle, SessionConfig},
utilities::unpickle_libolm,
Ed25519PublicKey,
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
initial_ratchet: LibolmRatchetPickle,
latest_ratchet: LibolmRatchetPickle,
signing_key: [u8; 32],
signing_key_verified: bool,
}

const PICKLE_VERSION: u32 = 2;
impl TryFrom<Pickle> for InboundGroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let initial_ratchet = (&pickle.initial_ratchet).into();
#[allow(clippy::needless_borrow)]
let latest_ratchet = (&pickle.latest_ratchet).into();
let signing_key = Ed25519PublicKey::from_slice(&pickle.signing_key)?;
let signing_key_verified = pickle.signing_key_verified;

Ok(Self {
initial_ratchet,
latest_ratchet,
signing_key,
signing_key_verified,
config: SessionConfig::version_1(),
})
}
}

unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
impl InboundGroupSession {
pub fn from_libolm_pickle(
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
const PICKLE_VERSION: u32 = 2;
unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}
}

Expand Down
Loading

0 comments on commit b68fc06

Please sign in to comment.