Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply suggestions from Clippy #20

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 54 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,24 @@ pub struct Id<'a> {
name: Cow<'a, str>,
}

#[derive(Debug)]
pub enum IdError<'a> {
EmptyName,
InvalidStartChar(char, Cow<'a, str>),
InvalidChar(char, Cow<'a, str>),
}

impl<'a> std::fmt::Display for IdError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IdError::EmptyName => write!(f, "Id cannot be empty"),
IdError::InvalidStartChar(c, id) => write!(f, "Id cannot begin with `{c}` (`{id}`)"),
IdError::InvalidChar(c, id) => write!(f, "Id cannot contain `{c}` (`{id}`)"),
}
}
}
impl<'a> std::error::Error for IdError<'a> {}

impl<'a> Id<'a> {
/// Creates an `Id` named `name`.
///
Expand All @@ -387,19 +405,20 @@ impl<'a> Id<'a> {
///
/// Passing an invalid string (containing spaces, brackets,
/// quotes, ...) will return an empty `Err` value.
pub fn new<Name: Into<Cow<'a, str>>>(name: Name) -> Result<Id<'a>, ()> {
pub fn new<Name: Into<Cow<'a, str>>>(name: Name) -> Result<Id<'a>, IdError<'a>> {
let name = name.into();
{
let mut chars = name.chars();
match chars.next() {
Some(c) if is_letter_or_underscore(c) => {}
_ => return Err(()),
Some(c) => return Err(IdError::InvalidStartChar(c, name)),
None => return Err(IdError::EmptyName),
}
if !chars.all(is_constituent) {
return Err(())
if let Some(bad) = chars.find(|c| !is_constituent(*c)) {
return Err(IdError::InvalidChar(bad, name));
}
}
return Ok(Id{ name: name });
return Ok(Id { name });

fn is_letter_or_underscore(c: char) -> bool {
in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_'
Expand All @@ -413,7 +432,7 @@ impl<'a> Id<'a> {
}

pub fn as_slice(&'a self) -> &'a str {
&*self.name
&self.name
}

pub fn name(self) -> Cow<'a, str> {
Expand Down Expand Up @@ -511,10 +530,10 @@ pub trait Labeller<'a,N,E> {
/// Graphviz HTML label.
pub fn escape_html(s: &str) -> String {
s
.replace("&", "&amp;")
.replace("\"", "&quot;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace('&', "&amp;")
.replace('"', "&quot;")
.replace('<', "&lt;")
.replace('>', "&gt;")
}

impl<'a> LabelText<'a> {
Expand Down Expand Up @@ -558,9 +577,9 @@ impl<'a> LabelText<'a> {
/// This includes quotes or suitable delimeters.
pub fn to_dot_string(&self) -> String {
match self {
&LabelStr(ref s) => format!("\"{}\"", LabelText::escape_default(s)),
&EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s[..])),
&HtmlStr(ref s) => format!("<{}>", s),
LabelStr(s) => format!("\"{}\"", LabelText::escape_default(s)),
EscStr(s) => format!("\"{}\"", LabelText::escape_str(&s[..])),
HtmlStr(s) => format!("<{}>", s),
}
}

Expand All @@ -572,7 +591,7 @@ impl<'a> LabelText<'a> {
match self {
EscStr(s) => s,
LabelStr(s) => if s.contains('\\') {
LabelText::escape_default(&*s).into()
LabelText::escape_default(&s).into()
} else {
s
},
Expand Down Expand Up @@ -605,18 +624,20 @@ pub struct Arrow {

use self::ArrowShape::*;

impl Arrow {
/// Return `true` if this is a default arrow.
fn is_default(&self) -> bool {
self.arrows.is_empty()
}

impl Default for Arrow {
/// Arrow constructor which returns a default arrow
pub fn default() -> Arrow {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy now lints against fn default() -> Self not in a Default implementation. While the crate is pre-1.0 and this is very unlikely to affect anyone, this is technically a breaking change, so I can revert if you'd prefer

fn default() -> Arrow {
Arrow {
arrows: vec![],
}
}
}

impl Arrow {
/// Return `true` if this is a default arrow.
fn is_default(&self) -> bool {
self.arrows.is_empty()
}

/// Arrow constructor which returns an empty arrow
pub fn none() -> Arrow {
Expand Down Expand Up @@ -644,33 +665,23 @@ impl Arrow {
let mut cow = String::new();
for arrow in &self.arrows {
cow.push_str(&arrow.to_dot_string());
};
}
cow
}
}


impl Into<Arrow> for [ArrowShape; 2] {
fn into(self) -> Arrow {
Arrow {
arrows: vec![self[0], self[1]],
}
}
}
impl Into<Arrow> for [ArrowShape; 3] {
fn into(self) -> Arrow {
Arrow {
arrows: vec![self[0], self[1], self[2]],
}
}
}
impl Into<Arrow> for [ArrowShape; 4] {
fn into(self) -> Arrow {
Arrow {
arrows: vec![self[0], self[1], self[2], self[3]],
}
macro_rules! arrowshape_to_arrow {
( $($n:expr), * ) => {
$(
impl From<[ArrowShape; $n]> for Arrow {
fn from(shape: [ArrowShape; $n]) -> Arrow {
Arrow {arrows: shape.to_vec() }
}
}
)*
}
}
arrowshape_to_arrow!(2, 3, 4);

/// Arrow modifier that determines if the shape is empty or filled.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -932,7 +943,7 @@ pub fn render_opts<'a,
for &s in arg {
w.write_all(s.as_bytes())?;
}
write!(w, "\n")
writeln!(w)
}

fn indent<W: Write>(w: &mut W) -> io::Result<()> {
Expand Down