#![warn(missing_docs)]
use std::error;
use std::fmt;
use std::fmt::Write;
use std::str;
use regex::Regex;
use uuid::Uuid;
lazy_static::lazy_static! {
static ref DEBUG_ID_RE: Regex = Regex::new(
r"(?ix)
^
(?P<uuid>
[0-9a-f]{8}-?
[0-9a-f]{4}-?
[0-9a-f]{4}-?
[0-9a-f]{4}-?
[0-9a-f]{12}
)
-?
(?P<appendix>
[0-9a-f]{1,8}
)?
( # ignored tail
(?:-?[0-9a-f]){1,24}
)?
$
"
)
.unwrap();
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseDebugIdError;
impl error::Error for ParseDebugIdError {
fn description(&self) -> &str {
"invalid debug identifier"
}
}
impl fmt::Display for ParseDebugIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::error::Error;
write!(f, "{}", self.description())
}
}
#[repr(C, packed)]
#[derive(Default, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
pub struct DebugId {
uuid: Uuid,
appendix: u32,
_padding: [u8; 12],
}
impl DebugId {
pub fn nil() -> Self {
Self::default()
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self::from_parts(uuid, 0)
}
pub fn from_parts(uuid: Uuid, appendix: u32) -> Self {
DebugId {
uuid,
appendix,
_padding: [0; 12],
}
}
pub fn from_guid_age(guid: &[u8], age: u32) -> Result<Self, ParseDebugIdError> {
if guid.len() != 16 {
return Err(ParseDebugIdError);
}
let uuid = Uuid::from_bytes([
guid[3], guid[2], guid[1], guid[0], guid[5], guid[4], guid[7], guid[6], guid[8],
guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15],
]);
Ok(DebugId::from_parts(uuid, age))
}
pub fn from_breakpad(string: &str) -> Result<Self, ParseDebugIdError> {
string.parse()
}
pub fn uuid(&self) -> Uuid {
self.uuid
}
pub fn appendix(&self) -> u32 {
self.appendix
}
pub fn is_nil(&self) -> bool {
self.uuid.is_nil() && self.appendix() == 0
}
pub fn breakpad(&self) -> BreakpadFormat<'_> {
BreakpadFormat { inner: self }
}
}
impl fmt::Debug for DebugId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DebugId")
.field("uuid", &self.uuid().to_hyphenated_ref().to_string())
.field("appendix", &self.appendix())
.finish()
}
}
impl fmt::Display for DebugId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.uuid.fmt(f)?;
if self.appendix > 0 {
write!(f, "-{:x}", { self.appendix })?;
}
Ok(())
}
}
impl str::FromStr for DebugId {
type Err = ParseDebugIdError;
fn from_str(string: &str) -> Result<DebugId, ParseDebugIdError> {
let captures = DEBUG_ID_RE.captures(string).ok_or(ParseDebugIdError)?;
let uuid = captures
.name("uuid")
.unwrap()
.as_str()
.parse()
.map_err(|_| ParseDebugIdError)?;
let appendix = captures
.name("appendix")
.map_or(Ok(0), |s| u32::from_str_radix(s.as_str(), 16))
.map_err(|_| ParseDebugIdError)?;
Ok(DebugId::from_parts(uuid, appendix))
}
}
impl From<Uuid> for DebugId {
fn from(uuid: Uuid) -> DebugId {
DebugId::from_uuid(uuid)
}
}
impl From<(Uuid, u32)> for DebugId {
fn from(tuple: (Uuid, u32)) -> DebugId {
let (uuid, appendix) = tuple;
DebugId::from_parts(uuid, appendix)
}
}
#[derive(Debug)]
pub struct BreakpadFormat<'a> {
inner: &'a DebugId,
}
impl<'a> fmt::Display for BreakpadFormat<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:X}{:x}",
self.inner.uuid().to_simple_ref(),
self.inner.appendix()
)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseCodeIdError;
impl error::Error for ParseCodeIdError {
fn description(&self) -> &str {
"invalid code identifier"
}
}
impl fmt::Display for ParseCodeIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::error::Error;
write!(f, "{}", self.description())
}
}
#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CodeId {
inner: String,
}
impl CodeId {
pub fn nil() -> Self {
Self::default()
}
pub fn new(mut string: String) -> Self {
string.make_ascii_lowercase();
CodeId { inner: string }
}
pub fn from_binary(slice: &[u8]) -> Self {
let mut string = String::with_capacity(slice.len() * 2);
for byte in slice {
write!(&mut string, "{:02x}", byte).expect("");
}
Self::new(string)
}
pub fn is_nil(&self) -> bool {
self.inner.is_empty()
}
pub fn as_str(&self) -> &str {
self.inner.as_str()
}
}
impl fmt::Display for CodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.inner)
}
}
impl fmt::Debug for CodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CodeId({})", self)
}
}
impl From<String> for CodeId {
fn from(string: String) -> Self {
Self::new(string)
}
}
impl From<&'_ str> for CodeId {
fn from(string: &str) -> Self {
Self::new(string.into())
}
}
impl str::FromStr for CodeId {
type Err = ParseCodeIdError;
fn from_str(string: &str) -> Result<Self, ParseCodeIdError> {
Ok(Self::new(string.into()))
}
}
#[cfg(feature = "serde")]
mod serde_support {
use serde::de::{self, Deserialize, Deserializer, Unexpected, Visitor};
use serde::ser::{Serialize, Serializer};
use super::*;
impl Serialize for CodeId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for CodeId {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let string = String::deserialize(deserializer)?;
Ok(CodeId::new(string))
}
}
impl<'de> Deserialize<'de> for DebugId {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct V;
impl<'de> Visitor<'de> for V {
type Value = DebugId;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("DebugId")
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<DebugId, E> {
value
.parse()
.map_err(|_| de::Error::invalid_value(Unexpected::Str(value), &self))
}
}
deserializer.deserialize_str(V)
}
}
impl Serialize for DebugId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.to_string())
}
}
}