Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply new error type to enforcer,model,config... #39

Merged
merged 2 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/adapter/file_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::adapter::Adapter;
use crate::errors::CasbinError;
use crate::error::Error;
use crate::model::Model;

use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::io::{Error as IoError, ErrorKind};

use crate::Result;

Expand Down Expand Up @@ -44,8 +45,12 @@ impl Adapter for FileAdapter {
}

fn save_policy(&self, m: &mut Model) -> Result<()> {
if self.file_path == "" {
return Err(CasbinError::new("save policy failed, file path is empty").into());
if self.file_path.is_empty() {
return Err(Error::IoError(IoError::new(
ErrorKind::Other,
"save policy failed, file path is empty",
))
.into());
}

let mut tmp = String::new();
Expand Down Expand Up @@ -91,7 +96,7 @@ impl Adapter for FileAdapter {
}

fn load_policy_line(line: String, m: &mut Model) {
if line == "" || line.chars().nth(0).unwrap() == '#' {
if line.is_empty() || line.starts_with('#') {
return;
}
let tokens: Vec<String> = line.split(',').map(|x| x.trim().to_string()).collect();
Expand Down
51 changes: 28 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
use crate::error::Error;
use crate::Result;

use std::collections::HashMap;

use std::convert::AsRef;
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, Cursor};
use std::io::{BufReader, Cursor, Error as IoError, ErrorKind};
use std::path::Path;

const DEFAULT_SECTION: &str = "default";
const DEFAULT_COMMENT: &str = "#";
const DEFAULT_COMMENT_SEM: &str = ";";
const DEFAULT_MULTI_LINE_SEPARATOR: &str = "\\";

pub(crate) struct Config {
pub data: HashMap<String, HashMap<String, String>>,
data: HashMap<String, HashMap<String, String>>,
}

impl Config {
pub fn new(path: &str) -> Self {
pub(crate) fn from_file<P: AsRef<Path>>(p: P) -> Result<Self> {
let mut c = Config {
data: HashMap::new(),
};

c.parse(path);
c
c.parse(p)?;
Ok(c)
}

pub(crate) fn from_text(text: &str) -> Self {
pub(crate) fn from_str<S: AsRef<str>>(s: S) -> Result<Self> {
let mut c = Config {
data: HashMap::new(),
};

c.parse_buffer(&mut BufReader::new(Cursor::new(text.as_bytes())));
c
c.parse_buffer(&mut BufReader::new(Cursor::new(s.as_ref().as_bytes())))?;
Ok(c)
}

fn parse(&mut self, path: &str) {
let mut f = File::open(path).expect("read config failed");
fn parse<P: AsRef<Path>>(&mut self, p: P) -> Result<()> {
let mut f = File::open(p)?;
let mut c = Vec::new();
f.read_to_end(&mut c).expect("error while reading config");
f.read_to_end(&mut c)?;

let mut reader: BufReader<Cursor<&[u8]>> = BufReader::new(Cursor::new(&c));
self.parse_buffer(&mut reader);
self.parse_buffer(&mut reader)
}

fn parse_buffer(&mut self, reader: &mut BufReader<Cursor<&[u8]>>) {
fn parse_buffer(&mut self, reader: &mut BufReader<Cursor<&[u8]>>) -> Result<()> {
let mut section = String::new();

loop {
let mut line = String::new();
let bytes = reader
.read_line(&mut line)
.expect("read config line failed");
let bytes = reader.read_line(&mut line)?;
if bytes == 0 {
// EOF reached
break;
break Ok(());
}
line = line.trim().to_string();
if line.is_empty()
Expand All @@ -67,9 +70,7 @@ impl Config {
line = line[..line.len() - 1].trim_end().to_string();

let mut inner_line = String::new();
let inner_bytes = reader
.read_line(&mut inner_line)
.expect("read config line failed");
let inner_bytes = reader.read_line(&mut inner_line)?;
if inner_bytes == 0 {
break;
}
Expand Down Expand Up @@ -99,7 +100,11 @@ impl Config {
.collect();

if option_val.len() != 2 {
panic!("parse content error, line={}", line);
return Err(Error::IoError(IoError::new(
ErrorKind::Other,
format!("parse content error, line={}", line),
))
.into());
}

self.add_config(
Expand Down Expand Up @@ -199,7 +204,7 @@ mod tests {

#[test]
fn test_get() {
let mut config = Config::new("examples/testini.ini");
let mut config = Config::from_file("examples/testini.ini").unwrap();

assert_eq!(Some(true), config.get_bool("debug"));
assert_eq!(Some(64), config.get_int("math::math.i64"));
Expand Down Expand Up @@ -269,7 +274,7 @@ mod tests {
math.f64 = 64.1
"#;

let mut config = Config::from_text(text);
let mut config = Config::from_str(text).unwrap();

assert_eq!(Some(true), config.get_bool("debug"));
assert_eq!(Some(64), config.get_int("math::math.i64"));
Expand Down
Loading