Skip to content

Commit

Permalink
Add rustfmt style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduerr authored Mar 30, 2019
1 parent 91aa683 commit cfd025b
Show file tree
Hide file tree
Showing 57 changed files with 1,878 additions and 2,395 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ matrix:
os: windows
env: CLIPPY=true
rust: stable
- name: "Rustfmt"
os: linux
env: RUSTFMT=true
rust: nightly
allow_failures:
- rust: nightly

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Changes compared to the latest Alacritty release which have a direct effect on t

### Style

Alacritty currently does not have any automatically enforced style guidelines. As a result of that, it is not possible to run `rustfmt` on existing files. New code should however follow the default ruleset of `rustfmt` and for newly created files it is possible to run the `rustfmt` tool directly.
All Alacritty changes are automatically verified by CI to conform to its rustfmt guidelines. If a CI build is failing because of formatting issues, you can install rustfmt using `rustup component add rustfmt` and then format all code using `cargo fmt`.

# Contact

Expand Down
25 changes: 11 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#[cfg(windows)]
use embed_resource;
#[cfg(windows)]
use tempfile;
#[cfg(windows)]
use reqwest;
#[cfg(windows)]
use tempfile;
#[cfg(windows)]
use zip;

use gl_generator::{Api, Fallbacks, GlobalGenerator, Profile, Registry};
Expand All @@ -26,25 +26,21 @@ use std::env;
use std::fs::File;
use std::path::Path;

#[cfg(windows)]
use std::io;
#[cfg(windows)]
use std::fs::OpenOptions;
#[cfg(windows)]
use std::io;

#[cfg(windows)]
const WINPTY_PACKAGE_URL: &str = "https://github.com/rprichard/winpty/releases/download/0.4.3/winpty-0.4.3-msvc2015.zip";
const WINPTY_PACKAGE_URL: &str =
"https://github.com/rprichard/winpty/releases/download/0.4.3/winpty-0.4.3-msvc2015.zip";

fn main() {
let dest = env::var("OUT_DIR").unwrap();
let mut file = File::create(&Path::new(&dest).join("gl_bindings.rs")).unwrap();

Registry::new(
Api::Gl,
(4, 5),
Profile::Core,
Fallbacks::All,
["GL_ARB_blend_func_extended"],
).write_bindings(GlobalGenerator, &mut file)
Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, ["GL_ARB_blend_func_extended"])
.write_bindings(GlobalGenerator, &mut file)
.unwrap();

