Skip to content

Commit

Permalink
refactor: ergonomic conversions (#5)
Browse files Browse the repository at this point in the history
### Description
This PR suggests the change of the methods for conversion to leverage
the [From](https://doc.rust-lang.org/std/convert/trait.From.html) \
[TryFrom](https://doc.rust-lang.org/std/convert/trait.TryFrom.html)
traits.

### Other changes (I can revert specific ones if don't agree)
- Rename `input_yaml_conf` to `input_json_conf` since it seems like the
default now is to use JSON.
- Change the responsibility of who needs to verify the structural
validity of the input (JSON/YAML). With the current proposal, the
Session is constructed from a format validated value
(`serde_json::Value`/`serde_yaml::Value`) and the caller is the one
responsible for handling invalid format.
  • Loading branch information
augustoccesar authored May 10, 2023
1 parent b1db98d commit 6eeca3d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 175 deletions.
6 changes: 3 additions & 3 deletions linkup-cli/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ async fn linkup_config_handler(
) -> impl Responder {
let sessions = SessionAllocator::new(string_store.into_inner());

let input_yaml_conf = match String::from_utf8(req_body.to_vec()) {
Ok(input_yaml_conf) => input_yaml_conf,
let input_json_conf = match String::from_utf8(req_body.to_vec()) {
Ok(input_json_conf) => input_json_conf,
Err(_) => return HttpResponse::BadRequest().body("Invalid request body encoding"),
};

match update_session_req_from_json(input_yaml_conf) {
match update_session_req_from_json(input_json_conf) {
Ok((desired_name, server_conf)) => {
let session_name = sessions
.store_session(server_conf, NameKind::Animal, desired_name)
Expand Down
6 changes: 4 additions & 2 deletions linkup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ mod tests {
async fn test_get_request_session_by_subdomain() {
let sessions = SessionAllocator::new(Arc::new(MemoryStringStore::new()));

let config = session_from_yml(String::from(CONF_STR)).unwrap();
let config_value: serde_yaml::Value = serde_yaml::from_str(CONF_STR).unwrap();
let config: Session = config_value.try_into().unwrap();

let name = sessions
.store_session(config, NameKind::Animal, "".to_string())
Expand Down Expand Up @@ -372,7 +373,8 @@ mod tests {
async fn test_get_target_url() {
let sessions = SessionAllocator::new(Arc::new(MemoryStringStore::new()));

let input_config = session_from_yml(String::from(CONF_STR)).unwrap();
let input_config_value: serde_yaml::Value = serde_yaml::from_str(CONF_STR).unwrap();
let input_config: Session = input_config_value.try_into().unwrap();

let name = sessions
.store_session(input_config, NameKind::Animal, "".to_string())
Expand Down
Loading

0 comments on commit 6eeca3d

Please sign in to comment.