mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
s/str::to_string/str::to_owned
This commit is contained in:
parent
9a5cd90634
commit
8fd4262136
@ -124,7 +124,7 @@ mod test {
|
||||
RedisActorSessionStore::new("127.0.0.1:6379"),
|
||||
private_key.clone(),
|
||||
)
|
||||
.cookie_name("test-session".to_string())
|
||||
.cookie_name("test-session".to_owned())
|
||||
.build(),
|
||||
)
|
||||
.wrap(middleware::Logger::default())
|
||||
|
@ -45,7 +45,7 @@ impl From<DBError> for ServiceError {
|
||||
match error {
|
||||
DBError::DatabaseError(kind, info) => {
|
||||
if let DatabaseErrorKind::UniqueViolation = kind {
|
||||
let message = info.details().unwrap_or_else(|| info.message()).to_string();
|
||||
let message = info.details().unwrap_or_else(|| info.message()).to_owned();
|
||||
return ServiceError::BadRequest(message);
|
||||
}
|
||||
ServiceError::InternalServerError
|
||||
|
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let pool: models::Pool = r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Failed to create pool.");
|
||||
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
|
||||
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_owned());
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
|
@ -21,7 +21,7 @@ fn app_config(config: &mut web::ServiceConfig) {
|
||||
config.service(
|
||||
web::scope("")
|
||||
.app_data(web::Data::new(AppState {
|
||||
foo: "bar".to_string(),
|
||||
foo: "bar".to_owned(),
|
||||
}))
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
||||
@ -96,7 +96,7 @@ mod tests {
|
||||
#[actix_web::test]
|
||||
async fn handle_post_1_unit_test() {
|
||||
let params = Form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
});
|
||||
let resp = handle_post_1(params).await.unwrap();
|
||||
|
||||
@ -116,7 +116,7 @@ mod tests {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/post1")
|
||||
.set_form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
})
|
||||
.to_request();
|
||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||
@ -134,12 +134,12 @@ mod tests {
|
||||
async fn handle_post_2_unit_test() {
|
||||
let state = TestRequest::default()
|
||||
.data(AppState {
|
||||
foo: "bar".to_string(),
|
||||
foo: "bar".to_owned(),
|
||||
})
|
||||
.to_http_request();
|
||||
let data = state.app_data::<actix_web::web::Data<AppState>>().unwrap();
|
||||
let params = Form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
});
|
||||
let resp = handle_post_2(data.clone(), params).await.unwrap();
|
||||
|
||||
@ -161,7 +161,7 @@ mod tests {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/post2")
|
||||
.set_form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
})
|
||||
.to_request();
|
||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||
@ -183,7 +183,7 @@ mod tests {
|
||||
async fn handle_post_3_unit_test() {
|
||||
let req = TestRequest::default().to_http_request();
|
||||
let params = Form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
});
|
||||
let result = handle_post_3(req.clone(), params).await;
|
||||
let resp = result.respond_to(&req);
|
||||
@ -205,7 +205,7 @@ mod tests {
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/post3")
|
||||
.set_form(MyParams {
|
||||
name: "John".to_string(),
|
||||
name: "John".to_owned(),
|
||||
})
|
||||
.to_request();
|
||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||
|
@ -14,7 +14,7 @@ pub async fn gen_tls_cert(user_email: &str, user_domain: &str) -> anyhow::Result
|
||||
// Create acme-challenge dir.
|
||||
fs::create_dir("./acme-challenge").unwrap();
|
||||
|
||||
let domain = user_domain.to_string();
|
||||
let domain = user_domain.to_owned();
|
||||
|
||||
// Create temporary Actix Web server for ACME challenge.
|
||||
let srv = HttpServer::new(|| {
|
||||
|
@ -31,7 +31,7 @@ impl ResponseError for Error {
|
||||
|
||||
async fn index() -> Result<HttpResponse, Error> {
|
||||
Err(Error {
|
||||
msg: "an example error message".to_string(),
|
||||
msg: "an example error message".to_owned(),
|
||||
status: 400,
|
||||
})
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ use std::io;
|
||||
|
||||
use actix_web::{
|
||||
error::ErrorBadRequest,
|
||||
http::header::ContentType,
|
||||
web::{self, BytesMut},
|
||||
App, Error, HttpResponse, HttpServer,
|
||||
};
|
||||
@ -56,7 +55,7 @@ async fn step_x(data: SomeData, client: &Client) -> actix_web::Result<SomeData>
|
||||
body.extend_from_slice(&chunk?);
|
||||
}
|
||||
|
||||
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
||||
let body = serde_json::from_slice::<HttpBinResponse>(&body).unwrap();
|
||||
|
||||
println!("{body:?}");
|
||||
|
||||
@ -71,9 +70,7 @@ async fn create_something(
|
||||
let some_data_3 = step_x(some_data_2, &client).await?;
|
||||
let d = step_x(some_data_3, &client).await?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&d).unwrap()))
|
||||
Ok(HttpResponse::Ok().json(d))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
@ -30,7 +30,7 @@ async fn main() -> io::Result<()> {
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
Html(include_str!("index.html").to_string())
|
||||
Html(include_str!("index.html").to_owned())
|
||||
}
|
||||
|
||||
#[get("/events")]
|
||||
|
@ -77,10 +77,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse<
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
let fallback = |err: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
.body(err.to_string())
|
||||
};
|
||||
|
||||
let hb = request
|
||||
|
@ -38,7 +38,7 @@ async fn page(params: web::Path<(i32,)>) -> actix_web::Result<impl Responder> {
|
||||
|
||||
#[get("/")]
|
||||
async fn hello() -> impl Responder {
|
||||
Html("<p>Hello world!</p>".to_string())
|
||||
Html("<p>Hello world!</p>".to_owned())
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
@ -71,10 +71,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
let fallback = |err: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
.body(err.to_string())
|
||||
};
|
||||
|
||||
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
||||
|
@ -75,10 +75,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
let fallback = |err: &str| {
|
||||
HttpResponse::build(res.status())
|
||||
.content_type(ContentType::plaintext())
|
||||
.body(e.to_string())
|
||||
.body(err.to_string())
|
||||
};
|
||||
|
||||
let tt = request
|
||||
|
@ -76,7 +76,7 @@ impl Handler<JoinRoom> for WsChatServer {
|
||||
let id = self.add_client_to_room(&room_name, None, client);
|
||||
let join_msg = format!(
|
||||
"{} joined {room_name}",
|
||||
client_name.unwrap_or_else(|| "anon".to_string()),
|
||||
client_name.unwrap_or_else(|| "anon".to_owned()),
|
||||
);
|
||||
|
||||
self.send_chat_message(&room_name, &join_msg, id);
|
||||
|
@ -64,7 +64,7 @@ impl WsChatSession {
|
||||
pub fn send_msg(&self, msg: &str) {
|
||||
let content = format!(
|
||||
"{}: {msg}",
|
||||
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
||||
self.name.clone().unwrap_or_else(|| "anon".to_owned()),
|
||||
);
|
||||
|
||||
let msg = SendMessage(self.room.clone(), self.id, content);
|
||||
@ -84,7 +84,7 @@ impl Actor for WsChatSession {
|
||||
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
||||
log::info!(
|
||||
"WsChatSession closed for {}({}) in room {}",
|
||||
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
||||
self.name.clone().unwrap_or_else(|| "anon".to_owned()),
|
||||
self.id,
|
||||
self.room
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user