#![allow(dead_code)]
use brlapi::{BrlApiError, Connection, ConnectionSettings, TtyMode};
pub fn try_connect() -> Result<Connection, BrlApiError> {
Connection::open()
}
pub fn try_connect_with_settings(settings: &ConnectionSettings) -> Result<Connection, BrlApiError> {
Connection::open_with_settings(Some(settings))
}
pub fn localhost_settings() -> ConnectionSettings {
ConnectionSettings::localhost()
}
pub fn ipv4_settings() -> ConnectionSettings {
ConnectionSettings::for_host("127.0.0.1")
}
pub fn with_connection<F, R>(test_fn: F) -> Option<R>
where
F: FnOnce(Connection) -> R,
{
match try_connect() {
Ok(conn) => {
println!("Connection established for test");
Some(test_fn(conn))
}
Err(e) => {
println!("No connection available for test: {e}");
None
}
}
}
pub fn with_tty_mode<F, R>(test_fn: F) -> Option<R>
where
F: FnOnce(&Connection) -> Result<R, BrlApiError>,
{
with_connection(|conn| {
match TtyMode::try_from(&conn) {
Ok(_tty_mode) => {
println!("TTY mode entered for test");
let result = test_fn(&conn);
result
}
Err(e) => {
println!("TTY mode not available for test: {e}");
Err(e)
}
}
})
.and_then(|r| r.ok())
}
pub fn assert_reasonable_result<T: std::fmt::Debug>(
result: Result<T, BrlApiError>,
operation_name: &str,
) -> Option<T> {
match result {
Ok(value) => {
println!("{operation_name}: Success");
Some(value)
}
Err(BrlApiError::OperationNotSupported)
| Err(BrlApiError::ConnectionRefused)
| Err(BrlApiError::TTYBusy)
| Err(BrlApiError::DeviceBusy) => {
println!("{operation_name}: Expected error: {}", result.unwrap_err());
None
}
Err(e) => {
println!("{operation_name}: Unexpected error: {e}");
None
}
}
}
pub mod test_data {
pub fn sample_texts() -> Vec<&'static str> {
vec![
"Hello BrlAPI",
"Test message",
"", "This is a very long text that exceeds typical braille display width",
"Unicode: 测试 العربية",
]
}
pub fn cursor_positions() -> Vec<i32> {
vec![
brlapi_sys::BRLAPI_CURSOR_LEAVE,
brlapi_sys::BRLAPI_CURSOR_OFF as i32,
0,
5,
10,
]
}
}