From eef73a05773639222f48b0ff8a103c1d0306d019 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 7 Aug 2024 17:47:26 +0900 Subject: [PATCH] 0.1.9 --- Cargo.toml | 2 +- README.md | 3 +- RELEASE_NOTE.md | 6 +++- src/lib.rs | 3 +- src/string.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/string.rs diff --git a/Cargo.toml b/Cargo.toml index 627b0c1..eca2a66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "device-common" -version = "0.1.6" +version = "0.1.9" edition = "2021" authors = ["Ryan Bae "] diff --git a/README.md b/README.md index 08586b9..67df8f5 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ device-service 에서 공통으로 쓰이는 기능들의 집합 - log.rs : log 관련 기능 / static function 으로 사용 가능한 expect_or_log - metrics.rs : cpu / memory 등 기본 metric 수집 기능 - orchestration.rs : orchestration 정보를 로드하는 기능 +- string.rs : string 확장 기능 - aws/kinesis.rs : AWS Kinesis Stream 관련 기능 - aws/parameter_store.rs : AWS Parameter Store 관련 기능 -- aws/s3.rs : AWS S3 관련 기능 \ No newline at end of file +- aws/s3.rs : AWS S3 관련 기능] \ No newline at end of file diff --git a/RELEASE_NOTE.md b/RELEASE_NOTE.md index a2b7520..0f8e703 100644 --- a/RELEASE_NOTE.md +++ b/RELEASE_NOTE.md @@ -36,4 +36,8 @@ first deploy 0.1.8 ====== -- added campaign database information \ No newline at end of file +- added campaign database information + +0.1.9 +====== +- added string.rs \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 00e432d..bf56651 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,4 +7,5 @@ pub mod env; pub mod orchestration; pub mod metrics; pub mod constants; -pub mod aws; \ No newline at end of file +pub mod aws; +pub mod string; \ No newline at end of file diff --git a/src/string.rs b/src/string.rs new file mode 100644 index 0000000..9203669 --- /dev/null +++ b/src/string.rs @@ -0,0 +1,84 @@ +use std::path::{Path, PathBuf}; + +pub fn remove_extension(file_path: &str) -> String { + let path = Path::new(file_path); + + match path.file_stem() { + Some(stem) => { + let mut new_path = PathBuf::from(stem); + + if let Some(parent) = path.parent() { + new_path = parent.join(stem); + } + + new_path.to_string_lossy().into_owned() + } + None => file_path.to_string() + } +} + +pub fn remove_path_and_get_filename(file_path: &str) -> Option { + let path = Path::new(file_path); + + path.file_name() + .map(|name| name.to_string_lossy().into_owned()) +} + +pub fn get_directory_path(file_path: &str) -> Option { + let path = Path::new(file_path); + + path.parent() + .map(|p| p.to_string_lossy().into_owned()) +} + +#[cfg(test)] +mod tests { + use std::env; + use super::*; + use serial_test::serial; + use crate::env::{get, set}; + + fn setup() { + env::remove_var("RUST_PROFILE"); + } + + fn teardown() { + env::remove_var("RUST_PROFILE"); + } + + #[test] + #[serial] + fn test_remove_extension() { + setup(); + let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; + let b= remove_extension(a); + + //println!("테스트 중: value = {}", b); + assert!(b.eq("apm-bigdata/flower-device/1111|2222|3333|4444")); + teardown(); + } + + #[test] + #[serial] + fn test_remove_path_and_get_filename() { + setup(); + let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; + let b= remove_path_and_get_filename(a).unwrap(); + + //println!("테스트 중: value = {}", b); + assert!(b.eq("1111|2222|3333|4444.csv")); + teardown(); + } + + #[test] + #[serial] + fn test_get_directory_path() { + setup(); + let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; + let b= get_directory_path(a).unwrap(); + + println!("테스트 중: value = {}", b); + assert!(b.eq("apm-bigdata/flower-device")); + teardown(); + } +} \ No newline at end of file