Skip to content

Commit

Permalink
feat(#124): fakehub.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Aug 28, 2024
1 parent 23f76ff commit 5e435d5
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 151 deletions.
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ env_logger = "0.11.3"
tempdir = "0.3.7"
hamcrest = "0.1.5"
reqwest = "0.12.5"
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "macro-diagnostics", ] }
7 changes: 2 additions & 5 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ use tokio::net::TcpListener;

use crate::handlers::home;
use crate::handlers::register_user::register_user;
use crate::xml::storage::Storage;

/// Handlers.
pub mod handlers;
/// GitHub domain objects.
/// Fakehub objects.
pub mod objects;
/// Reports.
pub mod report;
/// XML storage.
pub mod xml;
#[allow(unused_imports)]
#[macro_use]
extern crate hamcrest;
Expand Down Expand Up @@ -85,7 +82,7 @@ pub struct ServerConfig {
impl Server {
/// Start a server.
pub async fn start(self) -> Result<()> {
Storage::new(Some("fakehub.xml"));
// Storage::new(Some("fakehub.xml"));
let addr: String = format!("0.0.0.0:{}", self.port);
let started: io::Result<TcpListener> = TcpListener::bind(addr.clone()).await;
match started {
Expand Down
68 changes: 68 additions & 0 deletions server/src/objects/fakehub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::objects::github::GitHub;
use uuid::Uuid;

/// Fakehub.
pub struct Fakehub {
hubs: Vec<GitHub>,
}

impl Default for Fakehub {
fn default() -> Fakehub {
Fakehub {
hubs: vec![GitHub {
id: Uuid::new_v4(),
url: String::from("https://github.com"),
}],
}
}
}

impl Fakehub {
/// New fakehub
pub fn new(hubs: Vec<GitHub>) -> Fakehub {
Fakehub { hubs }
}

/// Add new GitHub
pub fn add(mut self, github: GitHub) {
self.hubs.push(github);
}
}

#[cfg(test)]
mod tests {

use crate::objects::fakehub::Fakehub;
use anyhow::Result;
use hamcrest::{equal_to, is, HamcrestMatcher};

#[test]
fn creates_default_fakehub() -> Result<()> {
let fakehub = Fakehub::default();
let default = fakehub.hubs.first().expect("Can not obtain GitHub");
assert_that!(default.id.is_nil(), is(equal_to(false)));
assert_that!(&default.url, is(equal_to("https://github.com")));
Ok(())
}
}
9 changes: 7 additions & 2 deletions server/src/xml/mod.rs → server/src/objects/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/// Storage.
pub mod storage;
use uuid::Uuid;

/// GitHub.
pub struct GitHub {
pub(crate) id: Uuid,
pub(crate) url: String,
}
2 changes: 2 additions & 0 deletions server/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
// SOFTWARE.
/// GitHub user.
pub mod user;
pub mod fakehub;
pub mod github;
144 changes: 0 additions & 144 deletions server/src/xml/storage.rs

This file was deleted.

0 comments on commit 5e435d5

Please sign in to comment.