update literal parser

This commit is contained in:
Robin Appelman 2021-09-09 16:32:05 +02:00
commit b388c2ccf0
4 changed files with 43 additions and 14 deletions

View file

@ -8,7 +8,6 @@ use std::path::PathBuf;
use thiserror::Error;
pub use nc::{parse, parse_glob};
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct Config {
@ -77,7 +76,7 @@ type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
pub enum Error {
#[error("Error while parsing php literal: {0:#}")]
#[error("{0:#}")]
Php(PhpParseError),
#[error("Provided config file doesn't seem to be a nextcloud config file: {0:#}")]
NotAConfig(#[from] NotAConfigError),
@ -91,20 +90,13 @@ pub enum Error {
NoUrl,
}
#[derive(Debug)]
#[derive(Debug, Error)]
#[error("Error while parsing '{path}':\n{err}")]
pub struct PhpParseError {
err: php_literal_parser::ParseError,
source: String,
path: PathBuf,
}
impl Display for PhpParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let err = self.err.clone().with_source(&self.source);
write!(f, "{}", err)
}
}
#[derive(Debug, Error)]
pub enum DbError {
#[error("unsupported database type {0}")]

View file

@ -69,7 +69,6 @@ fn parse_php(path: impl AsRef<Path>) -> Result<Value> {
Error::Php(PhpParseError {
err,
path: path.as_ref().into(),
source: php.into(),
})
})
}
@ -835,3 +834,23 @@ fn test_parse_config_sqlite_default_db() {
&config.database,
);
}
#[test]
fn test_parse_config_nested_array() {
let config = config_from_file("tests/configs/nested_array.php");
assert_eq!("https://cloud.example.com", config.nextcloud_url);
assert_eq!("oc_", config.database_prefix);
assert_debug_equal(
&Database::MySql {
database: "nextcloud".to_string(),
username: "nextcloud".to_string(),
password: "secret".to_string(),
connect: DbConnect::Tcp {
host: "127.0.0.1".to_string(),
port: 3306,
},
ssl_options: SslOptions::Disabled,
},
&config.database,
);
}