Skip to content

Commit

Permalink
Add Github Action for CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
bednie committed Aug 27, 2024
1 parent 0f43870 commit 1a44c43
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 25 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run examples
run: cargo run --example basic_usage
15 changes: 15 additions & 0 deletions examples/basic_usage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use stringvec::stringvec;

fn main() {
let mixed_types = stringvec!["hello", 42, 'A', 3.5];
println!("Mixed types vector: {:?}", mixed_types);

let names = stringvec!["Alice", "Bob", "Charlie"];
println!("Names: {:?}", names);

// Using with variables
let age = 30;
let height = 5.9;
let info = stringvec!["John Doe", age, height];
println!("Person info: {:?}", info);
}
82 changes: 57 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
//! # stringvec
//!
//! `stringvec` is a Rust library that provides a macro for easily creating
//! a `Vec<String>` from various types, along with a utility function to check
//! if a value is a `String`.

use std::any::{Any, TypeId};

#[macro_export]
macro_rules! stringvec {
($($element:expr),* $(,)?) => {
vec![$($element.to_string()),*]
};
}
/// Creates a `Vec<String>` from a list of expressions.
///
/// This macro converts each expression to a `String` using the `to_string()`
/// method and collects them into a `Vec<String>`.
///
/// # Examples
///
/// ```
/// use stringvec::stringvec;
///
/// let words = stringvec!["hello", 42, 'A', 3.14];
/// assert_eq!(words, vec!["hello", "42", "A", "3.14"]);
/// ```
#[macro_export]
macro_rules! stringvec {
($($element:expr),* $(,)?) => {
vec![$($element.to_string()),*]
};
}

pub fn is_string(s: &dyn Any) -> bool {
TypeId::of::<String>() == s.type_id()
}
/// Checks if the provided value is a `String`.
///
/// # Examples
///
/// ```
/// use stringvec::is_string;
///
/// let s = "hello".to_string();
/// let i = 42;
///
/// assert!(is_string(&s));
/// assert!(!is_string(&i));
/// ```
pub fn is_string(s: &dyn Any) -> bool {
TypeId::of::<String>() == s.type_id()
}

#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_svec_macro() {
let words = stringvec!["cat", 11, 'A', 3.14];
assert_eq!(words, vec!["cat", "11", "A", "3.14"]);
}
#[test]
fn test_stringvec_macro() {
let words = stringvec!["cat", 11, 'A', 3.5];
assert_eq!(words, vec!["cat", "11", "A", "3.5"]);
}

#[test]
fn test_is_string() {
let s = "Hello".to_string();
let i = 42;
assert!(is_string(&s));
assert!(!is_string(&i));
}
}
#[test]
fn test_is_string() {
let s = "Hello".to_string();
let i = 42;
assert!(is_string(&s));
assert!(!is_string(&i));
}
}

0 comments on commit 1a44c43

Please sign in to comment.