Skip to content

Commit

Permalink
[433] remove old run function
Browse files Browse the repository at this point in the history
  • Loading branch information
sepiropht committed Aug 14, 2018
1 parent 7e59e4e commit 80e4992
Show file tree
Hide file tree
Showing 68 changed files with 147 additions and 148 deletions.
13 changes: 6 additions & 7 deletions src/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Since these recipes are intended to be reused as-is and encourage best
practices, they set up error handling correctly when there are
`Result` types involved.

The basic pattern we use is to have a `fn run() -> Result` that acts
The basic pattern we use is to have a `fn main() -> Result` that acts
like the "real" main function. We use the [error-chain] crate to make
`?` work within `run`.

Expand All @@ -127,7 +127,7 @@ error_chain! {
}
}

fn run() -> Result<()> {
fn main() -> Result<()> {
let bytes = b"2001:db8::1";

// Bytes to string.
Expand All @@ -140,14 +140,13 @@ fn run() -> Result<()> {
Ok(())
}

quick_main!(run);
```

This is using the `error_chain!` macro to define a custom `Error` and
`Result` type, along with automatic conversions from two standard
library error types. The automatic conversions make the `?` operator
work. The `quick_main!` macro generates the actual `main` function and
prints out the error if one occurred.
work. Since rust 1.26, the main function can return a value, no need
to use a custom `run` function like before.

For the sake of readability error handling boilerplate is hidden by
default like below. In order to read full contents click on the
Expand All @@ -167,14 +166,14 @@ use url::{Url, Position};
# }
# }

fn run() -> Result<()> {
fn main() -> Result<()> {
let parsed = Url::parse("https://httpbin.org/cookies/set?k2=v2&k1=v1")?;
let cleaned: &str = &parsed[..Position::AfterPath];
println!("cleaned: {}", cleaned);
Ok(())
}
#
# quick_main!(run);

```

For more background on error handling in Rust, read [this page of the
Expand Down
4 changes: 2 additions & 2 deletions src/compression/tar/tar-compress.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use std::fs::File;
use flate2::Compression;
use flate2::write::GzEncoder;
fn run() -> Result<()> {
fn main() -> Result<()> {
let tar_gz = File::create("archive.tar.gz")?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("backup/logs", "/var/log")?;
Ok(())
}
#
# quick_main!(run);
```

[`Builder::append_dir_all`]: https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all
Expand Down
4 changes: 2 additions & 2 deletions src/compression/tar/tar-decompress.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tar::Archive;
# }
# }
fn run() -> Result<()> {
fn main() -> Result<()> {
let path = "archive.tar.gz";
let tar_gz = File::open(path)?;
Expand All @@ -34,7 +34,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`Archive::unpack`]: https://docs.rs/tar/*/tar/struct.Archive.html#method.unpack
Expand Down
4 changes: 2 additions & 2 deletions src/compression/tar/tar-strip-prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::path::PathBuf;
use flate2::read::GzDecoder;
use tar::Archive;
fn run() -> Result<()> {
fn main() -> Result<()> {
let file = File::open("archive.tar.gz")?;
let mut archive = Archive::new(GzDecoder::new(file));
let prefix = "bundle/logs";
Expand All @@ -44,7 +44,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`Archive::entries`]: https://docs.rs/tar/*/tar/struct.Archive.html#method.entries
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/parallel/rayon-thumbnails.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rayon::prelude::*;
# }
# }
fn run() -> Result<()> {
fn main() -> Result<()> {
let options: MatchOptions = Default::default();
let files: Vec<_> = glob_with("*.jpg", &options)?
.filter_map(|x| x.ok())
Expand Down Expand Up @@ -73,7 +73,7 @@ where
.save(file_path)?)
}
#
# quick_main!(run);
```

[`glob::glob_with`]: https://docs.rs/glob/*/glob/fn.glob_with.html
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/thread/global-mut-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn insert(fruit: &str) -> Result<()> {
Ok(())
}

fn run() -> Result<()> {
fn main() -> Result<()> {
insert("apple")?;
insert("orange")?;
insert("peach")?;
Expand All @@ -42,7 +42,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/thread/threadpool-fractal.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use image::{ImageBuffer, Pixel, Rgb};
# ((color * factor).powf(0.8) * 255.) as u8
# }
fn run() -> Result<()> {
fn main() -> Result<()> {
let (width, height) = (1920, 1080);
let mut img = ImageBuffer::new(width, height);
let iterations = 300;
Expand All @@ -115,7 +115,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`ImageBuffer::new`]: https://docs.rs/image/*/image/struct.ImageBuffer.html#method.new
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/thread/threadpool-walk.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn compute_digest<P: AsRef<Path>>(filepath: P) -> Result<(Digest, P)> {
Ok((context.finish(), filepath))
}
fn run() -> Result<()> {
fn main() -> Result<()> {
let pool = ThreadPool::new(num_cpus::get());
let (tx, rx) = channel();
Expand All @@ -80,7 +80,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`execute`]: https://docs.rs/threadpool/*/threadpool/struct.ThreadPool.html#method.execute
Expand Down
4 changes: 2 additions & 2 deletions src/cryptography/encryption/pbkdf2.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use data_encoding::HEXUPPER;
use ring::{digest, pbkdf2, rand};
use ring::rand::SecureRandom;

fn run() -> Result<()> {
fn main() -> Result<()> {
const CREDENTIAL_LEN: usize = digest::SHA512_OUTPUT_LEN;
const N_ITER: u32 = 100_000;
let rng = rand::SystemRandom::new();
Expand Down Expand Up @@ -67,7 +67,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`pbkdf2::derive`]: https://briansmith.org/rustdoc/ring/pbkdf2/fn.derive.html
Expand Down
4 changes: 2 additions & 2 deletions src/cryptography/hashing/hmac.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern crate ring;
use ring::{digest, hmac, rand};
use ring::rand::SecureRandom;

fn run() -> Result<()> {
fn main() -> Result<()> {
let mut key_value = [0u8; 48];
let rng = rand::SystemRandom::new();
rng.fill(&mut key_value)?;
Expand All @@ -31,7 +31,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`hmac::Signature`]: https://briansmith.org/rustdoc/ring/hmac/struct.Signature.html
Expand Down
4 changes: 2 additions & 2 deletions src/cryptography/hashing/sha-digest.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn sha256_digest<R: Read>(mut reader: R) -> Result<Digest> {
Ok(context.finish())
}

fn run() -> Result<()> {
fn main() -> Result<()> {
let path = "file.txt";

let mut output = File::create(path)?;
Expand All @@ -53,7 +53,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`digest::Context`]: https://briansmith.org/rustdoc/ring/digest/struct.Context.html
Expand Down
4 changes: 2 additions & 2 deletions src/datetime/parse/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
# }
# }

fn run() -> Result<()> {
fn main() -> Result<()> {
let rfc2822 = DateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")?;
println!("{}", rfc2822);

Expand All @@ -48,7 +48,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`chrono::format::strftime`]: https://docs.rs/chrono/*/chrono/format/strftime/index.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/build_tools/cc-bundled-static.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ extern {
fn greet(name: *const c_char);
}
fn run() -> Result<()> {
fn main() -> Result<()> {
unsafe { hello() }
let name = prompt("What's your name? ")?;
let c_name = CString::new(name)?;
unsafe { greet(c_name.as_ptr()) }
Ok(())
}
#
# quick_main!(run);
```

[`cc::Build::define`]: https://docs.rs/cc/*/cc/struct.Build.html#method.define
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/debugging/config_log/log-custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use log4rs::config::{Appender, Config, Root};
# }
# }
fn run() -> Result<()> {
fn main() -> Result<()> {
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{l} - {m}\n")))
.build("log/output.log")?;
Expand All @@ -49,7 +49,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`log4rs::append::file::FileAppender`]: https://docs.rs/log4rs/*/log4rs/append/file/struct.FileAppender.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/debugging/log/log-custom-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl log::Log for ConsoleLogger {
# }
# }

fn run() -> Result<()> {
fn main() -> Result<()> {
log::set_logger(&CONSOLE_LOGGER)?;
log::set_max_level(LevelFilter::Info);

Expand All @@ -48,7 +48,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`log::Log`]: https://docs.rs/log/*/log/trait.Log.html
Expand Down
6 changes: 3 additions & 3 deletions src/development_tools/debugging/log/log-syslog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use syslog::Facility;
# }

# #[cfg(target_os = "linux")]
fn run() -> Result<()> {
fn main() -> Result<()> {
syslog::init(Facility::LOG_USER,
log::LevelFilter::Debug,
Some("My app name"))?;
Expand All @@ -39,11 +39,11 @@ fn run() -> Result<()> {
# #[cfg(not(target_os = "linux"))]
# error_chain! {}
# #[cfg(not(target_os = "linux"))]
# fn run() -> Result<()> {
# fn main() -> Result<()> {
# Ok(())
# }
#
# quick_main!(run);

```

[`log::LevelFilter`]: https://docs.rs/log/*/log/enum.LevelFilter.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/versioning/semver-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use semver::{Version, VersionReq};
# }
# }
fn run() -> Result<()> {
fn main() -> Result<()> {
let version_constraint = "> 1.12.0";
let version_test = VersionReq::parse(version_constraint)?;
let output = Command::new("git").arg("--version").output()?;
Expand All @@ -47,7 +47,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);
```

[`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/versioning/semver-complex.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use semver::{Identifier, Version};
# }
# }

fn run() -> Result<()> {
fn main() -> Result<()> {
let version_str = "1.0.49-125+g72ee7853";
let parsed_version = Version::parse(version_str)?;

Expand All @@ -46,7 +46,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`semver::Version`]: https://docs.rs/semver/*/semver/struct.Version.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/versioning/semver-increment.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use semver::Version;
# }
# }

fn run() -> Result<()> {
fn main() -> Result<()> {
let mut parsed_version = Version::parse("0.2.6")?;

assert_eq!(
Expand Down Expand Up @@ -52,7 +52,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`semver::Version`]: https://docs.rs/semver/*/semver/struct.Version.html
Expand Down
4 changes: 2 additions & 2 deletions src/development_tools/versioning/semver-latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
)
}

fn run() -> Result<()> {
fn main() -> Result<()> {
assert_eq!(
find_max_matching_version("<= 1.0.0", vec!["0.9.0", "1.0.0", "1.0.1"])?,
Some(Version::parse("1.0.0")?)
Expand All @@ -58,7 +58,7 @@ fn run() -> Result<()> {
Ok(())
}
#
# quick_main!(run);

```

[`semver::Version`]: https://docs.rs/semver/*/semver/struct.Version.html
Expand Down
Loading

0 comments on commit 80e4992

Please sign in to comment.