1
0
mirror of https://github.com/actix/examples synced 2025-02-25 18:42:50 +01:00

chore: update derive_more to v2

This commit is contained in:
Rob Ede 2025-02-24 04:03:40 +00:00
parent a6df6cc903
commit df0dc549a5
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
15 changed files with 62 additions and 41 deletions

33
Cargo.lock generated
View File

@ -2600,7 +2600,7 @@ dependencies = [
"actix-web", "actix-web",
"confik", "confik",
"deadpool-postgres", "deadpool-postgres",
"derive_more 0.99.19", "derive_more 2.0.1",
"dotenvy", "dotenvy",
"serde", "serde",
"tokio-pg-mapper", "tokio-pg-mapper",
@ -2740,7 +2740,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05"
dependencies = [ dependencies = [
"derive_more-impl", "derive_more-impl 1.0.0",
]
[[package]]
name = "derive_more"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "093242cf7570c207c83073cf82f79706fe7b8317e98620a47d5be7c3d8497678"
dependencies = [
"derive_more-impl 2.0.1",
] ]
[[package]] [[package]]
@ -2755,6 +2764,18 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "derive_more-impl"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.98",
"unicode-xid",
]
[[package]] [[package]]
name = "derive_utils" name = "derive_utils"
version = "0.15.0" version = "0.15.0"
@ -3026,7 +3047,7 @@ name = "error-handling"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"derive_more 0.99.19", "derive_more 2.0.1",
"env_logger", "env_logger",
"log", "log",
"rand 0.9.0", "rand 0.9.0",
@ -5261,7 +5282,7 @@ name = "mysql"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"derive_more 0.99.19", "derive_more 2.0.1",
"dotenvy", "dotenvy",
"env_logger", "env_logger",
"log", "log",
@ -7410,7 +7431,7 @@ dependencies = [
"actix-session", "actix-session",
"actix-web", "actix-web",
"chrono", "chrono",
"derive_more 0.99.19", "derive_more 2.0.1",
"diesel", "diesel",
"dotenvy", "dotenvy",
"env_logger", "env_logger",
@ -8064,7 +8085,7 @@ name = "templating-yarte"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"derive_more 0.99.19", "derive_more 2.0.1",
"env_logger", "env_logger",
"log", "log",
"yarte", "yarte",

View File

@ -98,7 +98,7 @@ actix-ws = "0.3"
awc = "3.2" awc = "3.2"
chrono = { version = "0.4.30", features = ["serde"] } chrono = { version = "0.4.30", features = ["serde"] }
derive_more = "0.99.7" derive_more = "2"
dotenvy = "0.15" dotenvy = "0.15"
env_logger = "0.11" env_logger = "0.11"
eyre = { version = "0.6", default-features = false, features = ["auto-install", "track-caller"] } eyre = { version = "0.6", default-features = false, features = ["auto-install", "track-caller"] }

View File

@ -9,7 +9,7 @@ actix-session = { workspace = true, features = ["cookie-session"] }
actix-web.workspace = true actix-web.workspace = true
chrono.workspace = true chrono.workspace = true
derive_more.workspace = true derive_more = { workspace = true, features = ["display"] }
diesel = { version = "2", features = ["postgres", "r2d2", "uuid", "chrono"] } diesel = { version = "2", features = ["postgres", "r2d2", "uuid", "chrono"] }
dotenvy.workspace = true dotenvy.workspace = true
env_logger.workspace = true env_logger.workspace = true

View File

@ -5,13 +5,13 @@ use uuid::Error as ParseError;
#[derive(Debug, Display)] #[derive(Debug, Display)]
pub enum ServiceError { pub enum ServiceError {
#[display(fmt = "Internal Server Error")] #[display("Internal Server Error")]
InternalServerError, InternalServerError,
#[display(fmt = "BadRequest: {_0}")] #[display("BadRequest: {_0}")]
BadRequest(String), BadRequest(String),
#[display(fmt = "Unauthorized")] #[display("Unauthorized")]
Unauthorized, Unauthorized,
} }

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
derive_more.workspace = true derive_more = { workspace = true, features = ["display"] }
env_logger.workspace = true env_logger.workspace = true
log.workspace = true log.workspace = true
rand.workspace = true rand.workspace = true

View File

@ -13,7 +13,7 @@ http errors will be chosen, each with an equal chance of being selected:
*/ */
use actix_web::{web, App, Error, HttpResponse, HttpServer, ResponseError}; use actix_web::{web, App, Error, HttpResponse, HttpServer, ResponseError};
use derive_more::Display; // naming it clearly for illustration purposes use derive_more::Display;
use rand::{ use rand::{
distr::{Distribution, StandardUniform}, distr::{Distribution, StandardUniform},
Rng, Rng,
@ -21,13 +21,13 @@ use rand::{
#[derive(Debug, Display)] #[derive(Debug, Display)]
pub enum CustomError { pub enum CustomError {
#[display(fmt = "Custom Error 1")] #[display("Custom Error 1")]
CustomOne, CustomOne,
#[display(fmt = "Custom Error 2")] #[display("Custom Error 2")]
CustomTwo, CustomTwo,
#[display(fmt = "Custom Error 3")] #[display("Custom Error 3")]
CustomThree, CustomThree,
#[display(fmt = "Custom Error 4")] #[display("Custom Error 4")]
CustomFour, CustomFour,
} }

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
derive_more.workspace = true derive_more = { workspace = true, features = ["display", "error", "from"] }
dotenvy.workspace = true dotenvy.workspace = true
env_logger.workspace = true env_logger.workspace = true
log.workspace = true log.workspace = true

View File

@ -7,7 +7,7 @@ edition = "2021"
actix-web.workspace = true actix-web.workspace = true
confik = "0.11" confik = "0.11"
deadpool-postgres = { version = "0.12", features = ["serde"] } deadpool-postgres = { version = "0.12", features = ["serde"] }
derive_more.workspace = true derive_more = { workspace = true, features = ["display", "error", "from"] }
dotenvy.workspace = true dotenvy.workspace = true
serde.workspace = true serde.workspace = true
tokio-pg-mapper = "0.2.0" tokio-pg-mapper = "0.2.0"

View File

@ -11,7 +11,7 @@ ignored = [
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
derive_more.workspace = true derive_more = { workspace = true, features = ["display"] }
env_logger.workspace = true env_logger.workspace = true
log.workspace = true log.workspace = true
yarte = { version = "0.15", features = ["bytes-buf", "html-min"] } yarte = { version = "0.15", features = ["bytes-buf", "html-min"] }

View File

@ -5,12 +5,12 @@ use actix::prelude::*;
pub struct ChatMessage(pub String); pub struct ChatMessage(pub String);
#[derive(Clone, Message)] #[derive(Clone, Message)]
#[rtype(result = "usize")] #[rtype(result = "u64")]
pub struct JoinRoom(pub String, pub Option<String>, pub Recipient<ChatMessage>); pub struct JoinRoom(pub String, pub Option<String>, pub Recipient<ChatMessage>);
#[derive(Clone, Message)] #[derive(Clone, Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct LeaveRoom(pub String, pub usize); pub struct LeaveRoom(pub String, pub u64);
#[derive(Clone, Message)] #[derive(Clone, Message)]
#[rtype(result = "Vec<String>")] #[rtype(result = "Vec<String>")]
@ -18,4 +18,4 @@ pub struct ListRooms;
#[derive(Clone, Message)] #[derive(Clone, Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendMessage(pub String, pub usize, pub String); pub struct SendMessage(pub String, pub u64, pub String);

View File

@ -6,7 +6,7 @@ use actix_broker::BrokerSubscribe;
use crate::message::{ChatMessage, JoinRoom, LeaveRoom, ListRooms, SendMessage}; use crate::message::{ChatMessage, JoinRoom, LeaveRoom, ListRooms, SendMessage};
type Client = Recipient<ChatMessage>; type Client = Recipient<ChatMessage>;
type Room = HashMap<usize, Client>; type Room = HashMap<u64, Client>;
#[derive(Default)] #[derive(Default)]
pub struct WsChatServer { pub struct WsChatServer {
@ -20,13 +20,13 @@ impl WsChatServer {
Some(room) Some(room)
} }
fn add_client_to_room(&mut self, room_name: &str, id: Option<usize>, client: Client) -> usize { fn add_client_to_room(&mut self, room_name: &str, id: Option<u64>, client: Client) -> u64 {
let mut id = id.unwrap_or_else(rand::random::<usize>); let mut id = id.unwrap_or_else(rand::random);
if let Some(room) = self.rooms.get_mut(room_name) { if let Some(room) = self.rooms.get_mut(room_name) {
loop { loop {
if room.contains_key(&id) { if room.contains_key(&id) {
id = rand::random::<usize>(); id = rand::random();
} else { } else {
break; break;
} }
@ -45,7 +45,7 @@ impl WsChatServer {
id id
} }
fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: usize) -> Option<()> { fn send_chat_message(&mut self, room_name: &str, msg: &str, _src: u64) -> Option<()> {
let mut room = self.take_room(room_name)?; let mut room = self.take_room(room_name)?;
for (id, client) in room.drain() { for (id, client) in room.drain() {

View File

@ -9,7 +9,7 @@ use crate::{
#[derive(Default)] #[derive(Default)]
pub struct WsChatSession { pub struct WsChatSession {
id: usize, id: u64,
room: String, room: String,
name: Option<String>, name: Option<String>,
} }

View File

@ -40,7 +40,7 @@ async fn chat_route(
struct WsChatSession { struct WsChatSession {
/// unique session id /// unique session id
id: usize, id: u64,
/// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT), /// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
/// otherwise we drop connection. /// otherwise we drop connection.
hb: Instant, hb: Instant,

View File

@ -5,7 +5,7 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use actix::prelude::*; use actix::prelude::*;
use rand::{rngs::ThreadRng, Rng}; use rand::Rng as _;
use crate::session; use crate::session;
@ -13,7 +13,7 @@ use crate::session;
/// ///
/// New chat session is created /// New chat session is created
#[derive(Message)] #[derive(Message)]
#[rtype(usize)] #[rtype(u64)]
pub struct Connect { pub struct Connect {
pub addr: Recipient<session::Message>, pub addr: Recipient<session::Message>,
} }
@ -22,7 +22,7 @@ pub struct Connect {
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct Disconnect { pub struct Disconnect {
pub id: usize, pub id: u64,
} }
/// Send message to specific room /// Send message to specific room
@ -30,7 +30,7 @@ pub struct Disconnect {
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct Message { pub struct Message {
/// Id of the client session /// Id of the client session
pub id: usize, pub id: u64,
/// Peer message /// Peer message
pub msg: String, pub msg: String,
/// Room name /// Room name
@ -49,7 +49,7 @@ impl actix::Message for ListRooms {
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct Join { pub struct Join {
/// Client id /// Client id
pub id: usize, pub id: u64,
/// Room name /// Room name
pub name: String, pub name: String,
} }
@ -57,8 +57,8 @@ pub struct Join {
/// `ChatServer` manages chat rooms and responsible for coordinating chat /// `ChatServer` manages chat rooms and responsible for coordinating chat
/// session. implementation is super primitive /// session. implementation is super primitive
pub struct ChatServer { pub struct ChatServer {
sessions: HashMap<usize, Recipient<session::Message>>, sessions: HashMap<u64, Recipient<session::Message>>,
rooms: HashMap<String, HashSet<usize>>, rooms: HashMap<String, HashSet<u64>>,
} }
impl Default for ChatServer { impl Default for ChatServer {
@ -76,7 +76,7 @@ impl Default for ChatServer {
impl ChatServer { impl ChatServer {
/// Send message to all users in the room /// Send message to all users in the room
fn send_message(&self, room: &str, message: &str, skip_id: usize) { fn send_message(&self, room: &str, message: &str, skip_id: u64) {
if let Some(sessions) = self.rooms.get(room) { if let Some(sessions) = self.rooms.get(room) {
for id in sessions { for id in sessions {
if *id != skip_id { if *id != skip_id {
@ -100,7 +100,7 @@ impl Actor for ChatServer {
/// ///
/// Register new session and assign unique id to this session /// Register new session and assign unique id to this session
impl Handler<Connect> for ChatServer { impl Handler<Connect> for ChatServer {
type Result = usize; type Result = u64;
fn handle(&mut self, msg: Connect, _: &mut Context<Self>) -> Self::Result { fn handle(&mut self, msg: Connect, _: &mut Context<Self>) -> Self::Result {
println!("Someone joined"); println!("Someone joined");
@ -109,7 +109,7 @@ impl Handler<Connect> for ChatServer {
self.send_message("main", "Someone joined", 0); self.send_message("main", "Someone joined", 0);
// register session with random id // register session with random id
let id = rand::rng().random::<usize>(); let id = rand::rng().random::<u64>();
self.sessions.insert(id, msg.addr); self.sessions.insert(id, msg.addr);
// auto join session to main room // auto join session to main room

View File

@ -27,7 +27,7 @@ pub struct Message(pub String);
/// `ChatSession` actor is responsible for tcp peer communications. /// `ChatSession` actor is responsible for tcp peer communications.
pub struct ChatSession { pub struct ChatSession {
/// unique session id /// unique session id
id: usize, id: u64,
/// this is address of chat server /// this is address of chat server
addr: Addr<ChatServer>, addr: Addr<ChatServer>,
/// Client must send ping at least once per 10 seconds, otherwise we drop /// Client must send ping at least once per 10 seconds, otherwise we drop