Skip to content

Commit

Permalink
fix(cmn): updated code logic, tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Feb 8, 2023
1 parent ded4de7 commit 2a48daf
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 73 deletions.
28 changes: 13 additions & 15 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ jobs:
- armv7-unknown-linux-gnueabihf # ARMv7 Linux, hardfloat (kernel 3.2, glibc 2.17) ✅ Tested
- i686-unknown-linux-gnu # 32-bit Linux (kernel 3.2+, glibc 2.17+) ✅ Tested
- i686-unknown-linux-musl # 32-bit Linux (kernel 3.2+, musl libc) ✅ Tested
# - mips-unknown-linux-gnu # MIPS Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mips-unknown-linux-musl # MIPS Linux (kernel 2.6.32+, musl libc) ❌ Tested
# - mips64-unknown-linux-gnuabi64 # MIPS64 Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mips64el-unknown-linux-gnuabi64 # MIPS64el Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mipsel-unknown-linux-gnu # MIPSel Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - powerpc64le-unknown-linux-gnu # 64-bit PowerPC Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mips-unknown-linux-gnu # MIPS Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mips-unknown-linux-musl # MIPS Linux (kernel 2.6.32+, musl libc) ❌ Tested
# - mips64-unknown-linux-gnuabi64 # MIPS64 Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mips64el-unknown-linux-gnuabi64 # MIPS64el Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - mipsel-unknown-linux-gnu # MIPSel Linux (kernel 2.6.32+, glibc 2.11+) ❌ Tested
# - powerpc64le-unknown-linux-gnu # 64-bit PowerPC Linux (kernel 2.6.32+, glibc 2.11+)❌ Tested
- x86_64-unknown-linux-gnu # 64-bit Linux (kernel 2.6.32+, glibc 2.11+) ✅ Tested
- x86_64-unknown-linux-musl # 64-bit Linux (kernel 2.6.32+, musl libc) ✅ Tested

# macOS targets 🍎
- x86_64-apple-darwin # 64-bit macOS (10.7 Lion or later) ✅ Tested

# Windows targets 🪟
# - i686-pc-windows-msvc # 32-bit Windows ❌ Tested
# - x86_64-pc-windows-msvc # 64-bit Windows ❌ Tested
# - i686-pc-windows-msvc # 32-bit Windows ❌ Tested
# - x86_64-pc-windows-msvc # 64-bit Windows ❌ Tested

include:
# Linux targets 🐧
Expand Down Expand Up @@ -138,19 +138,17 @@ jobs:

- name: Run Cargo Format 🦀
id: run-check-format
if: github.ref == !github.event.check_run.conclusion
run: |
cargo check --all --all-features --release --verbose
- name: Run Clippy 🦀
id: run-check-clippy
if: github.ref == !github.event.check_run.conclusion
run: |
cargo clippy --all-targets --all-features --release -- -D warnings
- name: Run Cargo Doc 🦀
id: run-check-doc
if: github.ref == 'refs/heads/main' && !github.event.check_run.conclusion
if: github.ref == 'refs/heads/main'
run: |
cargo doc --color always --no-deps --all-features --release --verbose
Expand All @@ -162,7 +160,7 @@ jobs:
- name: Run Cargo Code Coverage 🦀
id: run-check-code-coverage
if: github.ref == 'refs/heads/main' && !github.event.check_run.conclusion
if: github.ref == 'refs/heads/main'
run: |
# Install tarpaulin
cargo install cargo-tarpaulin
Expand All @@ -172,7 +170,7 @@ jobs:
- name: Upload to codecov.io 📊
id: upload-codecov
if: github.ref == 'refs/heads/main' && !github.event.check_run.conclusion
if: github.ref == 'refs/heads/main'
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand All @@ -196,7 +194,7 @@ jobs:

