Skip to content

Commit

Permalink
feat(#17): clean for fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jul 10, 2024
1 parent 5e0c434 commit 2ee37bf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ use axum::Router;
use tokio::net::TcpListener;

use crate::routes::home;
use crate::routes::register_user::{register_user};
use crate::routes::register_user::register_user;
use crate::xml::storage::Storage;

pub mod report;
mod objects;
pub mod report;
mod routes;
mod xml;

Expand Down
8 changes: 3 additions & 5 deletions server/src/objects/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ impl User {
#[cfg(test)]
mod tests {
use anyhow::Result;

use crate::objects::user::User;

#[test]
fn returns_username() -> Result<()> {
let expected = "jeff";
let jeff = User::new(String::from(expected));
assert_eq!(
jeff.username,
expected,
jeff.username, expected,
"Username {} from user: {:?} does not match with expected {}",
jeff.username,
jeff,
expected
jeff.username, jeff, expected
);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
pub mod home;
pub mod rs_err;
pub mod register_user;
pub mod rs_err;
4 changes: 1 addition & 3 deletions server/src/routes/register_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub async fn register_user(Json(payload): Json<User>) -> Result<StatusCode, Stri
let user = User::new(payload.username.clone());
match user.save().await {
Ok(_) => Ok(StatusCode::CREATED),
Err(e) => Err(
format!("Can't register {}: {}", payload.username, e),
),
Err(e) => Err(format!("Can't register {}: {}", payload.username, e)),
}
}
26 changes: 14 additions & 12 deletions server/src/xml/storage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fs::File;
use std::io::Write;

// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
Expand All @@ -20,8 +23,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use anyhow::Result;
use std::fs::File;
use std::io::Write;
use log::info;

#[derive(Default)]
Expand All @@ -34,22 +35,23 @@ impl Storage {
let location = path.unwrap_or("fakehub.xml");
info!("Initializing XML storage: {location}");
let mut file = match File::create(location) {
Ok(file) => {
file
}
Ok(file) => file,
Err(err) => {
panic!("fakehub storage failed to initialize in '{location}': {err}");

Check warning on line 40 in server/src/xml/storage.rs

View check run for this annotation

Codecov / codecov/patch

server/src/xml/storage.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
}
};
if let Err(err) = file.write_all(
"<root>\
<github><users/></github>\
</root>".as_bytes()
</root>"
.as_bytes(),
) {
panic!("Failed to write initial content to '{}': {}", location, err);

Check warning on line 49 in server/src/xml/storage.rs

View check run for this annotation

Codecov / codecov/patch

server/src/xml/storage.rs#L49

Added line #L49 was not covered by tests
}
info!("'{}' initialized", location);
Storage { path: String::from(location) }
Storage {
path: String::from(location),
}
}
}

Expand All @@ -66,8 +68,10 @@ impl Storage {
#[cfg(test)]
mod tests {
use std::fs;

use anyhow::Result;
use tempdir::TempDir;

use crate::xml::storage::Storage;

#[test]
Expand All @@ -85,18 +89,16 @@ mod tests {
}

#[test]
fn reads_initial_content() -> Result<()>{
fn reads_initial_content() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = temp.path().join("fakehub.xml");
Storage::new(path.to_str());
let xml = fs::read_to_string(path).unwrap();
let expected = "<root><github><users/></github></root>";
assert_eq!(
xml,
expected,
xml, expected,
"Received initial XML {} does not match with expected {}",
xml,
expected
xml, expected
);
Ok(())
}
Expand Down

0 comments on commit 2ee37bf

Please sign in to comment.