mirror of
https://codeberg.org/icewind/nextcloud-config-parser.git
synced 2026-06-03 16:44:09 +02:00
optional redis
This commit is contained in:
parent
06de88ec21
commit
d9d0c397ee
3 changed files with 24 additions and 3 deletions
|
|
@ -9,7 +9,7 @@ repository = "https://github.com/icewind1991/nextcloud-config-parser"
|
||||||
documentation = "https://docs.rs/nextcloud-config-parser"
|
documentation = "https://docs.rs/nextcloud-config-parser"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
redis = { version = "0.20" }
|
redis = { version = "0.20", optional = true }
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
php-literal-parser = { version = "0.2", default-features = false }
|
php-literal-parser = { version = "0.2", default-features = false }
|
||||||
sqlx = { version = "0.5", features = ["any", "mysql", "sqlite", "postgres"], optional = true }
|
sqlx = { version = "0.5", features = ["any", "mysql", "sqlite", "postgres"], optional = true }
|
||||||
|
|
@ -19,3 +19,4 @@ sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "any", "mysql", "s
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
db-sqlx = ["sqlx"]
|
db-sqlx = ["sqlx"]
|
||||||
|
redis-connect = ["redis"]
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
mod nc;
|
mod nc;
|
||||||
|
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
use redis::{ConnectionAddr, ConnectionInfo};
|
use redis::{ConnectionAddr, ConnectionInfo};
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
@ -11,16 +13,19 @@ pub use nc::parse;
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub database: Database,
|
pub database: Database,
|
||||||
pub database_prefix: String,
|
pub database_prefix: String,
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
pub redis: RedisConfig,
|
pub redis: RedisConfig,
|
||||||
pub nextcloud_url: String,
|
pub nextcloud_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RedisConfig {
|
pub enum RedisConfig {
|
||||||
Single(ConnectionInfo),
|
Single(ConnectionInfo),
|
||||||
Cluster(Vec<ConnectionInfo>),
|
Cluster(Vec<ConnectionInfo>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
impl RedisConfig {
|
impl RedisConfig {
|
||||||
pub fn addr(&self) -> impl Iterator<Item = &ConnectionAddr> {
|
pub fn addr(&self) -> impl Iterator<Item = &ConnectionAddr> {
|
||||||
let boxed: Box<dyn Iterator<Item = &ConnectionAddr>> = match self {
|
let boxed: Box<dyn Iterator<Item = &ConnectionAddr>> = match self {
|
||||||
|
|
|
||||||
17
src/nc.rs
17
src/nc.rs
|
|
@ -1,5 +1,8 @@
|
||||||
use crate::{Config, Database, DbConnect, DbError, Error, NotAConfigError, RedisConfig, Result};
|
#[cfg(feature = "redis-connect")]
|
||||||
|
use crate::RedisConfig;
|
||||||
|
use crate::{Config, Database, DbConnect, DbError, Error, NotAConfigError, Result};
|
||||||
use php_literal_parser::Value;
|
use php_literal_parser::Value;
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
use redis::{ConnectionAddr, ConnectionInfo};
|
use redis::{ConnectionAddr, ConnectionInfo};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
@ -93,12 +96,14 @@ pub fn parse(path: impl AsRef<Path>) -> Result<Config> {
|
||||||
.clone()
|
.clone()
|
||||||
.into_string()
|
.into_string()
|
||||||
.ok_or(Error::NoUrl)?;
|
.ok_or(Error::NoUrl)?;
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
let redis = parse_redis_options(&parsed);
|
let redis = parse_redis_options(&parsed);
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
database,
|
database,
|
||||||
database_prefix,
|
database_prefix,
|
||||||
nextcloud_url,
|
nextcloud_url,
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
redis,
|
redis,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -230,11 +235,13 @@ fn split_host(host: &str) -> (&str, Option<u16>, Option<&str>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
enum RedisAddress {
|
enum RedisAddress {
|
||||||
Single(ConnectionAddr),
|
Single(ConnectionAddr),
|
||||||
Cluster(Vec<ConnectionAddr>),
|
Cluster(Vec<ConnectionAddr>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
||||||
let (redis_options, address) = if parsed["redis.cluster"].is_array() {
|
let (redis_options, address) = if parsed["redis.cluster"].is_array() {
|
||||||
let redis_options = &parsed["redis.cluster"];
|
let redis_options = &parsed["redis.cluster"];
|
||||||
|
|
@ -292,6 +299,7 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn test_redis_empty_password_none() {
|
fn test_redis_empty_password_none() {
|
||||||
let config =
|
let config =
|
||||||
php_literal_parser::from_str(r#"["redis" => ["host" => "redis", "password" => "pass"]]"#)
|
php_literal_parser::from_str(r#"["redis" => ["host" => "redis", "password" => "pass"]]"#)
|
||||||
|
|
@ -349,6 +357,7 @@ fn test_parse_config_basic() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
config.database.into(),
|
config.database.into(),
|
||||||
);
|
);
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
||||||
config.redis,
|
config.redis,
|
||||||
|
|
@ -362,6 +371,7 @@ fn test_parse_implicit_prefix() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn test_parse_empty_redis_password() {
|
fn test_parse_empty_redis_password() {
|
||||||
let config = config_from_file("tests/configs/empty_redis_password.php");
|
let config = config_from_file("tests/configs/empty_redis_password.php");
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
|
|
@ -371,6 +381,7 @@ fn test_parse_empty_redis_password() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn test_parse_full_redis() {
|
fn test_parse_full_redis() {
|
||||||
let config = config_from_file("tests/configs/full_redis.php");
|
let config = config_from_file("tests/configs/full_redis.php");
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
|
|
@ -380,6 +391,7 @@ fn test_parse_full_redis() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn test_parse_redis_socket() {
|
fn test_parse_redis_socket() {
|
||||||
let config = config_from_file("tests/configs/redis_socket.php");
|
let config = config_from_file("tests/configs/redis_socket.php");
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
|
|
@ -414,6 +426,7 @@ fn test_parse_comment_whitespace() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
config.database.into(),
|
config.database.into(),
|
||||||
);
|
);
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
||||||
config.redis,
|
config.redis,
|
||||||
|
|
@ -497,6 +510,7 @@ fn test_parse_postgres_socket_folder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
fn test_parse_redis_cluster() {
|
fn test_parse_redis_cluster() {
|
||||||
let config = config_from_file("tests/configs/redis.cluster.php");
|
let config = config_from_file("tests/configs/redis.cluster.php");
|
||||||
let mut conns = config.redis.into_vec();
|
let mut conns = config.redis.into_vec();
|
||||||
|
|
@ -540,6 +554,7 @@ fn test_parse_config_multiple() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
config.database.into(),
|
config.database.into(),
|
||||||
);
|
);
|
||||||
|
#[cfg(feature = "redis-connect")]
|
||||||
assert_debug_equal(
|
assert_debug_equal(
|
||||||
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
RedisConfig::Single(ConnectionInfo::from_str("redis://127.0.0.1").unwrap()),
|
||||||
config.redis,
|
config.redis,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue