From 0e11996545c491e91009256a06f2ddac36315832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 26 Mar 2022 14:56:22 +0100 Subject: [PATCH] events: reorder types in file module --- crates/ruma-common/src/events/file.rs | 236 +++++++++++++------------- 1 file changed, 118 insertions(+), 118 deletions(-) diff --git a/crates/ruma-common/src/events/file.rs b/crates/ruma-common/src/events/file.rs index 123701dc..f2b5cb79 100644 --- a/crates/ruma-common/src/events/file.rs +++ b/crates/ruma-common/src/events/file.rs @@ -14,124 +14,6 @@ use super::{ }; use crate::{serde::Base64, MxcUri}; -/// The encryption info of a file sent to a room with end-to-end encryption enabled. -/// -/// To create an instance of this type, first create a `EncryptedContentInit` and convert it via -/// `EncryptedContent::from` / `.into()`. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct EncryptedContent { - /// A [JSON Web Key](https://tools.ietf.org/html/rfc7517#appendix-A.3) object. - pub key: JsonWebKey, - - /// The 128-bit unique counter block used by AES-CTR, encoded as unpadded base64. - pub iv: Base64, - - /// A map from an algorithm name to a hash of the ciphertext, encoded as unpadded base64. - /// - /// Clients should support the SHA-256 hash, which uses the key sha256. - pub hashes: BTreeMap, - - /// Version of the encrypted attachments protocol. - /// - /// Must be `v2`. - pub v: String, -} - -/// Initial set of fields of `EncryptedContent`. -/// -/// This struct will not be updated even if additional fields are added to `EncryptedContent` in a -/// new (non-breaking) release of the Matrix specification. -#[derive(Debug)] -#[allow(clippy::exhaustive_structs)] -pub struct EncryptedContentInit { - /// A [JSON Web Key](https://tools.ietf.org/html/rfc7517#appendix-A.3) object. - pub key: JsonWebKey, - - /// The 128-bit unique counter block used by AES-CTR, encoded as unpadded base64. - pub iv: Base64, - - /// A map from an algorithm name to a hash of the ciphertext, encoded as unpadded base64. - /// - /// Clients should support the SHA-256 hash, which uses the key sha256. - pub hashes: BTreeMap, - - /// Version of the encrypted attachments protocol. - /// - /// Must be `v2`. - pub v: String, -} - -impl From for EncryptedContent { - fn from(init: EncryptedContentInit) -> Self { - let EncryptedContentInit { key, iv, hashes, v } = init; - Self { key, iv, hashes, v } - } -} - -/// Information about a file content. -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct FileContentInfo { - /// The original filename of the uploaded file. - #[serde(skip_serializing_if = "Option::is_none")] - pub name: Option, - - /// The mimetype of the file, e.g. “application/msword”. - #[serde(skip_serializing_if = "Option::is_none")] - pub mimetype: Option, - - /// The size of the file in bytes. - #[serde(skip_serializing_if = "Option::is_none")] - pub size: Option, -} - -impl FileContentInfo { - /// Creates an empty `FileContentInfo`. - pub fn new() -> Self { - Self::default() - } -} - -/// File content. -#[derive(Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct FileContent { - /// The URL to the file. - pub url: Box, - - /// Information about the uploaded file. - #[serde(flatten, skip_serializing_if = "Option::is_none")] - pub info: Option>, - - /// Information on the encrypted file. - /// - /// Required if file is encrypted. - #[serde(flatten, skip_serializing_if = "Option::is_none")] - pub encryption_info: Option>, -} - -impl FileContent { - /// Creates a new non-encrypted `FileContent` with the given url and file info. - pub fn plain(url: Box, info: Option>) -> Self { - Self { url, info, encryption_info: None } - } - - /// Creates a new encrypted `FileContent` with the given url, encryption info and file info. - pub fn encrypted( - url: Box, - encryption_info: EncryptedContent, - info: Option>, - ) -> Self { - Self { url, info, encryption_info: Some(Box::new(encryption_info)) } - } - - /// Whether the file is encrypted. - pub fn is_encrypted(&self) -> bool { - self.encryption_info.is_some() - } -} - /// The payload for an extensible text message. #[derive(Clone, Debug, Serialize, Deserialize, EventContent)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] @@ -203,3 +85,121 @@ impl FileEventContent { Self { message, file: FileContent::encrypted(url, encryption_info, info), relates_to: None } } } + +/// File content. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct FileContent { + /// The URL to the file. + pub url: Box, + + /// Information about the uploaded file. + #[serde(flatten, skip_serializing_if = "Option::is_none")] + pub info: Option>, + + /// Information on the encrypted file. + /// + /// Required if file is encrypted. + #[serde(flatten, skip_serializing_if = "Option::is_none")] + pub encryption_info: Option>, +} + +impl FileContent { + /// Creates a new non-encrypted `FileContent` with the given url and file info. + pub fn plain(url: Box, info: Option>) -> Self { + Self { url, info, encryption_info: None } + } + + /// Creates a new encrypted `FileContent` with the given url, encryption info and file info. + pub fn encrypted( + url: Box, + encryption_info: EncryptedContent, + info: Option>, + ) -> Self { + Self { url, info, encryption_info: Some(Box::new(encryption_info)) } + } + + /// Whether the file is encrypted. + pub fn is_encrypted(&self) -> bool { + self.encryption_info.is_some() + } +} + +/// Information about a file content. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct FileContentInfo { + /// The original filename of the uploaded file. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The mimetype of the file, e.g. “application/msword”. + #[serde(skip_serializing_if = "Option::is_none")] + pub mimetype: Option, + + /// The size of the file in bytes. + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, +} + +impl FileContentInfo { + /// Creates an empty `FileContentInfo`. + pub fn new() -> Self { + Self::default() + } +} + +/// The encryption info of a file sent to a room with end-to-end encryption enabled. +/// +/// To create an instance of this type, first create a `EncryptedContentInit` and convert it via +/// `EncryptedContent::from` / `.into()`. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct EncryptedContent { + /// A [JSON Web Key](https://tools.ietf.org/html/rfc7517#appendix-A.3) object. + pub key: JsonWebKey, + + /// The 128-bit unique counter block used by AES-CTR, encoded as unpadded base64. + pub iv: Base64, + + /// A map from an algorithm name to a hash of the ciphertext, encoded as unpadded base64. + /// + /// Clients should support the SHA-256 hash, which uses the key sha256. + pub hashes: BTreeMap, + + /// Version of the encrypted attachments protocol. + /// + /// Must be `v2`. + pub v: String, +} + +/// Initial set of fields of `EncryptedContent`. +/// +/// This struct will not be updated even if additional fields are added to `EncryptedContent` in a +/// new (non-breaking) release of the Matrix specification. +#[derive(Debug)] +#[allow(clippy::exhaustive_structs)] +pub struct EncryptedContentInit { + /// A [JSON Web Key](https://tools.ietf.org/html/rfc7517#appendix-A.3) object. + pub key: JsonWebKey, + + /// The 128-bit unique counter block used by AES-CTR, encoded as unpadded base64. + pub iv: Base64, + + /// A map from an algorithm name to a hash of the ciphertext, encoded as unpadded base64. + /// + /// Clients should support the SHA-256 hash, which uses the key sha256. + pub hashes: BTreeMap, + + /// Version of the encrypted attachments protocol. + /// + /// Must be `v2`. + pub v: String, +} + +impl From for EncryptedContent { + fn from(init: EncryptedContentInit) -> Self { + let EncryptedContentInit { key, iv, hashes, v } = init; + Self { key, iv, hashes, v } + } +}