Skip to content

Commit

Permalink
Merge pull request #80 from kyoto7250/feature/issue-44
Browse files Browse the repository at this point in the history
add: set timeout_second and move limit_size to config.toml
  • Loading branch information
kyoto7250 authored Jun 26, 2024
2 parents dfcf8dc + 40c1b8a commit 4480a87
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ The location of the file depends on your OS:

Sample config.toml file is `examples/key_bind.ron`:

### table config

The location of the file depends on your OS:

- macOS: `$HOME/.config/zhobo/table_config.toml`
- Linux: `$HOME/.config/zhobo/table_config.toml`
- Windows: `%APPDATA%/zhobo/tabel_config.toml`

```bash
limit_size = 200
```

## contribution

Contributions are welcome.
Expand Down
2 changes: 2 additions & 0 deletions examples/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ type = "mysql"
user = "root"
host = "localhost"
port = 3306
limit_size = 400
timeout_second = 5

[[conn]]
type = "mysql"
Expand Down
1 change: 0 additions & 1 deletion examples/table_config.toml

This file was deleted.

17 changes: 13 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,26 @@ impl App {
Some(Box::new(
MySqlPool::new(
conn.database_url()?.as_str(),
self.config.table_config.limit_size,
conn.limit_size,
conn.timeout_second,
)
.await?,
))
} else if conn.is_postgres() {
Some(Box::new(
PostgresPool::new(
conn.database_url()?.as_str(),
self.config.table_config.limit_size,
conn.limit_size,
conn.timeout_second,
)
.await?,
))
} else {
Some(Box::new(
SqlitePool::new(
conn.database_url()?.as_str(),
self.config.table_config.limit_size,
conn.limit_size,
conn.timeout_second,
)
.await?,
))
Expand Down Expand Up @@ -333,7 +336,13 @@ impl App {
}

if let Some(index) = self.record_table.table.selected_row.selected() {
if index.saturating_add(1) % self.config.table_config.limit_size == 0
let limit_size =
if let Some(connection) = self.connections.selected_connection() {
connection.limit_size
} else {
200
};
if index.saturating_add(1) % limit_size == 0
&& index >= self.record_table.table.rows.len() - 1
{
if let Some((database, table)) =
Expand Down
75 changes: 31 additions & 44 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ pub struct CliConfig {
/// Set the key bind file
#[structopt(long, short, global = true)]
key_bind_path: Option<std::path::PathBuf>,

/// Set the table viewer config
#[structopt(long, short, global = true)]
table_config_path: Option<std::path::PathBuf>,
}

#[derive(Debug, Deserialize, Clone)]
Expand All @@ -40,8 +36,6 @@ pub struct Config {
pub key_config: KeyConfig,
#[serde(default)]
pub log_level: LogLevel,
#[serde(default)]
pub table_config: TableConfig,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -77,10 +71,11 @@ impl Default for Config {
password: None,
database: None,
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
}],
key_config: KeyConfig::default(),
log_level: LogLevel::default(),
table_config: TableConfig::default(),
}
}
}
Expand All @@ -96,18 +91,18 @@ pub struct Connection {
password: Option<String>,
unix_domain_socket: Option<std::path::PathBuf>,
pub database: Option<String>,
#[serde(default = "default_limit_size")]
pub limit_size: usize,
#[serde(default = "default_timeout_second")]
pub timeout_second: u64,
}