#[cfg(windows)]
Expand All @@ -68,7 +64,8 @@ fn aquire_winpty_agent(out_path: &Path) {
.read(true)
.write(true)
.create(true)
.open(tmp_dir.path().join("winpty_package.zip")).unwrap();
.open(tmp_dir.path().join("winpty_package.zip"))
.unwrap();

io::copy(&mut response, &mut file).unwrap();

Expand All @@ -77,7 +74,7 @@ fn aquire_winpty_agent(out_path: &Path) {
let target = match env::var("TARGET").unwrap().split("-").next().unwrap() {
"x86_64" => "x64",
"i386" => "ia32",
_ => panic!("architecture has no winpty binary")
_ => panic!("architecture has no winpty binary"),
};

let mut winpty_agent = archive.by_name(&format!("{}/bin/winpty-agent.exe", target)).unwrap();
Expand Down
7 changes: 6 additions & 1 deletion ci/install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash

# Add clippy for linting with nightly builds
# Add clippy for lint validation
if [ "$CLIPPY" == "true" ]; then
rustup component add clippy
fi

# Add rustfmt for format validation
if [ "$RUSTFMT" == "true" ]; then
rustup component add rustfmt
fi
6 changes: 6 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ if [ "$CLIPPY" == "true" ]; then
exit
fi

# Run clippy rustfmt
if [ "$RUSTFMT" == "true" ]; then
cargo fmt -- --check
exit
fi

# Run test in release mode if a tag is present, to produce an optimized binary
if [ -n "$TRAVIS_TAG" ]; then
cargo test --release || error=true
Expand Down
46 changes: 18 additions & 28 deletions copypasta/src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
//! https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PasteboardGuide106/Articles/pbReading.html#//apple_ref/doc/uid/TP40008123-SW1
mod ns {
extern crate objc_id;
extern crate objc_foundation;
extern crate objc_id;

#[link(name = "AppKit", kind = "framework")]
extern {}
extern "C" {}

use std::mem;

use objc::runtime::{Class, Object};
use self::objc_foundation::{INSArray, INSObject, INSString};
use self::objc_foundation::{NSArray, NSDictionary, NSObject, NSString};
use self::objc_id::{Id, Owned};
use self::objc_foundation::{NSArray, NSObject, NSDictionary, NSString};
use self::objc_foundation::{INSString, INSArray, INSObject};
use objc::runtime::{Class, Object};

/// Rust API for NSPasteboard
pub struct Pasteboard(Id<Object>);
Expand Down Expand Up @@ -55,6 +55,7 @@ mod ns {

impl PasteboardReadObject<String> for Pasteboard {
type Err = ReadStringError;

fn read_object(&self) -> Result<String, ReadStringError> {
// Get string class; need this for passing to readObjectsForClasses
let ns_string_class = match Class::get("NSString") {
Expand Down Expand Up @@ -133,9 +134,7 @@ mod ns {

// The writeObjects method returns true in case of success, and
// false otherwise.
let ok: bool = unsafe {
msg_send![self.0, writeObjects:objects]
};
let ok: bool = unsafe { msg_send![self.0, writeObjects: objects] };

if ok {
Ok(())
Expand Down Expand Up @@ -175,9 +174,7 @@ mod ns {
impl ::std::error::Error for NewPasteboardError {
fn description(&self) -> &str {
match *self {
NewPasteboardError::GetPasteboardClass => {
"NSPasteboard class not found"
},
NewPasteboardError::GetPasteboardClass => "NSPasteboard class not found",
NewPasteboardError::LoadGeneralPasteboard => {
"[NSPasteboard generalPasteboard] failed"
},
Expand Down Expand Up @@ -209,9 +206,7 @@ mod ns {
}
};

let id = unsafe {
Id::from_ptr(ptr)
};
let id = unsafe { Id::from_ptr(ptr) };

Ok(Pasteboard(id))
}
Expand All @@ -222,9 +217,7 @@ mod ns {
/// This is the first step in providing data on the pasteboard. The
/// return value is the change count of the pasteboard
pub fn clear_contents(&mut self) -> usize {
unsafe {
msg_send![self.0, clearContents]
}
unsafe { msg_send![self.0, clearContents] }
}
}
}
Expand All @@ -236,7 +229,6 @@ pub enum Error {
WriteString(ns::WriteStringError),
}


impl ::std::error::Error for Error {
fn cause(&self) -> Option<&::std::error::Error> {
match *self {
Expand All @@ -258,9 +250,7 @@ impl ::std::error::Error for Error {
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Error::CreatePasteboard(ref err) => {
write!(f, "Failed to create pasteboard: {}", err)
},
Error::CreatePasteboard(ref err) => write!(f, "Failed to create pasteboard: {}", err),
Error::ReadString(ref err) => {
write!(f, "Failed to read string from pasteboard: {}", err)
},
Expand Down Expand Up @@ -301,23 +291,23 @@ impl super::Load for Clipboard {
fn load_primary(&self) -> Result<String, Self::Err> {
use self::ns::PasteboardReadObject;

self.0.read_object()
.map_err(::std::convert::From::from)
self.0.read_object().map_err(::std::convert::From::from)
}
}

impl super::Store for Clipboard {
fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err>
where S: Into<String>
where
S: Into<String>,
{
use self::ns::PasteboardWriteObject;

self.0.write_object(contents.into())
.map_err(::std::convert::From::from)
self.0.write_object(contents.into()).map_err(::std::convert::From::from)
}

fn store_selection<S>(&mut self, _contents: S) -> Result<(), Self::Err>
where S: Into<String>
where
S: Into<String>,
{
// No such thing on macOS
Ok(())
Expand All @@ -327,7 +317,7 @@ impl super::Store for Clipboard {
#[cfg(test)]
mod tests {
use super::Clipboard;
use ::{Load, Store};
use {Load, Store};

#[test]
fn create_clipboard_save_load_contents() {
Expand Down
12 changes: 3 additions & 9 deletions copypasta/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ impl Load for Clipboard {
type Err = Error;

fn new() -> Result<Self, Error> {
ClipboardContext::new()
.map(Clipboard)
.map_err(Error::Clipboard)
ClipboardContext::new().map(Clipboard).map_err(Error::Clipboard)
}

fn load_primary(&self) -> Result<String, Self::Err> {
Expand All @@ -56,9 +54,7 @@ impl Store for Clipboard {
where
S: Into<String>,
{
self.0
.set_contents(contents.into())
.map_err(Error::Clipboard)
self.0.set_contents(contents.into()).map_err(Error::Clipboard)
}

/// Sets the secondary clipboard contents
Expand All @@ -67,8 +63,6 @@ impl Store for Clipboard {
where
S: Into<String>,
{
self.0
.set_contents(contents.into())
.map_err(Error::Clipboard)
self.0.set_contents(contents.into()).map_err(Error::Clipboard)
}
}
48 changes: 20 additions & 28 deletions copypasta/src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//!
//! FIXME: Implement actual X11 clipboard API using the ICCCM reference
//! https://tronche.com/gui/x/icccm/
use std::ffi::OsStr;
use std::io;
use std::process::{Output, Command};
use std::process::{Command, Output};
use std::string::FromUtf8Error;
use std::ffi::OsStr;

use super::{Load, Store};

Expand Down Expand Up @@ -45,13 +45,11 @@ impl ::std::error::Error for Error {
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Error::Io(ref err) => {
match err.kind() {
io::ErrorKind::NotFound => {
write!(f, "Please install `xclip` to enable clipboard support")
},
_ => write!(f, "Error calling xclip: {}", err),
}
Error::Io(ref err) => match err.kind() {
io::ErrorKind::NotFound => {
write!(f, "Please install `xclip` to enable clipboard support")
},
_ => write!(f, "Error calling xclip: {}", err),
},
Error::Xclip(ref s) => write!(f, "Error from xclip: {}", s),
Error::Utf8(ref err) => write!(f, "Error parsing xclip output: {}", err),
Expand Down Expand Up @@ -79,17 +77,13 @@ impl Load for Clipboard {
}

fn load_primary(&self) -> Result<String, Self::Err> {
let output = Command::new("xclip")
.args(&["-o", "-selection", "clipboard"])
.output()?;
let output = Command::new("xclip").args(&["-o", "-selection", "clipboard"]).output()?;

Clipboard::process_xclip_output(output)
}

fn load_selection(&self) -> Result<String, Self::Err> {
let output = Command::new("xclip")
.args(&["-o"])
.output()?;
let output = Command::new("xclip").args(&["-o"]).output()?;

Clipboard::process_xclip_output(output)
}
Expand All @@ -99,15 +93,17 @@ impl Store for Clipboard {
/// Sets the primary clipboard contents
#[inline]
fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err>
where S: Into<String>
where
S: Into<String>,
{
self.store(contents, &["-i", "-selection", "clipboard"])
}

/// Sets the secondary clipboard contents
#[inline]
fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err>
where S: Into<String>
where
S: Into<String>,
{
self.store(contents, &["-i"])
}
Expand All @@ -116,26 +112,22 @@ impl Store for Clipboard {
impl Clipboard {
fn process_xclip_output(output: Output) -> Result<String, Error> {
if output.status.success() {
String::from_utf8(output.stdout)
.map_err(::std::convert::From::from)
String::from_utf8(output.stdout).map_err(::std::convert::From::from)
} else {
String::from_utf8(output.stderr)
.map_err(::std::convert::From::from)
String::from_utf8(output.stderr).map_err(::std::convert::From::from)
}
}

fn store<C, S>(&mut self, contents: C, args: &[S]) -> Result<(), Error>
where C: Into<String>,
S: AsRef<OsStr>,
where
C: Into<String>,
S: AsRef<OsStr>,
{
use std::io::Write;
use std::process::{Command, Stdio};

let contents = contents.into();
let mut child = Command::new("xclip")
.args(args)
.stdin(Stdio::piped())
.spawn()?;
let mut child = Command::new("xclip").args(args).stdin(Stdio::piped()).spawn()?;

if let Some(stdin) = child.stdin.as_mut() {
stdin.write_all(contents.as_bytes())?;
Expand All @@ -154,7 +146,7 @@ impl Clipboard {
#[cfg(test)]
mod tests {
use super::Clipboard;
use ::{Load, Store};
use {Load, Store};

#[test]
fn clipboard_works() {
Expand Down
Loading

0 comments on commit cfd025b

Please sign in to comment.