Skip to content

Commit

Permalink
Merge pull request #10 from sebastienrousseau/feature/mini-functions
Browse files Browse the repository at this point in the history
Feature/mini functions
  • Loading branch information
sebastienrousseau authored Jan 1, 2023
2 parents 31de50a + c22c313 commit 6e0ad3b
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 40 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
Cargo.lock
Icon?
src/.DS_Store
tests/*.*
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ readme = "README.md"
repository = "https://github.com/sebastienrousseau/mini-functions.git"
resolver = "2" # Enables the new Cargo resolution engine
rust-version = "1.57.0"
version = "0.0.1"
version = "0.0.2"

[badges]
maintenance = { status = "actively-developed" }

[dependencies]
time = { version ="0.3.17", features = ["default"] }

[dev-dependencies]
regex = "1.7.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[Request Feature][3]
[Contributing Guidelines][4]**

## Welcome to the Mini Functions Library for Rust 👋 (v0.0.1)
## Welcome to the Mini Functions Library for Rust 👋 (v0.0.2)

Mini Functions defines a base layer of functionality for writing Rust
applications. It provides a set of functions that can be used in your
Expand All @@ -33,24 +33,35 @@ prototype for each function.

`mini-functions` requires Rust **1.57.0** or later.

### Table 1 - Mini Functions Library for Rust
The `mini-functions` library consists of the following functions listed
in alphabetical order in the following tables.

### Table 1 - Date Functions (date.rs)

| Function | Include File | Function Prototype | Description |
| -------- | ------------ | ------------------ | ----------- |
| `Date::date()` | `date.rs` | `fn date()` | Returns the current date in UTC format. |
| `Date::day()` | `date.rs` | `fn day()` | Returns the current day. |
| `Date::hour()` | `date.rs` | `fn hour()` | Returns the current hour. |
| `Date::iso_8601() | `date.rs` | `fn iso_8601()` | Returns the current date and time in ISO 8601 format. |
| `Date::microsecond()` | `date.rs` | `fn microsecond()` | Returns the current microsecond. |
| `Date::millisecond()` | `date.rs` | `fn millisecond()` | Returns the current millisecond. |
| `Date::minute()` | `date.rs` | `fn minute()` | Returns the current minute. |
| `Date::month()` | `date.rs` | `fn month()` | Returns the current month. |
| `Date::nanosecond()` | `date.rs` | `fn nanosecond()` | Returns the current nanosecond. |
| `Date::now()` | `date.rs` | `fn now()` | Returns the current date and time in UTC format. |
| `Date::now_utc()` | `date.rs` | `fn now_utc()` | Returns the current date and time in UTC format. |
| `Date::second()` | `date.rs` | `fn second()` | Returns the current second. |
| `Date::timestamp()` | `date.rs` | `fn timestamp()` | Returns the current timestamp. |
| `Date::timezone()` | `date.rs` | `fn timezone()` | Returns the current timezone. |
| `Date::weekday()` | `date.rs` | `fn weekday()` | Returns the current weekday. |
| `Date::year()` | `date.rs` | `fn year()` | Returns the current year. |

### Table 2 - Log Function (log.rs)

| Function | Include File | Function Prototype | Description |
| -------- | ------------ | ------------------ | ----------- |
| `Log::new()` | `log.rs` | `fn new()` | Creates a new log instance. |
| `Log::log()` | `log.rs` | `fn log()` | Logs a message to the console.|

![divider][divider]

## Semantic Versioning Policy 🚥
Expand Down
7 changes: 4 additions & 3 deletions examples/basic.rs → examples/date.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

use mini_functions::date::Date;

fn main() {
let date = Date::date();
let day = Date::day();
let hour = Date::hour();
let iso_8601 = Date::iso_8601();
let microsecond = Date::microsecond();
let millisecond = Date::millisecond();
let minute = Date::minute();
Expand All @@ -19,14 +19,15 @@ fn main() {
println!("✅ Date::date(): {}", date);
println!("✅ Date::day(): {}", day);
println!("✅ Date::hour(): {}", hour);
println!("✅ Date::iso_8601(): {}", iso_8601);
println!("✅ Date::microsecond(): {}", microsecond);
println!("✅ Date::millisecond(): {}", millisecond);
println!("✅ Date::minute(): {}", minute);
println!("✅ Date::month(): {}", month);
println!("✅ Date::nanosecond(): {}", nanosecond);
println!("✅ Date::now(): {}", now_utc);
println!("✅ Date::now_utc(): {}", now_utc);
println!("✅ Date::second(): {}", second);
println!("✅ Date::timestamp(): {}", timestamp);
println!("✅ Date::weekday(): {}", weekday);
println!("✅ Date::year(): {}", year);
}
}
12 changes: 12 additions & 0 deletions examples/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use mini_functions::{date::Date, log::Log};

fn main() {
let log = Log::new(
"12345678-1234-1234-1234-1234567890ab",
&Date::now_utc(),
"INFO",
"SystemTrayEvent",
"Showing main window",
);
log.log();
}
77 changes: 46 additions & 31 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ impl Date {
OffsetDateTime::now_utc().date().to_string()
}

/// Create a new OffsetDateTime with the current date and time (UTC)
pub fn now_utc() -> String {
OffsetDateTime::now_utc().to_string()
}
/// Get the year of the date in the stored offset.
pub fn year() -> String {
OffsetDateTime::now_utc().year().to_string()
}
/// Get the month of the date in the stored offset.
///
/// The month is a number from 1 to 12.
/// January is 1, February is 2, and so on.
pub fn month() -> String {
OffsetDateTime::now_utc().month().to_string()
}
/// Get the day of the date in the stored offset.
///
/// The day is a number from 1 to 31.
Expand All @@ -50,36 +35,62 @@ impl Date {
pub fn hour() -> String {
OffsetDateTime::now_utc().hour().to_string()
}
/// Get the minute within the hour in the stored offset.
/// Get the ISO week number of the date in the stored offset.
///
/// The returned value will always be in the range 0..60.
pub fn minute() -> String {
OffsetDateTime::now_utc().minute().to_string()
/// The returned value will always be in the range 1..=53.
pub fn iso_week() -> String {
OffsetDateTime::now_utc().iso_week().to_string()
}
/// Get the second within the minute in the stored offset.
/// Get the microsecond of the second in the stored offset.
///
/// The returned value will always be in the range 0..60.
pub fn second() -> String {
OffsetDateTime::now_utc().second().to_string()
/// The returned value will always be in the range 0..1_000_000.
pub fn microsecond() -> String {
OffsetDateTime::now_utc().microsecond().to_string()
}
/// Get the milliseconds within the second in the stored offset.
///
/// The returned value will always be in the range 0..1_000.
pub fn millisecond() -> String {
OffsetDateTime::now_utc().millisecond().to_string()
}
/// Get the microsecond of the second in the stored offset.
/// Get the minute within the hour in the stored offset.
///
/// The returned value will always be in the range 0..1_000_000.
pub fn microsecond() -> String {
OffsetDateTime::now_utc().microsecond().to_string()
/// The returned value will always be in the range 0..60.
pub fn minute() -> String {
OffsetDateTime::now_utc().minute().to_string()
}
/// Get the month of the date in the stored offset.
///
/// The month is a number from 1 to 12.
/// January is 1, February is 2, and so on.
pub fn month() -> String {
OffsetDateTime::now_utc().month().to_string()
}
/// Get the nanoseconds within the second in the stored offset.
///
/// The returned value will always be in the range 0..1_000_000_000.
pub fn nanosecond() -> String {
OffsetDateTime::now_utc().nanosecond().to_string()
}
/// Create a new OffsetDateTime in the ISO 8601 format.
pub fn iso_8601() -> String {
let now = OffsetDateTime::now_utc();
let iso_8601 = format!(
"{}T{:02}:{:02}:{:02}.{:03}{:+03}{:02}",
now.date(),
now.hour(),
now.minute(),
now.second(),
now.millisecond(),
now.offset().whole_hours(),
now.offset().minutes_past_hour()
);
iso_8601
}
/// Create a new OffsetDateTime with the current date and time (UTC)
pub fn now_utc() -> String {
OffsetDateTime::now_utc().to_string()
}
/// Get the UtcOffset.
pub fn offset() -> String {
OffsetDateTime::now_utc().offset().to_string()
Expand All @@ -90,11 +101,11 @@ impl Date {
pub fn ordinal() -> String {
OffsetDateTime::now_utc().ordinal().to_string()
}
/// Get the ISO week number of the date in the stored offset.
/// Get the second within the minute in the stored offset.
///
/// The returned value will always be in the range 1..=53.
pub fn iso_week() -> String {
OffsetDateTime::now_utc().iso_week().to_string()
/// The returned value will always be in the range 0..60.
pub fn second() -> String {
OffsetDateTime::now_utc().second().to_string()
}
/// Get the Time in the stored offset.
pub fn time() -> String {
Expand All @@ -110,4 +121,8 @@ impl Date {
pub fn weekday() -> String {
OffsetDateTime::now_utc().weekday().to_string()
}
/// Get the year of the date in the stored offset.
pub fn year() -> String {
OffsetDateTime::now_utc().year().to_string()
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
)]

pub mod date;
pub mod log;
91 changes: 91 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! # Core Log functionality
//!
//! Log provides an easy way to log a message to the console with a simple, readable output format.
//!
// Copyright © 2022-2023 Mini Functions. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/// Implements [`Log`] to log a message to the console with a simple, readable output format.
///
/// # Arguments
///
/// * `session_id` - A string slice that holds a session ID. The session ID is a unique identifier for the current session. A random GUID (Globally Unique Identifier) is generated by default.
/// * `time` - A string slice that holds the timestamp in ISO 8601 format.
/// * `level` - A string slice that holds the level (INFO, WARN, ERROR, etc.).
/// * `component` - A string slice that holds the component name.
/// * `description` - A string slice that holds the description of the log message.
///
/// # Examples
/// ```
/// use mini_functions::log::Log;
///
/// let log = Log::new(
/// "12345678-1234-1234-1234-1234567890ab",
/// "2023-12-23T23:23:23.222222+00:00",
/// "INFO",
/// "SystemTrayEvent",
/// "Showing main window",
/// );
/// ```
///
#[non_exhaustive]
#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct Log {
session_id: String,
time: String,
level: String,
component: String,
description: String,
}

impl Log {
/// Create a new `Log` instance.
/// # Arguments
/// * `session_id` - A string slice that holds a session ID. The session ID is a unique identifier for the current session. A random GUID (Globally Unique Identifier) is generated by default.
/// * `time` - A string slice that holds the timestamp in ISO 8601 format.
/// * `level` - A string slice that holds the level (INFO, WARN, ERROR, etc.).
/// * `component` - A string slice that holds the component name.
/// * `description` - A string slice that holds the description of the log message.
///
/// # Returns
/// A new `Log` instance.
///
/// # Examples
///
/// ```
/// use mini_functions::log::Log;
///
/// let log = Log::new(
/// "12345678-1234-1234-1234-1234567890ab",
/// "2023-12-23T23:23:23.222222+00:00",
/// "INFO",
/// "SystemTrayEvent",
/// "Showing main window",
/// );
/// ```
#[must_use]
pub fn new(
session_id: &str,
time: &str,
level: &str,
component: &str,
description: &str,
) -> Self {
Self {
session_id: session_id.to_string(),
time: time.to_string(),
level: level.to_string(),
component: component.to_string(),
description: description.to_string(),
}
}
pub fn log(&self) {
println!(
"SessionID={} Timestamp={} Level={} Component={} Description=\"{}\"",
self.session_id, self.time, self.level, self.component, self.description
);
}
}
Loading

0 comments on commit 6e0ad3b

Please sign in to comment.