From c12f8700b2b825252d1c2552a25c8877a4a30d4b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 17 May 2021 00:28:40 +0200 Subject: [PATCH] events: Make last remaining types non-exhaustive --- crates/ruma-events/src/room.rs | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/crates/ruma-events/src/room.rs b/crates/ruma-events/src/room.rs index ed4f1810..63ddbcb0 100644 --- a/crates/ruma-events/src/room.rs +++ b/crates/ruma-events/src/room.rs @@ -108,7 +108,11 @@ impl ThumbnailInfo { } /// A file sent to a room with end-to-end encryption enabled. +/// +/// To create an instance of this type, first create a `EncryptedFileInit` and convert it via +/// `EncryptedFile::from` / `.into()`. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct EncryptedFile { /// The URL to the file. pub url: MxcUri, @@ -127,8 +131,41 @@ pub struct EncryptedFile { pub v: String, } +/// Initial set of fields of `EncryptedFile`. +/// +/// This struct will not be updated even if additional fields are added to `EncryptedFile` in a new +/// (non-breaking) release of the Matrix specification. +pub struct EncryptedFileInit { + /// The URL to the file. + pub url: MxcUri, + + /// 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: String, + + /// 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 EncryptedFile { + fn from(init: EncryptedFileInit) -> Self { + let EncryptedFileInit { url, key, iv, hashes, v } = init; + Self { url, key, iv, hashes, v } + } +} + /// A [JSON Web Key](https://tools.ietf.org/html/rfc7517#appendix-A.3) object. +/// +/// To create an instance of this type, first create a `JsonWebKeyInit` and convert it via +/// `JsonWebKey::from` / `.into()`. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct JsonWebKey { /// Key type. Must be `oct`. pub kty: String, @@ -146,3 +183,32 @@ pub struct JsonWebKey { /// [W3C extension](https://w3c.github.io/webcrypto/#iana-section-jwk). pub ext: bool, } + +/// Initial set of fields of `JsonWebKey`. +/// +/// This struct will not be updated even if additional fields are added to `JsonWebKey` in a new +/// (non-breaking) release of the Matrix specification. +pub struct JsonWebKeyInit { + /// Key type. Must be `oct`. + pub kty: String, + + /// Key operations. Must at least contain `encrypt` and `decrypt`. + pub key_ops: Vec, + + /// Required. Algorithm. Must be `A256CTR`. + pub alg: String, + + /// The key, encoded as url-safe unpadded base64. + pub k: String, + + /// Extractable. Must be `true`. This is a + /// [W3C extension](https://w3c.github.io/webcrypto/#iana-section-jwk). + pub ext: bool, +} + +impl From for JsonWebKey { + fn from(init: JsonWebKeyInit) -> Self { + let JsonWebKeyInit { kty, key_ops, alg, k, ext } = init; + Self { kty, key_ops, alg, k, ext } + } +}