180 lines
4.2 KiB
Rust
180 lines
4.2 KiB
Rust
use std::fmt;
|
|
use std::str::FromStr;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::prelude::*;
|
|
|
|
pub use radicle_crdt::clock::Physical as Timestamp;
|
|
|
|
/// Author.
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
pub struct Author {
|
|
pub id: NodeId,
|
|
}
|
|
|
|
impl Author {
|
|
pub fn new(id: NodeId) -> Self {
|
|
Self { id }
|
|
}
|
|
|
|
pub fn id(&self) -> &NodeId {
|
|
&self.id
|
|
}
|
|
}
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum ReactionError {
|
|
#[error("invalid reaction")]
|
|
InvalidReaction,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Serialize, Deserialize)]
|
|
#[serde(transparent)]
|
|
pub struct Reaction {
|
|
pub emoji: char,
|
|
}
|
|
|
|
impl Reaction {
|
|
pub fn new(emoji: char) -> Result<Self, ReactionError> {
|
|
if emoji.is_whitespace() || emoji.is_ascii() || emoji.is_alphanumeric() {
|
|
return Err(ReactionError::InvalidReaction);
|
|
}
|
|
Ok(Self { emoji })
|
|
}
|
|
}
|
|
|
|
impl FromStr for Reaction {
|
|
type Err = ReactionError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let mut chars = s.chars();
|
|
let first = chars.next().ok_or(ReactionError::InvalidReaction)?;
|
|
|
|
// Reactions should not consist of more than a single emoji.
|
|
if chars.next().is_some() {
|
|
return Err(ReactionError::InvalidReaction);
|
|
}
|
|
Reaction::new(first)
|
|
}
|
|
}
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum TagError {
|
|
#[error("invalid tag name: `{0}`")]
|
|
InvalidName(String),
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
|
|
#[serde(transparent)]
|
|
pub struct Tag(String);
|
|
|
|
impl Tag {
|
|
pub fn new(name: impl Into<String>) -> Result<Self, TagError> {
|
|
let name = name.into();
|
|
|
|
if name.chars().any(|c| c.is_whitespace()) || name.is_empty() {
|
|
return Err(TagError::InvalidName(name));
|
|
}
|
|
Ok(Self(name))
|
|
}
|
|
|
|
pub fn name(&self) -> &str {
|
|
self.0.as_str()
|
|
}
|
|
}
|
|
|
|
impl FromStr for Tag {
|
|
type Err = TagError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
Self::new(s)
|
|
}
|
|
}
|
|
|
|
impl From<Tag> for String {
|
|
fn from(Tag(name): Tag) -> Self {
|
|
name
|
|
}
|
|
}
|
|
|
|
/// RGB color.
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
|
pub struct Color(u32);
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum ColorConversionError {
|
|
#[error("invalid format: expect '#rrggbb'")]
|
|
InvalidFormat,
|
|
#[error(transparent)]
|
|
ParseInt(#[from] std::num::ParseIntError),
|
|
}
|
|
|
|
impl fmt::Display for Color {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "#{:06x}", self.0)
|
|
}
|
|
}
|
|
|
|
impl FromStr for Color {
|
|
type Err = ColorConversionError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let hex = s.replace('#', "").to_lowercase();
|
|
|
|
if hex.chars().count() != 6 {
|
|
return Err(ColorConversionError::InvalidFormat);
|
|
}
|
|
|
|
match u32::from_str_radix(&hex, 16) {
|
|
Ok(n) => Ok(Color(n)),
|
|
Err(e) => Err(e.into()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Serialize for Color {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::ser::Serializer,
|
|
{
|
|
let s = self.to_string();
|
|
serializer.serialize_str(&s)
|
|
}
|
|
}
|
|
|
|
impl<'a> Deserialize<'a> for Color {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: serde::de::Deserializer<'a>,
|
|
{
|
|
let color = String::deserialize(deserializer)?;
|
|
Self::from_str(&color).map_err(serde::de::Error::custom)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_color() {
|
|
let c = Color::from_str("#ffccaa").unwrap();
|
|
assert_eq!(c.to_string(), "#ffccaa".to_owned());
|
|
assert_eq!(serde_json::to_string(&c).unwrap(), "\"#ffccaa\"".to_owned());
|
|
assert_eq!(serde_json::from_str::<'_, Color>("\"#ffccaa\"").unwrap(), c);
|
|
|
|
let c = Color::from_str("#0000aa").unwrap();
|
|
assert_eq!(c.to_string(), "#0000aa".to_owned());
|
|
|
|
let c = Color::from_str("#aa0000").unwrap();
|
|
assert_eq!(c.to_string(), "#aa0000".to_owned());
|
|
|
|
let c = Color::from_str("#00aa00").unwrap();
|
|
assert_eq!(c.to_string(), "#00aa00".to_owned());
|
|
|
|
Color::from_str("#aa00").unwrap_err();
|
|
Color::from_str("#abc").unwrap_err();
|
|
}
|
|
}
|