From 74c565a69088cfe0af277b0af830a0eb9959a243 Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 25 Nov 2022 22:03:33 +0100 Subject: [PATCH] Server: avoids some duplicated code --- server/src/main.rs | 63 ++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index e5eb025b..c87ee5f0 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -315,17 +315,7 @@ fn handle_request(request: &mut Request, store: Store) -> Result { if let Some(target) = store_target(request)? { - if !match &target { - NamedGraphName::DefaultGraph => true, - NamedGraphName::NamedNode(target) => store - .contains_named_graph(target) - .map_err(internal_server_error)?, - } { - return Err(( - Status::NOT_FOUND, - format!("The graph {} does not exists", GraphName::from(target)), - )); - } + assert_that_graph_exists(&store, &target)?; let format = graph_content_negotiation(request)?; let triples = store.quads_for_pattern( None, @@ -460,12 +450,7 @@ fn handle_request(request: &mut Request, store: Store) -> Result store - .contains_named_graph(target) - .map_err(internal_server_error)?, - NamedGraphName::DefaultGraph => true, - }; + let new = assert_that_graph_exists(&store, &target).is_ok(); store .load_graph( BufReader::new(request.body_mut()), @@ -507,17 +492,7 @@ fn handle_request(request: &mut Request, store: Store) -> Result { if let Some(target) = store_target(request)? { - if !match &target { - NamedGraphName::DefaultGraph => true, - NamedGraphName::NamedNode(target) => store - .contains_named_graph(target) - .map_err(internal_server_error)?, - } { - return Err(( - Status::NOT_FOUND, - format!("The graph {} does not exists", GraphName::from(target)), - )); - } + assert_that_graph_exists(&store, &target)?; } Ok(Response::builder(Status::OK).build()) } @@ -532,19 +507,16 @@ fn handle_request(request: &mut Request, store: Store) -> Result Result { +fn base_url(request: &Request) -> String { let mut url = request.url().clone(); - if let Some(host) = request.url().host_str() { - url.set_host(Some(host)).map_err(bad_request)?; - } url.set_query(None); url.set_fragment(None); - Ok(url.into()) + url.into() } fn resolve_with_base(request: &Request, url: &str) -> Result { Ok(NamedNode::new_unchecked( - Iri::parse(base_url(request)?) + Iri::parse(base_url(request)) .map_err(bad_request)? .resolve(url) .map_err(bad_request)? @@ -603,7 +575,7 @@ fn evaluate_sparql_query( named_graph_uris: Vec, request: &Request, ) -> Result { - let mut query = Query::parse(&query, Some(&base_url(request)?)).map_err(bad_request)?; + let mut query = Query::parse(&query, Some(&base_url(request))).map_err(bad_request)?; if use_default_graph_as_union { if !default_graph_uris.is_empty() || !named_graph_uris.is_empty() { @@ -736,7 +708,7 @@ fn evaluate_sparql_update( request: &Request, ) -> Result { let mut update = - Update::parse(&update, Some(base_url(request)?.as_str())).map_err(bad_request)?; + Update::parse(&update, Some(base_url(request).as_str())).map_err(bad_request)?; if use_default_graph_as_union { if !default_graph_uris.is_empty() || !named_graph_uris.is_empty() { @@ -812,6 +784,25 @@ fn store_target(request: &Request) -> Result, HttpError> } } +fn assert_that_graph_exists(store: &Store, target: &NamedGraphName) -> Result<(), HttpError> { + if match target { + NamedGraphName::DefaultGraph => true, + NamedGraphName::NamedNode(target) => store + .contains_named_graph(target) + .map_err(internal_server_error)?, + } { + Ok(()) + } else { + Err(( + Status::NOT_FOUND, + format!( + "The graph {} does not exists", + GraphName::from(target.clone()) + ), + )) + } +} + #[derive(Eq, PartialEq, Debug, Clone, Hash)] enum NamedGraphName { NamedNode(NamedNode),