Implement debug for identifier types

This commit is contained in:
Fredrik Lanker 2020-12-27 22:38:02 +01:00 committed by GitHub
parent 4831585223
commit 68ba3049ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 14 deletions

View File

@ -14,6 +14,7 @@ Improvements:
* Add `DeviceKeyId::from_parts` and `SigningKeyId::from_parts`
* Add `DeviceIdBox` and `ServerNameBox` type aliases
* Clean up debug logs for identifiers
# 0.17.4

View File

@ -1,16 +1,22 @@
//! Identifiers for device keys for end-to-end encryption.
use std::{convert::TryInto, num::NonZeroU8};
use std::{convert::TryInto, fmt, num::NonZeroU8};
use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId, Error};
/// A key algorithm and a device id, combined with a ':'
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct DeviceKeyId {
full_id: Box<str>,
colon_idx: NonZeroU8,
}
impl fmt::Debug for DeviceKeyId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl DeviceKeyId {
/// Create a `DeviceKeyId` from a `DeviceKeyAlgorithm` and a `DeviceId`.
pub fn from_parts(algorithm: DeviceKeyAlgorithm, device_id: &DeviceId) -> Self {

View File

@ -1,6 +1,6 @@
//! Matrix event identifiers.
use std::{convert::TryFrom, num::NonZeroU8};
use std::{convert::TryFrom, fmt, num::NonZeroU8};
use crate::{Error, ServerName};
@ -37,12 +37,18 @@ use crate::{Error, ServerName};
/// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
/// );
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct EventId {
full_id: Box<str>,
colon_idx: Option<NonZeroU8>,
}
impl fmt::Debug for EventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl EventId {
/// Attempts to generate an `EventId` for the given origin server with a localpart consisting
/// of 18 random ASCII characters. This should only be used for events in the original format

View File

@ -146,7 +146,6 @@ macro_rules! opaque_identifier {
) => {
$( #[doc = $docs] )*
#[repr(transparent)]
#[derive(::std::fmt::Debug)]
pub struct $id(str);
::paste::paste! {
@ -185,6 +184,12 @@ macro_rules! opaque_identifier {
}
}
impl ::std::fmt::Debug for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
f.write_str(self.as_ref())
}
}
impl Clone for ::std::boxed::Box<$id> {
fn clone(&self) -> Self {
(**self).to_owned()

View File

@ -1,6 +1,6 @@
//! Matrix room alias identifiers.
use std::{convert::TryFrom, num::NonZeroU8};
use std::{convert::TryFrom, fmt, num::NonZeroU8};
use crate::{server_name::ServerName, Error};
@ -17,12 +17,18 @@ use crate::{server_name::ServerName, Error};
/// "#ruma:example.com"
/// );
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct RoomAliasId {
pub(crate) full_id: Box<str>,
pub(crate) colon_idx: NonZeroU8,
}
impl fmt::Debug for RoomAliasId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl RoomAliasId {
/// Returns the room's alias.
pub fn alias(&self) -> &str {

View File

@ -1,6 +1,6 @@
//! Matrix room identifiers.
use std::{convert::TryFrom, num::NonZeroU8};
use std::{convert::TryFrom, fmt, num::NonZeroU8};
use crate::{Error, ServerName};
@ -17,12 +17,18 @@ use crate::{Error, ServerName};
/// "!n8f893n9:example.com"
/// );
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct RoomId {
pub(crate) full_id: Box<str>,
pub(crate) colon_idx: NonZeroU8,
}
impl fmt::Debug for RoomId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl RoomId {
/// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of
/// 18 random ASCII characters.

View File

@ -1,6 +1,6 @@
//! Matrix identifiers for places where a room ID or room alias ID are used interchangeably.
use std::{convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8};
use std::{convert::TryFrom, fmt, hint::unreachable_unchecked, num::NonZeroU8};
use crate::{server_name::ServerName, Error, RoomAliasId, RoomId};
@ -23,12 +23,18 @@ use crate::{server_name::ServerName, Error, RoomAliasId, RoomId};
/// "!n8f893n9:example.com"
/// );
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct RoomIdOrAliasId {
full_id: Box<str>,
colon_idx: NonZeroU8,
}
impl fmt::Debug for RoomIdOrAliasId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl RoomIdOrAliasId {
/// Returns the local part (everything after the `!` or `#` and before the first colon).
pub fn localpart(&self) -> &str {

View File

@ -13,7 +13,7 @@ use crate::Error;
/// A Matrix-spec compliant server name.
#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent, crate = "serde"))]
pub struct ServerName(str);
@ -45,6 +45,12 @@ impl ServerName {
}
}
impl fmt::Debug for ServerName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Clone for Box<ServerName> {
fn clone(&self) -> Self {
(**self).to_owned()

View File

@ -1,6 +1,6 @@
//! Matrix user identifiers.
use std::{convert::TryFrom, num::NonZeroU8};
use std::{convert::TryFrom, fmt, num::NonZeroU8};
use crate::{Error, ServerName};
@ -17,7 +17,7 @@ use crate::{Error, ServerName};
/// "@carl:example.com"
/// );
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct UserId {
full_id: Box<str>,
colon_idx: NonZeroU8,
@ -30,6 +30,12 @@ pub struct UserId {
is_historical: bool,
}
impl fmt::Debug for UserId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.full_id)
}
}
impl UserId {
/// Attempts to generate a `UserId` for the given origin server with a localpart consisting of
/// 12 random ASCII characters.