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

Loading…
Cancel
Save