Server: avoids big if let blocks

pull/300/head
Tpt 2 years ago committed by Thomas Tanon
parent 74c565a690
commit 38fdffc147
  1. 47
      server/src/main.rs

@ -244,7 +244,8 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
configure_and_evaluate_sparql_query(store, &[url_query(request)], None, request) configure_and_evaluate_sparql_query(store, &[url_query(request)], None, request)
} }
("/query", "POST") => { ("/query", "POST") => {
if let Some(content_type) = content_type(request) { let content_type =
content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if content_type == "application/sparql-query" { if content_type == "application/sparql-query" {
let mut buffer = String::new(); let mut buffer = String::new();
request request
@ -274,12 +275,10 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
} else { } else {
Err(unsupported_media_type(&content_type)) Err(unsupported_media_type(&content_type))
} }
} else {
Err(bad_request("No Content-Type given"))
}
} }
("/update", "POST") => { ("/update", "POST") => {
if let Some(content_type) = content_type(request) { let content_type =
content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if content_type == "application/sparql-update" { if content_type == "application/sparql-update" {
let mut buffer = String::new(); let mut buffer = String::new();
request request
@ -309,9 +308,6 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
} else { } else {
return Err(unsupported_media_type(&content_type)); return Err(unsupported_media_type(&content_type));
} }
} else {
Err(bad_request("No Content-Type given"))
}
} }
(path, "GET") if path.starts_with("/store") => { (path, "GET") if path.starts_with("/store") => {
if let Some(target) = store_target(request)? { if let Some(target) = store_target(request)? {
@ -364,9 +360,11 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
} }
} }
(path, "PUT") if path.starts_with("/store") => { (path, "PUT") if path.starts_with("/store") => {
if let Some(content_type) = content_type(request) { let content_type =
content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if let Some(target) = store_target(request)? { if let Some(target) = store_target(request)? {
if let Some(format) = GraphFormat::from_media_type(&content_type) { let format = GraphFormat::from_media_type(&content_type)
.ok_or_else(|| unsupported_media_type(&content_type))?;
let new = !match &target { let new = !match &target {
NamedGraphName::NamedNode(target) => { NamedGraphName::NamedNode(target) => {
if store if store
@ -404,19 +402,13 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
}) })
.build()) .build())
} else { } else {
Err(unsupported_media_type(&content_type)) let format = DatasetFormat::from_media_type(&content_type)
} .ok_or_else(|| unsupported_media_type(&content_type))?;
} else if let Some(format) = DatasetFormat::from_media_type(&content_type) {
store.clear().map_err(internal_server_error)?; store.clear().map_err(internal_server_error)?;
store store
.load_dataset(BufReader::new(request.body_mut()), format, None) .load_dataset(BufReader::new(request.body_mut()), format, None)
.map_err(internal_server_error)?; .map_err(internal_server_error)?;
Ok(Response::builder(Status::NO_CONTENT).build()) Ok(Response::builder(Status::NO_CONTENT).build())
} else {
Err(unsupported_media_type(&content_type))
}
} else {
Err(bad_request("No Content-Type given"))
} }
} }
(path, "DELETE") if path.starts_with("/store") => { (path, "DELETE") if path.starts_with("/store") => {
@ -447,7 +439,8 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
Ok(Response::builder(Status::NO_CONTENT).build()) Ok(Response::builder(Status::NO_CONTENT).build())
} }
(path, "POST") if path.starts_with("/store") => { (path, "POST") if path.starts_with("/store") => {
if let Some(content_type) = content_type(request) { let content_type =
content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if let Some(target) = store_target(request)? { if let Some(target) = store_target(request)? {
if let Some(format) = GraphFormat::from_media_type(&content_type) { if let Some(format) = GraphFormat::from_media_type(&content_type) {
let new = assert_that_graph_exists(&store, &target).is_ok(); let new = assert_that_graph_exists(&store, &target).is_ok();
@ -474,8 +467,7 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
.map_err(bad_request)?; .map_err(bad_request)?;
Ok(Response::builder(Status::NO_CONTENT).build()) Ok(Response::builder(Status::NO_CONTENT).build())
} else if let Some(format) = GraphFormat::from_media_type(&content_type) { } else if let Some(format) = GraphFormat::from_media_type(&content_type) {
let graph = let graph = resolve_with_base(request, &format!("/store/{:x}", random::<u128>()))?;
resolve_with_base(request, &format!("/store/{:x}", random::<u128>()))?;
store store
.load_graph(BufReader::new(request.body_mut()), format, &graph, None) .load_graph(BufReader::new(request.body_mut()), format, &graph, None)
.map_err(bad_request)?; .map_err(bad_request)?;
@ -486,9 +478,6 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
} else { } else {
Err(unsupported_media_type(&content_type)) Err(unsupported_media_type(&content_type))
} }
} else {
Err(bad_request("No Content-Type given"))
}
} }
(path, "HEAD") if path.starts_with("/store") => { (path, "HEAD") if path.starts_with("/store") => {
if let Some(target) = store_target(request)? { if let Some(target) = store_target(request)? {
@ -553,7 +542,7 @@ fn configure_and_evaluate_sparql_query(
} }
} }
} }
if let Some(query) = query { let query = query.ok_or_else(|| bad_request("You should set the 'query' parameter"))?;
evaluate_sparql_query( evaluate_sparql_query(
store, store,
query, query,
@ -562,9 +551,6 @@ fn configure_and_evaluate_sparql_query(
named_graph_uris, named_graph_uris,
request, request,
) )
} else {
Err(bad_request("You should set the 'query' parameter"))
}
} }
fn evaluate_sparql_query( fn evaluate_sparql_query(
@ -685,7 +671,7 @@ fn configure_and_evaluate_sparql_update(
} }
} }
} }
if let Some(update) = update { let update = update.ok_or_else(|| bad_request("You should set the 'update' parameter"))?;
evaluate_sparql_update( evaluate_sparql_update(
store, store,
update, update,
@ -694,9 +680,6 @@ fn configure_and_evaluate_sparql_update(
named_graph_uris, named_graph_uris,
request, request,
) )
} else {
Err(bad_request("You should set the 'update' parameter"))
}
} }
fn evaluate_sparql_update( fn evaluate_sparql_update(

Loading…
Cancel
Save