#[derive(Debug, Deserialize, Clone)]
#[cfg_attr(test, derive(Serialize, PartialEq))]
pub struct TableConfig {
pub limit_size: usize,
fn default_limit_size() -> usize {
200
}

impl Default for TableConfig {
fn default() -> Self {
Self { limit_size: 200 }
}
fn default_timeout_second() -> u64 {
5
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -214,51 +209,26 @@ impl Config {
get_app_config_path()?.join("key_bind.ron")
};

let table_config_path = if let Some(table_config_path) = &config.table_config_path {
table_config_path.clone()
} else {
get_app_config_path()?.join("table_config.toml")
};

let table_config = Config::_load_table_config(table_config_path)?;
if let Ok(file) = File::open(config_path) {
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
let config: Result<ReadConfig, toml::de::Error> = toml::from_str(&contents);
match config {
Ok(config) => return Ok(Config::build(config, key_bind_path, table_config)),
Ok(config) => return Ok(Config::build(config, key_bind_path)),
Err(e) => panic!("fail to parse connection config file: {}", e),
}
}

Ok(Config {
table_config,
..Default::default()
})
}

fn _load_table_config(table_config_path: PathBuf) -> anyhow::Result<TableConfig> {
if let Ok(file) = File::open(table_config_path) {
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
let table_config: Result<TableConfig, toml::de::Error> = toml::from_str(&contents);
match table_config {
Ok(config) => return Ok(config),
Err(e) => panic!("fail to parse table config: {}", e),
}
}
Ok(TableConfig::default())
Ok(Config::default())
}

fn build(read_config: ReadConfig, key_bind_path: PathBuf, table_config: TableConfig) -> Self {
fn build(read_config: ReadConfig, key_bind_path: PathBuf) -> Self {
let key_bind = KeyBind::load(key_bind_path).unwrap();
Config {
conn: read_config.conn,
log_level: read_config.log_level,
key_config: KeyConfig::from(key_bind),
table_config,
}
}
}
Expand Down Expand Up @@ -470,7 +440,6 @@ mod test {
let cli_config = CliConfig {
config_path: Some(Path::new("examples/config.toml").to_path_buf()),
key_bind_path: Some(Path::new("examples/key_bind.ron").to_path_buf()),
table_config_path: Some(Path::new("examples/table_config.toml").to_path_buf()),
};

assert_eq!(Config::new(&cli_config).is_ok(), true);
Expand All @@ -489,6 +458,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

let mysql_result = mysql_conn.database_url().unwrap();
Expand All @@ -507,6 +478,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

let postgres_result = postgres_conn.database_url().unwrap();
Expand All @@ -525,6 +498,8 @@ mod test {
password: None,
database: None,
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

let sqlite_result = sqlite_conn.database_url().unwrap();
Expand Down Expand Up @@ -564,6 +539,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

assert_eq!(
Expand All @@ -587,6 +564,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

assert_eq!(
Expand All @@ -609,6 +588,8 @@ mod test {
password: None,
database: None,
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

let sqlite_result = sqlite_conn.database_url().unwrap();
Expand All @@ -628,6 +609,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

assert_eq!(
Expand All @@ -651,6 +634,8 @@ mod test {
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

assert_eq!(
Expand All @@ -673,6 +658,8 @@ mod test {
password: None,
database: None,
unix_domain_socket: None,
limit_size: 200,
timeout_second: 5,
};

let sqlite_result = sqlite_conn.database_url().unwrap();
Expand Down
8 changes: 6 additions & 2 deletions src/database/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ pub struct MySqlPool {
}

impl MySqlPool {
pub async fn new(database_url: &str, limit_size: usize) -> anyhow::Result<Self> {
pub async fn new(
database_url: &str,
limit_size: usize,
timeout_second: u64,
) -> anyhow::Result<Self> {
Ok(Self {
pool: MySqlPoolOptions::new()
.acquire_timeout(Duration::from_secs(5))
.acquire_timeout(Duration::from_secs(timeout_second))
.connect(database_url)
.await?,
limit_size,
Expand Down
8 changes: 6 additions & 2 deletions src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ pub struct PostgresPool {
}

impl PostgresPool {
pub async fn new(database_url: &str, limit_size: usize) -> anyhow::Result<Self> {
pub async fn new(
database_url: &str,
limit_size: usize,
timeout_second: u64,
) -> anyhow::Result<Self> {
Ok(Self {
pool: PgPoolOptions::new()
.acquire_timeout(Duration::from_secs(5))
.acquire_timeout(Duration::from_secs(timeout_second))
.connect(database_url)
.await?,
limit_size,
Expand Down
8 changes: 6 additions & 2 deletions src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ pub struct SqlitePool {
}

impl SqlitePool {
pub async fn new(database_url: &str, limit_size: usize) -> anyhow::Result<Self> {
pub async fn new(
database_url: &str,
limit_size: usize,
timeout_second: u64,
) -> anyhow::Result<Self> {
Ok(Self {
pool: SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs(5))
.acquire_timeout(Duration::from_secs(timeout_second))
.connect(database_url)
.await?,
limit_size,
Expand Down

0 comments on commit 4480a87

Please sign in to comment.