Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 3, 2024
2 parents 2aa773b + 717c9a2 commit 5551f71
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 17 deletions.
4 changes: 4 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ tempdir = "0.3.7"
hamcrest = "0.1.5"
reqwest = "0.12.5"
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "macro-diagnostics", ] }
md-5 = "0.11.0-pre.4"
hex-literal = "0.4.1"
chrono = "0.4.38"
rand = "0.8.5"
53 changes: 53 additions & 0 deletions server/src/handlers/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

/// Cursor.
#[derive(Clone)]
pub struct Cursor {
/// Base.
pub base: String,
}

impl Cursor {
/// As string.
pub fn as_string(self) -> String {
self.base
}
}

#[cfg(test)]
mod tests {

use crate::handlers::cursor::Cursor;
use anyhow::Result;
use hamcrest::{equal_to, is, HamcrestMatcher};

#[test]
fn prints_current() -> Result<()> {
let expected = String::from("test");
let cursor = Cursor {
base: expected.clone(),
};
assert_that!(cursor.as_string(), is(equal_to(expected)));
Ok(())
}
}
4 changes: 4 additions & 0 deletions server/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
// 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.
/// Cursor.
pub mod cursor;
/// Home handler.
pub mod home;
/// Handler for internal user registration.
pub mod register_user;
/// Slashed Cursor.
pub mod sh_cursor;
6 changes: 3 additions & 3 deletions server/src/handlers/register_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ pub async fn register_user(
State(config): State<ServerConfig>,
Json(payload): Json<User>,
) -> Result<StatusCode, String> {
let newcomer = User::new(payload.username.clone());
let mut newcomer = User::new(payload.login.clone());
let fakehub = config.fakehub;
let github = fakehub.main();
match newcomer.register_in(&mut github.clone()) {
Ok(_) => {
info!("New user is here. Hello @{}", newcomer.username);
info!("New user is here. Hello @{}", newcomer.login);
Ok(StatusCode::CREATED)
}
Err(e) => Err(format!("Can't register user @{}: {}", newcomer.username, e)),
Err(e) => Err(format!("Can't register user @{}: {}", newcomer.login, e)),
}
}

Expand Down
77 changes: 77 additions & 0 deletions server/src/handlers/sh_cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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::handlers::cursor::Cursor;

/// Slashed cursor with complete value.
/// ```
/// use hamcrest::assert_that;
/// use hamcrest::{equal_to, is, HamcrestMatcher};
/// use server::handlers::cursor::Cursor;
/// use server::handlers::sh_cursor::ShCursor;
///
/// let url = ShCursor::new(
/// Cursor {base: String::from("http://localhost:3000") },
/// String::from("repos")
/// ).as_string();
/// assert_that!(url, is(equal_to(String::from("http://localhost:3000/repos"))));
/// ```
pub struct ShCursor {
origin: Cursor,
complete: String,
}

impl ShCursor {
/// New.
pub fn new(origin: Cursor, complete: String) -> ShCursor {
ShCursor { origin, complete }
}

/// Format as string.
pub fn as_string(self) -> String {
format!("{}/{}", self.origin.as_string(), self.complete)
}
}

#[cfg(test)]
mod tests {

use crate::handlers::cursor::Cursor;
use crate::handlers::sh_cursor::ShCursor;
use anyhow::Result;
use hamcrest::{equal_to, is, HamcrestMatcher};

#[test]
fn prints_formatted() -> Result<()> {
let url = ShCursor::new(
Cursor {
base: String::from("http://localhost:3000"),
},
String::from("repos"),
)
.as_string();
assert_that!(
url,
is(equal_to(String::from("http://localhost:3000/repos")))
);
Ok(())
}
}
4 changes: 2 additions & 2 deletions server/src/objects/fakehub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use uuid::Uuid;
/// let fakehub = FakeHub::default();
/// let github = fakehub.main();
/// let jeff = github.user("jeff").expect("Failed to get user");
/// assert_that!(&jeff.username, is(equal_to("jeff")));
/// assert_that!(&jeff.login, is(equal_to("jeff")));
/// ```
#[derive(Clone)]
pub struct FakeHub {
Expand Down Expand Up @@ -91,7 +91,7 @@ mod tests {
let users = github.clone().users();
let user = users.first().expect("Failed to get user");
assert_that!(&github.name, is(equal_to("main")));
assert_that!(&user.username, is(equal_to("jeff")));
assert_that!(&user.login, is(equal_to("jeff")));
Ok(())
}
}
6 changes: 3 additions & 3 deletions server/src/objects/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl GitHub {
/// Add user to GitHub.
/// `user` User
pub fn add_user(&mut self, user: User) {
self.users.insert(user.clone().username, user);
self.users.insert(user.clone().login, user);
}

/// User.
Expand Down Expand Up @@ -72,7 +72,7 @@ mod tests {
github.add_user(User::new(expected.clone()));
let users = github.users();
let user = users.first().expect("Failed to get user");
assert_that!(&user.username, is(equal_to(&expected)));
assert_that!(&user.login, is(equal_to(&expected)));
Ok(())
}

Expand All @@ -86,7 +86,7 @@ mod tests {
let expected = "foo";
github.add_user(User::new(String::from(expected)));
let foo = github.user(expected).expect("Failed to get user");
assert_that!(&foo.username, is(equal_to(expected)));
assert_that!(&foo.login, is(equal_to(expected)));
Ok(())
}
}
70 changes: 70 additions & 0 deletions server/src/objects/json/json_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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::user::User;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// JSON User.
#[derive(Serialize, Deserialize)]
pub struct JsonUser {
origin: User,
}

impl JsonUser {
/// New.
pub fn new(origin: User) -> JsonUser {
JsonUser { origin }
}

/// Print it as JSON document.
pub fn as_json(self) -> Value {
let mut map = Map::new();
map.insert(String::from("login"), Value::from(self.origin.login));
for (k, v) in self.origin.extra.into_iter() {
map.insert(k, v);
}
Value::Object(map)
}
}

#[cfg(test)]
mod tests {
use crate::objects::json::json_user::JsonUser;
use crate::objects::user::User;
use anyhow::Result;
use hamcrest::{equal_to, is, HamcrestMatcher};
use serde_json::Value;

#[test]
fn prints_json() -> Result<()> {
let jeff = "jeff";
let mut base = User::new(String::from(jeff));
let url = "testing";
base.extra
.insert(String::from("url"), Value::String(String::from(url)));
let user = JsonUser::new(base);
let value = user.as_json();
assert_that!(value["login"].as_str(), is(equal_to(Some(jeff))));
assert_that!(value["url"].as_str(), is(equal_to(Some(url))));
Ok(())
}
}
23 changes: 23 additions & 0 deletions server/src/objects/json/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.
/// JSON User.
pub mod json_user;
2 changes: 2 additions & 0 deletions server/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
pub mod fakehub;
/// GitHub.
pub mod github;
/// JSON objects.
pub mod json;
/// GitHub repository.
pub mod repo;
/// GitHub user.
Expand Down
Loading

2 comments on commit 5551f71

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 5551f71 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 137-e669be66 discovered in server/src/objects/user.rs) and submitted as #141. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 5551f71 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 137-66b3c00b discovered in server/src/objects/user.rs) and submitted as #142. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.