Skip to content

Commit

Permalink
feat(#17): serde_xml_rs, puzzle for #save
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jul 10, 2024
1 parent b8b9336 commit 5e0c434
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ path = "src/lib.rs"
openapi = { path = "../github-mirror" }
anyhow = "1.0.86"
serde = { version = "1.0.203", features = ["derive"] }
serde-xml-rs = "0.6.0"
serde_json = "1.0.117"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros", "fs"] }
axum = "0.7.5"
Expand Down
36 changes: 33 additions & 3 deletions server/src/objects/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use anyhow::Result;
use log::info;
use serde::Deserialize;
use log::{debug, info};
use serde::{Deserialize, Serialize};
use serde_xml_rs::to_string;

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
pub(crate) username: String,
}
Expand All @@ -34,9 +35,38 @@ impl User {
}
}

// @todo #17:40min Apply XMLed user to the <users/> node in storage.
// We should apply XMLed user to the <users> XML node in storage. First we
// need to check that user with provided name does not exist, and only then
// apply it to the storage. Keep in mind that application function in the
// storage should be thread-safe (as well as #xml function). Don't forget to
// create unit tests that prove that.
impl User {
pub async fn save(self) -> Result<()> {
info!("registering user @{}", self.username);
let xml = to_string(&self).unwrap();
debug!("XMLed user: {}", xml);
Ok(())
}
}

#[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,
"Username {} from user: {:?} does not match with expected {}",
jeff.username,
jeff,
expected
);
Ok(())
}
}

0 comments on commit 5e0c434

Please sign in to comment.