Skip to content

Commit fe3f2e7

Browse files
authored
refactor: centralize constants into per-crate constants.rs files (#63)
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent dfb88aa commit fe3f2e7

File tree

27 files changed

+93
-69
lines changed

27 files changed

+93
-69
lines changed

rsworkspace/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rsworkspace/crates/acp-nats-agent/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl std::fmt::Display for DispatchError {
6161
}
6262
}
6363

64-
const DEFAULT_OPERATION_TIMEOUT: Duration = Duration::from_secs(30);
64+
use crate::constants::DEFAULT_OPERATION_TIMEOUT;
6565

6666
pub struct AgentSideNatsConnection<N> {
6767
nats: N,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use std::time::Duration;
2+
3+
pub const DEFAULT_OPERATION_TIMEOUT: Duration = Duration::from_secs(30);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mod connection;
2+
pub mod constants;
23

34
pub use connection::{AgentSideNatsConnection, ConnectionError};

rsworkspace/crates/acp-nats-ws/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use acp_nats::{AcpPrefix, AcpPrefixError, Config, NatsConfig};
22
use clap::Parser;
3-
use std::net::{IpAddr, Ipv4Addr};
3+
use std::net::IpAddr;
44
use trogon_std::env::ReadEnv;
55

6-
const DEFAULT_HOST: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
7-
const DEFAULT_PORT: u16 = 8080;
6+
use crate::constants::{DEFAULT_HOST, DEFAULT_PORT};
87

98
#[derive(Parser, Debug)]
109
#[command(name = "acp-nats-ws")]
@@ -50,6 +49,7 @@ pub fn apply_timeout_overrides<E: ReadEnv>(mut ws: WsConfig, env_provider: &E) -
5049
#[cfg(test)]
5150
mod tests {
5251
use super::*;
52+
use std::net::Ipv4Addr;
5353
use trogon_std::env::InMemoryEnv;
5454

5555
fn config_from_env(env: &InMemoryEnv) -> WsConfig {

rsworkspace/crates/acp-nats-ws/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::sync::watch;
99
use tracing::{error, info, warn};
1010
use trogon_std::time::SystemClock;
1111

12-
const DUPLEX_BUFFER_SIZE: usize = 64 * 1024;
12+
use crate::constants::DUPLEX_BUFFER_SIZE;
1313

1414
/// Handles a single WebSocket connection by bridging it to NATS via ACP.
1515
pub async fn handle<N>(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::net::{IpAddr, Ipv4Addr};
2+
3+
pub const DEFAULT_HOST: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
4+
pub const DEFAULT_PORT: u16 = 8080;
5+
pub const DUPLEX_BUFFER_SIZE: usize = 64 * 1024;
6+
pub const THREAD_NAME: &str = "acp-ws-local";

rsworkspace/crates/acp-nats-ws/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod config;
22
mod connection;
3+
mod constants;
34
mod upgrade;
45

56
use tokio::sync::{mpsc, watch};
@@ -82,7 +83,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8283
#[cfg(coverage)]
8384
fn main() {}
8485

85-
const THREAD_NAME: &str = "acp-ws-local";
86+
use constants::THREAD_NAME;
8687

8788
/// Runs a single-threaded tokio runtime with a
8889
/// `LocalSet`. All WebSocket connections are processed here because the ACP

rsworkspace/crates/acp-nats/src/acp_prefix.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
1010
use std::sync::Arc;
1111

12+
use crate::constants::MAX_PREFIX_LENGTH;
1213
use crate::nats::token;
1314
use crate::subject_token_violation::SubjectTokenViolation;
1415

15-
const MAX_PREFIX_LENGTH: usize = 128;
16-
1716
/// Error returned when [`AcpPrefix`] validation fails.
1817
#[derive(Debug, Clone, PartialEq)]
1918
pub struct AcpPrefixError(pub SubjectTokenViolation);

rsworkspace/crates/acp-nats/src/agent/bridge.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::cell::RefCell;
2-
use std::time::Duration;
32

43
use crate::config::Config;
54
use crate::nats::{
@@ -30,13 +29,7 @@ use super::{
3029
set_session_mode, set_session_model,
3130
};
3231

33-
/// Delay before publishing `session.ready` to NATS.
34-
///
35-
/// The `Agent` trait returns the response value *before* the transport layer
36-
/// serializes and writes it to the client. Without a delay the spawned task
37-
/// could publish `session.ready` before the client has received the
38-
/// `session/new` response, violating the ordering guarantee.
39-
const SESSION_READY_DELAY: Duration = Duration::from_millis(100);
32+
use crate::constants::SESSION_READY_DELAY;
4033

4134
pub struct Bridge<N, C: GetElapsed> {
4235
pub(crate) nats: N,

0 commit comments

Comments
 (0)