#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![no_std]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
pub use stun_types as stun;
pub mod attribute;
pub mod channel;
pub mod message;
pub mod tcp;
pub mod transmit;
pub use stun_proto::Instant;
pub mod prelude {
pub use crate::transmit::DelayedTransmitBuild;
}
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use stun_types::message::{LongTermCredentials, LongTermKeyCredentials};
pub use stun_types::{AddressFamily, TransportType};
pub fn debug_init() {
attribute::attributes_init();
message::debug_init();
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TurnCredentials {
username: String,
password: String,
}
impl From<TurnCredentials> for LongTermCredentials {
fn from(value: TurnCredentials) -> Self {
LongTermCredentials::new(value.username, value.password)
}
}
impl TurnCredentials {
pub fn into_long_term_credentials(self, realm: &str) -> LongTermKeyCredentials {
LongTermKeyCredentials::new(self.username, self.password, realm.to_string())
}
pub fn new(username: &str, password: &str) -> Self {
Self {
username: username.to_owned(),
password: password.to_owned(),
}
}
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
}
#[cfg(test)]
mod tests {
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Layer;
pub fn test_init_log() -> DefaultGuard {
crate::debug_init();
let level_filter = std::env::var("TURN_LOG")
.or(std::env::var("RUST_LOG"))
.ok()
.and_then(|var| var.parse::<tracing_subscriber::filter::Targets>().ok())
.unwrap_or(
tracing_subscriber::filter::Targets::new().with_default(tracing::Level::TRACE),
);
let registry = tracing_subscriber::registry().with(
tracing_subscriber::fmt::layer()
.with_file(true)
.with_line_number(true)
.with_level(true)
.with_target(false)
.with_test_writer()
.with_filter(level_filter),
);
tracing::subscriber::set_default(registry)
}
}