Skip to content

Commit

Permalink
Merge pull request #13 from JerryHue/lab-7
Browse files Browse the repository at this point in the history
Setup rustfmt and clippy
  • Loading branch information
JerryHue authored Nov 6, 2021
2 parents 2eff94e + fb0c851 commit 32b3783
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"rust-lang.rust"
]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust"
}
}
93 changes: 93 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Contributing to `yassgy`

## Setting up the project

### Prerequisites

You would need to download the following:

- `rustc` version 1.54.0 or greater,
- `cargo` version 1.54.0 or greater,
- `rustfmt` version 1.4.37 or greater,
- `rust-clippy` version 0.1.55 or greater.

This also entails that you'd need to download the dependencies that Rust
requires. Such dependencies may differ if you are developing on Linux, or Windows.

All tools can be installed in a bundle using `rustup`, the recommended tool
to manage Rust toolchains. The tool can be found
[here](https://www.rust-lang.org/tools/install).

Note: for Windows users, installing `rustup` requires
[`Microsoft C++ Build Tools`](https://visualstudio.microsoft.com/visual-cpp-build-tools/).
These can be download either by downloading Visual Studio and installing the
C++ SDK, or just downloading the build tools separately.

### Building the project

After you have cloned the project, you may invoke the following command at
the root directory of the project:

```bash
cargo build
```

After building is finished, you will find a `target` directory. The binary
executable is found at `target/debug/yassgy`.

## Starting to contribute

If you want to contribute, you can always file an issue, or make a PR.

### Filing an issue

`yassgy` is just starting, so there is a lot of room to improve upon it!

If you are filing a bug, please describe the steps you took to replicate. If
you cannot replicate, we won't be able to know how to fix it.

If you are filing an improvement, always describe what you would like to
improve, and how you would like to improve it. We may also use the issue
to discuss how this improvement can be carried out.

If you are filing a new feature, describe what the feature is supposed to
be like, and then we can discuss a plan on how to implement it.

### Making a PR

To create a PR, you may fork your project and add that as your `origin`
remote repo reference on your local git repository. If you already
this repo as your `origin` remote, you may rename it to `upstream`.

All changes must be done on a separate topic branch that addresses the
issue at hand (or part of an issue), and a PR merge must be done
from the topic branch on your fork to the upstream's master branch.

If your PR addresses the issue satisfactorily, a review process will
start. If your PR does not address the issue, then your PR will be closed.

You may also create a draft PR if you would like to provide some progress
to your PR, and to also provide some discussion with respect to your PR.

#### Formatting and Linting

All the code added must adhere to the coding style set by `rustfmt`.
Before committing your code, you may format you code with the following command:

```bash
cargo fmt
```

If you prefer, you may set a configuration in your editor of choice
to run this command every time you save a file.

Also, the code must be run through `clippy`, Rust's official linter.
You may run the linter with the following command:

```bash
cargo clippy
```

However, since `clippy` produces a compilation of the project, I wouldn't
recommend running every time you save a file, since it would slow down
your productivity.
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,10 @@

Yet Another Static Site Generator, Yassgy for short, is, as it name entails, a static site generator.

## Requirements
## Setting up and development

While Rust can compile programs into static executables, I have not tried to compile my projects in
that way, yet. Thus, yassgy can only be used in a development environment for now.

The tools you need are:

- `rustc version 1.54 or above`
- `cargo version 1.54 or above`

Both tools can be installed in a bundle using `rustup`, the recommended tool to manage Rust toolchains.
The tool can be found [here](https://www.rust-lang.org/tools/install).

### If you are a Windows user

Note that having installed the [`Microsoft C++ Build Tools`](https://visualstudio.microsoft.com/visual-cpp-build-tools/)
is a requirement for `rustup`.

After you have installed Rust, run the following command in the project's root folder:

```cargo build```

and you should have the binary `yassgy` in the `yassgy/target/debug` directory.
To know more about setting up a development environment, check the
[contribution guidelines](./CONTRIBUTING.md).

## Usage

Expand Down
27 changes: 27 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Explicit formatting configuration

# General formatting
force_explicit_abi = true
match_arm_leading_pipes = "Never"
max_width = 80
newline_style = "Unix"
remove_nested_parens = true
use_small_heuristics = "Default"
use_try_shorthand = true

# Importing and module-specific formatting
reorder_imports = true
reorder_modules = true

# Struct-specific formatting
use_field_init_shorthand = true

# Function-specific formatting
fn_args_layout = "Tall"

# Macro-specific formatting
merge_derives = true

# Tabs-specific formatting
hard_tabs = false
tab_spaces = 4
39 changes: 22 additions & 17 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env::Args;
use std::option;
use std::fs;
use std::option;

#[derive(Debug)]
enum Option {
Expand All @@ -22,19 +22,20 @@ pub enum Command {
},
}

fn parse_config(config: &String) -> Vec<Option> {
let mut options = Vec::new();
fn parse_config(config: &str) -> Vec<Option> {
let mut options = Vec::new();

let file = fs::File::open(config).expect("file should open read only");
let json: serde_json::Value = serde_json::de::from_reader(file).expect("unable to read to json");
let json: serde_json::Value =
serde_json::de::from_reader(file).expect("unable to read to json");

if let option::Option::Some(file_name) = json["input"].as_str() {
options.push(Option::InputPath(file_name.to_owned()));
}
}
if let option::Option::Some(output_path) = json["output"].as_str() {
options.push(Option::OutputPath(output_path.to_owned()));
}
if let option::Option::Some(language_tag) = json["lang"].as_str(){
if let option::Option::Some(language_tag) = json["lang"].as_str() {
options.push(Option::Language(language_tag.to_owned()));
}
options
Expand Down Expand Up @@ -87,7 +88,7 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
let mut output_dir_path = None;
let mut language_tag = None;

while let Some(option) = opts.next() {
for option in &mut opts {
if let Option::OutputPath(output_path) = option {
if output_dir_path == None {
output_dir_path = Some(output_path);
Expand All @@ -99,8 +100,10 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
}
}

let output_dir_path = output_dir_path.unwrap_or(String::from("dist"));
let language_tag = language_tag.unwrap_or(String::from("en-CA"));
let output_dir_path =
output_dir_path.unwrap_or_else(|| String::from("dist"));
let language_tag =
language_tag.unwrap_or_else(|| String::from("en-CA"));

Command::GenerateSite {
input_path,
Expand All @@ -112,7 +115,7 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
let mut input_path = None;
let mut language_tag = None;

while let Some(option) = opts.next() {
for option in &mut opts {
if let Option::InputPath(input_pathname) = option {
if input_path == None {
input_path = Some(input_pathname);
Expand All @@ -124,11 +127,12 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
}
}

let language_tag = language_tag.unwrap_or(String::from("en-CA"));
let language_tag =
language_tag.unwrap_or_else(|| String::from("en-CA"));

if input_path.is_some() {
if let Some(input_path) = input_path {
Command::GenerateSite {
input_path: input_path.unwrap(),
input_path,
output_dir_path,
language_tag,
}
Expand All @@ -140,7 +144,7 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
let mut input_path = None;
let mut output_dir_path = None;

while let Some(option) = opts.next() {
for option in opts {
if let Option::InputPath(input_pathname) = option {
if input_path == None {
input_path = Some(input_pathname);
Expand All @@ -152,11 +156,12 @@ fn parse_opts_as_command(opts: Vec<Option>) -> Command {
}
}

let output_dir_path = output_dir_path.unwrap_or(String::from("dist"));
let output_dir_path =
output_dir_path.unwrap_or_else(|| String::from("dist"));

if input_path.is_some() {
if let Some(input_path) = input_path {
Command::GenerateSite {
input_path: input_path.unwrap(),
input_path,
output_dir_path,
language_tag,
}
Expand Down
23 changes: 18 additions & 5 deletions src/html_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ enum ErrorKind {
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrorKind::PathNameNotTextFile => write!(f, "path to file is not txt file"),
ErrorKind::PathNameNotTextFile => {
write!(f, "path to file is not txt file")
}
}
}
}
Expand All @@ -34,7 +36,9 @@ pub struct HtmlPage {
}

impl HtmlPage {
fn is_path_to_text_file<P: AsRef<path::Path>>(file_name: &P) -> io::Result<()> {
fn is_path_to_text_file<P: AsRef<path::Path>>(
file_name: &P,
) -> io::Result<()> {
let file_name_ref = file_name.as_ref();

if let Some(extension) = file_name_ref.extension() {
Expand All @@ -50,7 +54,10 @@ impl HtmlPage {
))
}

pub fn new<P: AsRef<path::Path>>(file_name: &P, language_tag: &str) -> io::Result<HtmlPage> {
pub fn new<P: AsRef<path::Path>>(
file_name: &P,
language_tag: &str,
) -> io::Result<HtmlPage> {
HtmlPage::is_path_to_text_file(&file_name)?;

let file = fs::File::open(file_name)?;
Expand Down Expand Up @@ -109,7 +116,10 @@ impl HtmlPage {
let third_line = &actual_lines[2];

if second_line.is_empty() && third_line.is_empty() {
return (Some(first_line.clone()), &actual_lines[(start + 3)..=end]);
return (
Some(first_line.clone()),
&actual_lines[(start + 3)..=end],
);
}
}

Expand All @@ -124,7 +134,10 @@ impl HtmlPage {
self.file_name.file_stem().expect("").to_string_lossy()
}

pub fn write_to_file<P: AsRef<path::Path>>(&self, dir_path: P) -> io::Result<()> {
pub fn write_to_file<P: AsRef<path::Path>>(
&self,
dir_path: P,
) -> io::Result<()> {
let output_path = dir_path
.as_ref()
.join(self.file_name.file_name().unwrap())
Expand Down
20 changes: 15 additions & 5 deletions src/static_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub struct StaticSite {
}

impl StaticSite {
pub fn from_directory<P: AsRef<path::Path>>(input_path: P) -> io::Result<StaticSite> {
pub fn from_directory<P: AsRef<path::Path>>(
input_path: P,
) -> io::Result<StaticSite> {
let mut dir_paths: Vec<path::PathBuf> = Vec::new();
let mut file_paths: Vec<path::PathBuf> = Vec::new();

Expand All @@ -25,7 +27,8 @@ impl StaticSite {
let mut input_dir_entries = input_path.read_dir()?;

loop {
let (mut txt_files, dirs) = StaticSite::get_txt_files_and_dirs(&mut input_dir_entries);
let (mut txt_files, dirs) =
StaticSite::get_txt_files_and_dirs(&mut input_dir_entries);

file_paths.append(&mut txt_files);

Expand All @@ -38,7 +41,9 @@ impl StaticSite {
if let Ok(dir_entries) = input_path.read_dir() {
input_dir_entries = dir_entries;
} else {
println!("A directory had to be skipped due to some error.");
println!(
"A directory had to be skipped due to some error."
);
}

continue;
Expand Down Expand Up @@ -114,7 +119,11 @@ impl StaticSite {
(text_file_paths, dir_paths)
}

pub fn create<P: AsRef<path::Path>>(&self, output_dir: P, lang_tag: &str) -> io::Result<()> {
pub fn create<P: AsRef<path::Path>>(
&self,
output_dir: P,
lang_tag: &str,
) -> io::Result<()> {
let mut index: Vec<(String, path::PathBuf)> = vec![];

let output_folder_path = output_dir.as_ref();
Expand Down Expand Up @@ -166,7 +175,8 @@ impl StaticSite {
index: &[(String, path::PathBuf)],
lang_tag: &str,
) -> io::Result<()> {
let mut out_file = fs::File::create(output_folder.as_ref().join("index.html"))?;
let mut out_file =
fs::File::create(output_folder.as_ref().join("index.html"))?;

let mut ul = String::from("<ul>");

Expand Down

0 comments on commit 32b3783

Please sign in to comment.