Skip to content

Commit 6bab4ad

Browse files
authored
apply new formatting everywhere (leptos-rs#502)
1 parent d4648da commit 6bab4ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5142
-4259
lines changed

integrations/actix/src/lib.rs

+110-49
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ pub struct ResponseParts {
3232

3333
impl ResponseParts {
3434
/// Insert a header, overwriting any previous value with the same key
35-
pub fn insert_header(&mut self, key: header::HeaderName, value: header::HeaderValue) {
35+
pub fn insert_header(
36+
&mut self,
37+
key: header::HeaderName,
38+
value: header::HeaderValue,
39+
) {
3640
self.headers.insert(key, value);
3741
}
3842
/// Append a header, leaving any header with the same key intact
39-
pub fn append_header(&mut self, key: header::HeaderName, value: header::HeaderValue) {
43+
pub fn append_header(
44+
&mut self,
45+
key: header::HeaderName,
46+
value: header::HeaderValue,
47+
) {
4048
self.headers.append(key, value);
4149
}
4250
}
@@ -60,13 +68,21 @@ impl ResponseOptions {
6068
res_parts.status = Some(status);
6169
}
6270
/// Insert a header, overwriting any previous value with the same key
63-
pub fn insert_header(&self, key: header::HeaderName, value: header::HeaderValue) {
71+
pub fn insert_header(
72+
&self,
73+
key: header::HeaderName,
74+
value: header::HeaderValue,
75+
) {
6476
let mut writeable = self.0.write();
6577
let res_parts = &mut *writeable;
6678
res_parts.headers.insert(key, value);
6779
}
6880
/// Append a header, leaving any header with the same key intact
69-
pub fn append_header(&self, key: header::HeaderName, value: header::HeaderValue) {
81+
pub fn append_header(
82+
&self,
83+
key: header::HeaderName,
84+
value: header::HeaderValue,
85+
) {
7086
let mut writeable = self.0.write();
7187
let res_parts = &mut *writeable;
7288
res_parts.headers.append(key, value);
@@ -81,7 +97,8 @@ pub fn redirect(cx: leptos::Scope, path: &str) {
8197
response_options.set_status(StatusCode::FOUND);
8298
response_options.insert_header(
8399
header::LOCATION,
84-
header::HeaderValue::from_str(path).expect("Failed to create HeaderValue"),
100+
header::HeaderValue::from_str(path)
101+
.expect("Failed to create HeaderValue"),
85102
);
86103
}
87104

@@ -173,7 +190,8 @@ pub fn handle_server_fns_with_context(
173190

174191
match server_fn(cx, body).await {
175192
Ok(serialized) => {
176-
let res_options = use_context::<ResponseOptions>(cx).unwrap();
193+
let res_options =
194+
use_context::<ResponseOptions>(cx).unwrap();
177195

178196
// clean up the scope, which we only needed to run the server fn
179197
disposer.dispose();
@@ -183,7 +201,8 @@ pub fn handle_server_fns_with_context(
183201
let mut res_parts = res_options.0.write();
184202

185203
if accept_header == Some("application/json")
186-
|| accept_header == Some("application/x-www-form-urlencoded")
204+
|| accept_header
205+
== Some("application/x-www-form-urlencoded")
187206
|| accept_header == Some("application/cbor")
188207
{
189208
res = HttpResponse::Ok();
@@ -221,7 +240,9 @@ pub fn handle_server_fns_with_context(
221240
res.body(Bytes::from(data))
222241
}
223242
Payload::Url(data) => {
224-
res.content_type("application/x-www-form-urlencoded");
243+
res.content_type(
244+
"application/x-www-form-urlencoded",
245+
);
225246
res.body(data)
226247
}
227248
Payload::Json(data) => {
@@ -230,13 +251,15 @@ pub fn handle_server_fns_with_context(
230251
}
231252
}
232253
}
233-
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
254+
Err(e) => HttpResponse::InternalServerError()
255+
.body(e.to_string()),
234256
}
235257
} else {
236258
HttpResponse::BadRequest().body(format!(
237259
"Could not find a server function at the route {:?}. \
238-
\n\nIt's likely that you need to call ServerFn::register() on the \
239-
server function type, somewhere in your `main` function.",
260+
\n\nIt's likely that you need to call \
261+
ServerFn::register() on the server function type, \
262+
somewhere in your `main` function.",
240263
req.path()
241264
))
242265
}
@@ -256,13 +279,13 @@ pub fn handle_server_fns_with_context(
256279
///
257280
/// This can then be set up at an appropriate route in your application:
258281
/// ```
259-
/// use actix_web::{HttpServer, App};
282+
/// use actix_web::{App, HttpServer};
260283
/// use leptos::*;
261-
/// use std::{env,net::SocketAddr};
284+
/// use std::{env, net::SocketAddr};
262285
///
263286
/// #[component]
264287
/// fn MyApp(cx: Scope) -> impl IntoView {
265-
/// view! { cx, <main>"Hello, world!"</main> }
288+
/// view! { cx, <main>"Hello, world!"</main> }
266289
/// }
267290
///
268291
/// # if false { // don't actually try to run a server in a doctest...
@@ -272,11 +295,17 @@ pub fn handle_server_fns_with_context(
272295
/// let addr = conf.leptos_options.site_addr.clone();
273296
/// HttpServer::new(move || {
274297
/// let leptos_options = &conf.leptos_options;
275-
///
298+
///
276299
/// App::new()
277300
/// // {tail:.*} passes the remainder of the URL as the route
278301
/// // the actual routing will be handled by `leptos_router`
279-
/// .route("/{tail:.*}", leptos_actix::render_app_to_stream(leptos_options.to_owned(), |cx| view! { cx, <MyApp/> }))
302+
/// .route(
303+
/// "/{tail:.*}",
304+
/// leptos_actix::render_app_to_stream(
305+
/// leptos_options.to_owned(),
306+
/// |cx| view! { cx, <MyApp/> },
307+
/// ),
308+
/// )
280309
/// })
281310
/// .bind(&addr)?
282311
/// .run()
@@ -353,14 +382,14 @@ where
353382
///
354383
/// This can then be set up at an appropriate route in your application:
355384
/// ```
356-
/// use actix_web::{HttpServer, App};
385+
/// use actix_web::{App, HttpServer};
357386
/// use leptos::*;
358-
/// use std::{env,net::SocketAddr};
359387
/// use leptos_actix::DataResponse;
388+
/// use std::{env, net::SocketAddr};
360389
///
361390
/// #[component]
362391
/// fn MyApp(cx: Scope, data: &'static str) -> impl IntoView {
363-
/// view! { cx, <main>"Hello, world!"</main> }
392+
/// view! { cx, <main>"Hello, world!"</main> }
364393
/// }
365394
///
366395
/// # if false { // don't actually try to run a server in a doctest...
@@ -370,14 +399,21 @@ where
370399
/// let addr = conf.leptos_options.site_addr.clone();
371400
/// HttpServer::new(move || {
372401
/// let leptos_options = &conf.leptos_options;
373-
///
402+
///
374403
/// App::new()
375404
/// // {tail:.*} passes the remainder of the URL as the route
376405
/// // the actual routing will be handled by `leptos_router`
377-
/// .route("/{tail:.*}", leptos_actix::render_preloaded_data_app(
378-
/// leptos_options.to_owned(),
379-
/// |req| async move { Ok(DataResponse::Data("async func that can preload data")) },
380-
/// |cx, data| view! { cx, <MyApp data/> })
406+
/// .route(
407+
/// "/{tail:.*}",
408+
/// leptos_actix::render_preloaded_data_app(
409+
/// leptos_options.to_owned(),
410+
/// |req| async move {
411+
/// Ok(DataResponse::Data(
412+
/// "async func that can preload data",
413+
/// ))
414+
/// },
415+
/// |cx, data| view! { cx, <MyApp data/> },
416+
/// ),
381417
/// )
382418
/// })
383419
/// .bind(&addr)?
@@ -430,7 +466,11 @@ where
430466
})
431467
}
432468

433-
fn provide_contexts(cx: leptos::Scope, req: &HttpRequest, res_options: ResponseOptions) {
469+
fn provide_contexts(
470+
cx: leptos::Scope,
471+
req: &HttpRequest,
472+
res_options: ResponseOptions,
473+
) {
434474
let path = leptos_corrected_path(req);
435475

436476
let integration = ServerIntegration { path };
@@ -457,25 +497,27 @@ async fn stream_app(
457497
res_options: ResponseOptions,
458498
additional_context: impl Fn(leptos::Scope) + 'static + Clone + Send,
459499
) -> HttpResponse<BoxBody> {
460-
let (stream, runtime, scope) = render_to_stream_with_prefix_undisposed_with_context(
461-
app,
462-
move |cx| {
463-
let meta = use_context::<MetaContext>(cx);
464-
let head = meta
465-
.as_ref()
466-
.map(|meta| meta.dehydrate())
467-
.unwrap_or_default();
468-
let body_meta = meta
469-
.as_ref()
470-
.and_then(|meta| meta.body.as_string())
471-
.unwrap_or_default();
472-
format!("{head}</head><body{body_meta}>").into()
473-
},
474-
additional_context,
475-
);
500+
let (stream, runtime, scope) =
501+
render_to_stream_with_prefix_undisposed_with_context(
502+
app,
503+
move |cx| {
504+
let meta = use_context::<MetaContext>(cx);
505+
let head = meta
506+
.as_ref()
507+
.map(|meta| meta.dehydrate())
508+
.unwrap_or_default();
509+
let body_meta = meta
510+
.as_ref()
511+
.and_then(|meta| meta.body.as_string())
512+
.unwrap_or_default();
513+
format!("{head}</head><body{body_meta}>").into()
514+
},
515+
additional_context,
516+
);
476517

477518
let cx = leptos::Scope { runtime, id: scope };
478-
let (head, tail) = html_parts(options, use_context::<MetaContext>(cx).as_ref());
519+
let (head, tail) =
520+
html_parts(options, use_context::<MetaContext>(cx).as_ref());
479521

480522
let mut stream = Box::pin(
481523
futures::stream::once(async move { head.clone() })
@@ -493,11 +535,13 @@ async fn stream_app(
493535

494536
let res_options = res_options.0.read();
495537

496-
let (status, mut headers) = (res_options.status, res_options.headers.clone());
538+
let (status, mut headers) =
539+
(res_options.status, res_options.headers.clone());
497540
let status = status.unwrap_or_default();
498541

499542
let complete_stream =
500-
futures::stream::iter([first_chunk.unwrap(), second_chunk.unwrap()]).chain(stream);
543+
futures::stream::iter([first_chunk.unwrap(), second_chunk.unwrap()])
544+
.chain(stream);
501545
let mut res = HttpResponse::Ok()
502546
.content_type("text/html")
503547
.streaming(complete_stream);
@@ -514,7 +558,10 @@ async fn stream_app(
514558
res
515559
}
516560

517-
fn html_parts(options: &LeptosOptions, meta_context: Option<&MetaContext>) -> (String, String) {
561+
fn html_parts(
562+
options: &LeptosOptions,
563+
meta_context: Option<&MetaContext>,
564+
) -> (String, String) {
518565
// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
519566
// we add _bg to the wasm files if cargo-leptos doesn't set the env var LEPTOS_OUTPUT_NAME
520567
// Otherwise we need to add _bg because wasm_pack always does. This is not the same as options.output_name, which is set regardless
@@ -578,7 +625,9 @@ fn html_parts(options: &LeptosOptions, meta_context: Option<&MetaContext>) -> (S
578625
/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
579626
/// create routes in Actix's App without having to use wildcard matching or fallbacks. Takes in your root app Element
580627
/// as an argument so it can walk you app tree. This version is tailored to generated Actix compatible paths.
581-
pub fn generate_route_list<IV>(app_fn: impl FnOnce(leptos::Scope) -> IV + 'static) -> Vec<String>
628+
pub fn generate_route_list<IV>(
629+
app_fn: impl FnOnce(leptos::Scope) -> IV + 'static,
630+
) -> Vec<String>
582631
where
583632
IV: IntoView + 'static,
584633
{
@@ -658,7 +707,12 @@ pub trait LeptosRoutes {
658707
/// to those paths to Leptos's renderer.
659708
impl<T> LeptosRoutes for actix_web::App<T>
660709
where
661-
T: ServiceFactory<ServiceRequest, Config = (), Error = Error, InitError = ()>,
710+
T: ServiceFactory<
711+
ServiceRequest,
712+
Config = (),
713+
Error = Error,
714+
InitError = (),
715+
>,
662716
{
663717
fn leptos_routes<IV>(
664718
self,
@@ -671,7 +725,10 @@ where
671725
{
672726
let mut router = self;
673727
for path in paths.iter() {
674-
router = router.route(path, render_app_to_stream(options.clone(), app_fn.clone()));
728+
router = router.route(
729+
path,
730+
render_app_to_stream(options.clone(), app_fn.clone()),
731+
);
675732
}
676733
router
677734
}
@@ -693,7 +750,11 @@ where
693750
for path in paths.iter() {
694751
router = router.route(
695752
path,
696-
render_preloaded_data_app(options.clone(), data_fn.clone(), app_fn.clone()),
753+
render_preloaded_data_app(
754+
options.clone(),
755+
data_fn.clone(),
756+
app_fn.clone(),
757+
),
697758
);
698759
}
699760
router

0 commit comments

Comments
 (0)