Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix clippy warnings about non-local impl definition #145

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions src/megolm/group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,46 @@ impl GroupSession {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use matrix_pickle::Decode;
use zeroize::Zeroize;
use crate::{megolm::group_session::libolm_compat::Pickle, utilities::unpickle_libolm};

use crate::{
megolm::libolm::LibolmRatchetPickle,
utilities::{unpickle_libolm, LibolmEd25519Keypair},
};
const PICKLE_VERSION: u32 = 1;
unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}

#[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::LibolmEd25519Keypair,
Ed25519Keypair,
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
pub(super) 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)
}
}

Expand Down
87 changes: 48 additions & 39 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 @@ -371,50 +370,60 @@ impl InboundGroupSession {
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,
}

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 crate::{
megolm::inbound_group_session::libolm_compat::Pickle, utilities::unpickle_libolm,
};

const PICKLE_VERSION: u32 = 2;

unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}

#[cfg(feature = "libolm-compat")]
mod libolm_compat {
use matrix_pickle::Decode;
use zeroize::Zeroize;

use super::InboundGroupSession;
use crate::{
megolm::{libolm::LibolmRatchetPickle, SessionConfig},
Ed25519PublicKey,
};

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

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(),
})
}
}
}

/// A format suitable for serialization which implements [`serde::Serialize`]
/// and [`serde::Deserialize`]. Obtainable by calling
/// [`InboundGroupSession::pickle`].
Expand Down
Loading
Loading