Skip to content

Commit

Permalink
feat: proper extension for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 23, 2024
1 parent 503142d commit 48684e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
31 changes: 19 additions & 12 deletions reqtui/src/collection/collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::collection::{
errors::CollectionError,
types::{Collection, Info},
};
use crate::collection::types::{Collection, Info};
use std::{
path::Path,
time::{self, UNIX_EPOCH},
Expand All @@ -27,7 +24,7 @@ where
let collection_name = collections_dir.as_ref().join(file_name);
let file = std::fs::read_to_string(&collection_name)?;
let mut collection: Collection = serde_json::from_str(&file)?;
collection.path = collection_name;
collection.path = collection_name.join(".json");
collections.push(collection);
}

Expand All @@ -36,10 +33,7 @@ where
Ok(collections)
}

pub fn create_from_form(
name: String,
description: String,
) -> anyhow::Result<Collection, CollectionError> {
pub fn create_from_form(name: String, description: String) -> Collection {
let name = if name.is_empty() {
let now = time::SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -54,12 +48,25 @@ pub fn create_from_form(
let name_as_file_name = name.to_lowercase().replace(' ', "_");
let collection_name = collections_dir.join(name_as_file_name);

Ok(Collection {
Collection {
info: Info {
name,
description: Some(description),
},
requests: None,
path: collection_name,
})
path: format!("{}.json", collection_name.to_string_lossy()).into(),
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_creating_from_form() {
let collection = create_from_form("any valid name".into(), "any desctiption".into());

assert!(collection.path.to_string_lossy().ends_with(".json"));
assert!(collection.info.name.eq("any valid name"));
assert!(collection.info.description.is_some())
}
}
4 changes: 0 additions & 4 deletions reqtui/src/collection/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
pub enum CollectionError {
IOError(String),
SerializationError(String),
Unknown(String),
}

impl std::fmt::Display for CollectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CollectionError::IOError(msg) => write!(f, "{}", msg),
CollectionError::SerializationError(msg) => write!(f, "{}", msg),
CollectionError::Unknown(msg) => write!(f, "{}", msg),
}
}
Expand Down
2 changes: 1 addition & 1 deletion reqtui/src/fs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn create_collection(
name: String,
description: String,
) -> anyhow::Result<Collection, FsError> {
let collection = create_from_form(name, description).map_err(|_| FsError::Unknown)?;
let collection = create_from_form(name, description);

if collection.path.exists() {
return Err(FsError::CollectionAlreadyExists(
Expand Down

0 comments on commit 48684e1

Please sign in to comment.