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 1 commit
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
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(
Johennes marked this conversation as resolved.
Show resolved Hide resolved
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
Loading