Skip to content

Instantly share code, notes, and snippets.

@kinggoesgaming
Created May 20, 2017 01:16
Show Gist options
  • Save kinggoesgaming/9f7fb1e3d4e1ce4612af24ff10e2ec4c to your computer and use it in GitHub Desktop.
Save kinggoesgaming/9f7fb1e3d4e1ce4612af24ff10e2ec4c to your computer and use it in GitHub Desktop.
/*
Crate: ore
File: /project/channel.rs
Module: ::project::channel
Visibility: public
*/
// TODO: documentation
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Error as DeserializeError, Visitor};
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::result::Result as StdResult;
use std::num::ParseIntError;
// TODO: documentation
#[derive(Clone, Copy, Debug)]
pub struct Channel<'a> {
color: Color,
name: &'a str,
}
// TODO: documentation
#[derive(Clone, Debug)]
pub enum ColorError {
Format,
Length(usize),
Parse(ParseIntError),
}
// TODO: documentation
#[derive(Clone, Copy, Debug)]
pub enum Color {
RGB(u8, u8, u8),
Transparent,
}
impl<'a> Deserialize<'a> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'a>
{
deserializer.deserialize_str(ColorVisitor)
}
}
impl Serialize for Color {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer {
unimplemented!()
}
}
impl Display for ColorError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
unimplemented!()
}
}
impl StdError for ColorError {
fn description(&self) -> &str {
""
}
fn cause(&self) -> Option<&StdError> {
None
}
}
// TODO: documentation
struct ColorVisitor;
impl<'a> Visitor<'a> for ColorVisitor {
type Value = Color;
fn expecting(&self, f: &mut Formatter) -> FmtResult {
write!(f, r#"a string in the format #xxxxxx"#)
}
fn visit_str<E>(self, v: &str) -> StdResult<Self::Value, E>
where E: StdError,
{
if v.len() != 7 {
Err(ColorError::Format)
} else {
Ok(Color::Transparent)
}
}
}
/usr/bin/cargo build --color=always
Compiling ore v0.1.0 (file:///home/hunardev/src/ore)
error[E0308]: mismatched types
--> src/project/channel.rs:83:17
|
83 | Err(ColorError::Format)
| ^^^^^^^^^^^^^^^^^^ expected type parameter, found enum `project::channel::ColorError`
|
= note: expected type `E`
found type `project::channel::ColorError`
error: aborting due to previous error
error: Could not compile `ore`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment