fancy error formatting

This commit is contained in:
Robin Appelman 2021-05-03 19:15:56 +02:00
commit 87dd4c570b
4 changed files with 35 additions and 8 deletions

View file

@ -18,5 +18,7 @@ sqlx = { version = "0.5", features = ["any", "mysql", "sqlite", "postgres"], opt
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "any", "mysql", "sqlite", "postgres"] } sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "any", "mysql", "sqlite", "postgres"] }
[features] [features]
default = ["span-colors"]
span-colors = ["php-literal-parser/colors"]
db-sqlx = ["sqlx"] db-sqlx = ["sqlx"]
redis-connect = ["redis"] redis-connect = ["redis"]

View file

@ -1,8 +1,12 @@
use nextcloud_config_parser::{parse, Error}; use nextcloud_config_parser::parse;
fn main() -> Result<(), Error> { fn main() {
let config = parse("tests/configs/basic.php")?; let config = match parse("tests/configs/basic.php") {
Ok(config) => config,
Err(err) => {
eprintln!("{}", err);
return;
}
};
dbg!(config); dbg!(config);
Ok(())
} }

View file

@ -8,6 +8,7 @@ use std::path::PathBuf;
use thiserror::Error; use thiserror::Error;
pub use nc::parse; pub use nc::parse;
use std::fmt::{Display, Formatter};
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Config {
@ -75,7 +76,7 @@ type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("Error while parsing php literal: {0:#}")] #[error("Error while parsing php literal: {0:#}")]
Php(#[from] php_literal_parser::ParseError), Php(PhpParseError),
#[error("Provided config file doesn't seem to be a nextcloud config file: {0:#}")] #[error("Provided config file doesn't seem to be a nextcloud config file: {0:#}")]
NotAConfig(#[from] NotAConfigError), NotAConfig(#[from] NotAConfigError),
#[error("Failed to read config file")] #[error("Failed to read config file")]
@ -90,6 +91,20 @@ pub enum Error {
NoUrl, NoUrl,
} }
#[derive(Debug)]
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)] #[derive(Debug, Error)]
pub enum DbError { pub enum DbError {
#[error("unsupported database type {0}")] #[error("unsupported database type {0}")]

View file

@ -1,6 +1,6 @@
#[cfg(feature = "redis-connect")] #[cfg(feature = "redis-connect")]
use crate::RedisConfig; use crate::RedisConfig;
use crate::{Config, Database, DbConnect, DbError, Error, NotAConfigError, Result}; use crate::{Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError, Result};
use php_literal_parser::Value; use php_literal_parser::Value;
#[cfg(feature = "redis-connect")] #[cfg(feature = "redis-connect")]
use redis::{ConnectionAddr, ConnectionInfo}; use redis::{ConnectionAddr, ConnectionInfo};
@ -54,7 +54,13 @@ fn parse_php(path: impl AsRef<Path>) -> Result<Value> {
))); )));
} }
}; };
Ok(php_literal_parser::from_str(php)?) php_literal_parser::from_str(php).map_err(|err| {
Error::Php(PhpParseError {
err,
path: path.as_ref().into(),
source: php.into(),
})
})
} }
fn merge_configs(input: Vec<(PathBuf, Value)>) -> Result<Value> { fn merge_configs(input: Vec<(PathBuf, Value)>) -> Result<Value> {