- name: Generate Changelog 📜
id: generate-changelog
if: github.ref == 'refs/heads/main' && !github.event.check_run.conclusion
if: github.ref == 'refs/heads/main'
run: |
# Append version information to CHANGELOG.md
echo "## [${{ env.VERSION }}] - $(date +'%Y-%m-%d')" >> ${{ github.workspace }}/CHANGELOG.md
Expand All @@ -211,7 +209,7 @@ jobs:
- name: Create Release 🚀
id: create_release
if: github.ref == 'refs/heads/main' && !github.event.check_run.conclusion
if: github.ref == 'refs/heads/main'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ license = "MIT OR Apache-2.0"
name = "cmn"
repository = "https://github.com/sebastienrousseau/cmn/"
rust-version = "1.67"
version = "0.0.1"
version = "0.0.2"
include = [
"../../LICENSE-APACHE",
"../../LICENSE-MIT",
Expand Down
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

A Rust library for accessing a collection of mathematical and cryptographic constants

[![Made With Love][made-with-rust]][6]
[![Crates.io][crates-badge]][8]
[![Lib.rs][libs-badge]][10]
[![Docs.rs][docs-badge]][9]
[![License][license-badge]][2]
[![Codecov][codecov-badge]][11]
[![Made With Love][made-with-rust]][6] [![Crates.io][crates-badge]][8] [![Lib.rs][libs-badge]][10] [![Docs.rs][docs-badge]][9] [![License][license-badge]][2] [![Codecov][codecov-badge]][11]

![divider][divider]

Expand Down Expand Up @@ -77,7 +72,7 @@ To use `cmn` in your project, add the following to your

```toml
[dependencies]
cmn = "0.0.1"
cmn = "0.0.2"
```

Add the following to your `main.rs` file:
Expand Down Expand Up @@ -146,6 +141,6 @@ lot of useful suggestions on how to improve this project.
[crates-badge]: https://img.shields.io/crates/v/cmn.svg?style=for-the-badge 'Crates.io'
[divider]: https://raw.githubusercontent.com/sebastienrousseau/vault/main/assets/elements/divider.svg "divider"
[docs-badge]: https://img.shields.io/docsrs/cmn.svg?style=for-the-badge 'Docs.rs'
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.1-orange.svg?style=for-the-badge 'Lib.rs'
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.2-orange.svg?style=for-the-badge 'Lib.rs'
[license-badge]: https://img.shields.io/crates/l/cmn.svg?style=for-the-badge 'License'
[made-with-rust]: https://img.shields.io/badge/rust-f04041?style=for-the-badge&labelColor=c0282d&logo=rust 'Made With Rust'
2 changes: 1 addition & 1 deletion examples/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use cmn::Words;

fn main() {
// Create a Constants instance
let c = Constants {};
let c = Constants::new();

// Retrieve the list of constants
let constants = c.constants();
Expand Down
80 changes: 48 additions & 32 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use serde::{Deserialize, Serialize};

/// Contains several commonly used mathematical and cryptographic
/// constants.
/// Contains several commonly used mathematical and cryptographic constants.
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Constant {
/// The name of the constant.
Expand All @@ -14,9 +13,13 @@ pub struct Constant {
/// The value of the constant.
pub value: String,
}

/// The `Constants` structure holds mathematical and hash constants.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Constants;
#[derive(Clone, Serialize, Debug)]
pub struct Constants {
/// A vector of constants.
pub constants: Vec<Constant>,
}

impl Constants {
/// Returns a vector of tuples with the constant name and its value.
Expand All @@ -38,14 +41,12 @@ impl Constants {
/// ```
///
pub fn constant(&self, name: &str) -> Option<Constant> {
let constants = self.constants();
for constant in &constants {
if constant.name == name {
return Some(constant.clone());
}
}
constants.into_iter().find(|constant| constant.name == name)
self.constants
.iter()
.find(|constant| constant.name == name)
.cloned()
}

/// Returns a vector of tuples with the constant name and its value.
///
/// # Examples
Expand All @@ -54,12 +55,29 @@ impl Constants {
/// extern crate cmn;
/// use cmn::constants::Constants;
///
/// let constants = Constants.constants();
/// assert!(constants.len() >= 9);
/// let constants = Constants::new();
/// assert!(constants.constants().len() >= 9);
///
/// ```
pub fn constants(&self) -> &Vec<Constant> {
&self.constants
}

/// Create a new instance of the `Constants` structure.
///
/// # Example
///
/// ```
/// extern crate cmn;
/// use cmn::constants::Constants;
///
/// let constants = Constants::new();
/// assert_eq!(constants.constants().len(), 13);
///
/// ```
pub fn constants(&self) -> Vec<Constant> {
vec![
///
pub fn new() -> Self {
let constants = vec![
Constant {
name: "EULER",
value: EULER.to_string(),
Expand Down Expand Up @@ -98,33 +116,31 @@ impl Constants {
},
Constant {
name: "SPECIAL_CHARS",
value: format!("{SPECIAL_CHARS:?}"),
value: SPECIAL_CHARS.iter().collect::<String>(),
},
Constant {
name: "SQRT2",
value: SQRT2.to_string(),
},
Constant {
name: "SQRT3",
value: SQRT3.to_string(),
},
Constant {
name: "SQRT5",
value: SQRT5.to_string(),
},
]
];

Self { constants }
}
/// Create a new instance of the `Constants` structure.
///
/// # Example
///
/// ```
/// extern crate cmn;
/// use cmn::constants::Constants;
///
/// let constants = Constants::new();
/// assert_eq!(constants.constants().len(), 12);
///
/// ```
///
pub fn new() -> Self {
Self {}

/// Returns `true` if the `Constants` structure is valid.
/// Otherwise, returns `false`.
pub fn is_valid(&self) -> bool {
self.constants()
.iter()
.all(|constant| !constant.name.is_empty() && !constant.value.is_empty())
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! [![Rust](https://img.shields.io/badge/rust-f04041?style=for-the-badge&labelColor=c0282d&logo=rust)](https://www.rust-lang.org)
//! [![Crates.io](https://img.shields.io/crates/v/cmn.svg?style=for-the-badge&color=success&labelColor=27A006)](https://crates.io/crates/cmn)
//! [![Lib.rs](https://img.shields.io/badge/lib.rs-v0.0.1-success.svg?style=for-the-badge&color=8A48FF&labelColor=6F36E4)](https://lib.rs/crates/cmn)
//! [![Lib.rs](https://img.shields.io/badge/lib.rs-v0.0.2-success.svg?style=for-the-badge&color=8A48FF&labelColor=6F36E4)](https://lib.rs/crates/cmn)
//! [![GitHub](https://img.shields.io/badge/github-555555?style=for-the-badge&labelColor=000000&logo=github)](https://github.com/sebastienrousseau/cmn)
//! [![License](https://img.shields.io/crates/l/cmn.svg?style=for-the-badge&color=007EC6&labelColor=03589B)](http://opensource.org/licenses/MIT)
//!
Expand Down Expand Up @@ -71,10 +71,9 @@
//! use cmn::Words;
//!
//! // Constants
//! let constants = Constants.constants();
//! for constant in constants {
//! println!("Name: {} Value: {}", constant.name, constant.value);
//! }
//! let constants = Constants::new();
//! let constant = constants.constant("EULER");
//! assert_eq!(constant.unwrap().name, "EULER");
//!
//! // Words
//! let words = Words::new();
Expand Down Expand Up @@ -113,12 +112,11 @@ use serde::{Deserialize, Serialize};
/// provides a collection of constant values that are used throughout
/// the library.
pub mod constants;
pub use constants::Constants;

/// The `words` module contains the `Words` structure, which provides a
/// collection of words that are used throughout the library.
pub mod words;

pub use constants::Constants;
pub use words::Words;

/// The `Common` structure provides a central location to store data
Expand All @@ -135,7 +133,9 @@ impl Common {
}
/// Returns the `Constants` instance.
pub fn constants(&self) -> Constants {
Constants
Constants {
..Default::default()
}
}
/// Returns a new instance of the `Words` structure.
pub fn words(&self) -> Words {
Expand Down
6 changes: 6 additions & 0 deletions src/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/// The `serde` crate provides the `Serialize` and `Deserialize` traits
/// that are used to serialize and deserialize the data.
extern crate serde;
use serde::{Deserialize, Serialize};

/// Contains several words for use in generating passphrases.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Words;

impl Words {
Expand Down
19 changes: 14 additions & 5 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ mod tests {

#[test]
fn test_new() {
let new_constant = Constants.constants();
let new_constant = Constants::new();
let constants = new_constant;
assert!(constants.len() > 0);
assert!(constants.is_valid());
assert!(constants.constants().len() >= 9);
assert!(constants.constants().len() <= 16);
}

#[test]
fn test_common_new() {
let common = Common::new();
assert!(common.constants().is_valid());
assert!(common.constants().constants().len() >= 9);
}

#[test]
fn test_constants() {
let constants = Constants.constants();
assert!(constants.len() >= 9);
let constants = Constants::new();
assert!(constants.constants.len() >= 9);
}

#[test]
Expand All @@ -26,6 +35,6 @@ mod tests {
#[test]
fn test_default() {
let common = Common::default();
assert_eq!(common.constants().constants().len(), 12);
assert_eq!(common.constants().constants().len(), 13);
}
}
6 changes: 3 additions & 3 deletions tests/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod tests {
fn test_constants() {
let new_constant = Constants::new();
let constants = new_constant.constants();
assert_eq!(constants.len(), 12);
assert_eq!(constants.len(), 13);

let names = constants.iter().map(|c| c.name).collect::<Vec<_>>();
assert!(names.contains(&"EULER"));
Expand All @@ -38,12 +38,12 @@ mod tests {
fn test_new() {
let new_constant = Constants::new();
let constants = new_constant.constants();
assert!(constants.len() > 0);
assert!(!constants.is_empty());
}
#[test]
fn test_default() {
let default_constant = Constants::default();
let constants = default_constant.constants();
assert!(constants.len() > 0);
assert!(!constants.is_empty());
}
}

0 comments on commit 2a48daf

Please sign in to comment.