Skip to content

Commit

Permalink
Fix : #6
Browse files Browse the repository at this point in the history
  • Loading branch information
caofengyi authored and caofengyi committed Feb 2, 2024
1 parent ea50240 commit 081a30d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
37 changes: 19 additions & 18 deletions src/adapter/file_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@ use crate::{
model::Model,
util::parse_csv_line,
Result,

};


#[cfg(feature = "runtime-async-std")]
use async_std::{
fs::File as asyncFile,
fs::File as file,
io::prelude::*,
io::{BufReader as asyncBufReader, Error as asyncIoError, ErrorKind as asyncErrorKind},
path::Path as asyncPath,
io::{BufReader as ioBufReader, Error as ioError, ErrorKind as ioErrorKind},
path::Path as ioPath,
prelude::*,
};


#[cfg(feature = "runtime-tokio")]
use std::{
io::{Error as tokioIoError, ErrorKind as tokioErrorKind},
path::Path as tokioPath,
io::{Error as ioError, ErrorKind as ioErrorKind},
path::Path as ioPath,
};
#[cfg(feature = "runtime-tokio")]
use tokio::{
fs::File as tokioFile,
io::{AsyncBufReadExt, AsyncWriteExt, BufReader as tokioBufReader},
fs::File as file,
io::{AsyncBufReadExt, AsyncWriteExt, BufReader as ioBufReader},
};

use async_trait::async_trait;

use std::convert::AsRef;
use async_trait::async_trait;
use std::fmt::Write;

pub struct FileAdapter<P> {
Expand All @@ -43,7 +44,7 @@ type LoadFilteredPolicyFileHandler<'a> =

impl<P> FileAdapter<P>
where
P: AsRef<tokioPath> + Send + Sync,
P: AsRef<ioPath> + Send + Sync,
{
pub fn new(p: P) -> FileAdapter<P> {
FileAdapter {
Expand All @@ -64,8 +65,8 @@ where
m: &mut dyn Model,
handler: LoadPolicyFileHandler,
) -> Result<()> {
let f = tokioFile::open(&self.file_path).await?;
let mut lines = tokioBufReader::new(f).lines();
let f = file::open(&self.file_path).await?;
let mut lines = ioBufReader::new(f).lines();
#[cfg(feature = "runtime-async-std")]
while let Some(line) = lines.next().await {
handler(line?, m)
Expand All @@ -85,8 +86,8 @@ where
filter: Filter<'a>,
handler: LoadFilteredPolicyFileHandler<'a>,
) -> Result<bool> {
let f = tokioFile::open(&self.file_path).await?;
let mut lines = tokioBufReader::new(f).lines();
let f = file::open(&self.file_path).await?;
let mut lines = ioBufReader::new(f).lines();

let mut is_filtered = false;
#[cfg(feature = "runtime-async-std")]
Expand All @@ -107,7 +108,7 @@ where
}

async fn save_policy_file(&self, text: String) -> Result<()> {
let mut file = tokioFile::create(&self.file_path).await?;
let mut file = file::create(&self.file_path).await?;
file.write_all(text.as_bytes()).await?;
Ok(())
}
Expand All @@ -116,7 +117,7 @@ where
#[async_trait]
impl<P> Adapter for FileAdapter<P>
where
P: AsRef<tokioPath> + Send + Sync,
P: AsRef<ioPath> + Send + Sync,
{
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> {
self.is_filtered = false;
Expand All @@ -138,8 +139,8 @@ where

async fn save_policy(&mut self, m: &mut dyn Model) -> Result<()> {
if self.file_path.as_ref().as_os_str().is_empty() {
return Err(tokioIoError::new(
tokioErrorKind::Other,
return Err(ioError::new(
ioErrorKind::Other,
"save policy failed, file path is empty",
)
.into());
Expand Down
28 changes: 14 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ use crate::Result;
#[cfg(feature = "runtime-async-std")]
use async_std::{
io::prelude::*,
io::{BufReader, Cursor, Error as IoError, ErrorKind},
io::{BufReader as ioBufReader, Cursor, Error as ioError, ioErrorKind},

Check failure on line 6 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

unresolved import `async_std::io::ioErrorKind`

Check warning on line 6 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

unused import: `Cursor`

Check failure on line 6 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

unresolved import `async_std::io::ioErrorKind`

Check warning on line 6 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

unused import: `Cursor`
};

#[cfg(all(feature = "runtime-async-std", not(target_arch = "wasm32")))]
use async_std::{fs::File, path::Path};
use async_std::{fs::File as file, path::Path as ioPath};

#[cfg(feature = "runtime-tokio")]
use std::{io::Cursor as tokioCursor, path::Path as tokioPath};
use std::{io::Cursor as tokioCursor, path::Path as ioPath};
#[cfg(feature = "runtime-tokio")]
use tokio::io::{
AsyncBufReadExt, AsyncReadExt, BufReader as tokioBufReader, Error as tokioIoError, ErrorKind as tokioErrorKind,
AsyncBufReadExt, AsyncReadExt, BufReader as ioBufReader, Error as ioError, ErrorKind as ioErrorKind,
};

#[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))]
use tokio::fs::File as tokioFile;
use tokio::fs::File as file;

use std::collections::HashMap;

Expand All @@ -32,7 +32,7 @@ pub(crate) struct Config {

impl Config {
#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn from_file<P: AsRef<tokioPath>>(p: P) -> Result<Self> {
pub(crate) async fn from_file<P: AsRef<ioPath>>(p: P) -> Result<Self> {
let mut c = Config {
data: HashMap::new(),
};
Expand All @@ -46,25 +46,25 @@ impl Config {
data: HashMap::new(),
};

c.parse_buffer(&mut tokioBufReader::new(tokioCursor::new(s.as_ref().as_bytes())))
c.parse_buffer(&mut ioBufReader::new(tokioCursor::new(s.as_ref().as_bytes())))

Check failure on line 49 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

failed to resolve: use of undeclared crate or module `tokioCursor`

Check failure on line 49 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

failed to resolve: use of undeclared crate or module `tokioCursor`
.await?;
Ok(c)
}

#[cfg(not(target_arch = "wasm32"))]
async fn parse<P: AsRef<tokioPath>>(&mut self, p: P) -> Result<()> {
let mut f = tokioFile::open(p).await?;
async fn parse<P: AsRef<ioPath>>(&mut self, p: P) -> Result<()> {
let mut f = file::open(p).await?;
let mut c = Vec::new();
f.read_to_end(&mut c).await?;

let mut reader: tokioBufReader<tokioCursor<&[u8]>> =
tokioBufReader::new(tokioCursor::new(&c));
let mut reader: ioBufReader<tokioCursor<&[u8]>> =

Check failure on line 60 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

cannot find type `tokioCursor` in this scope

Check failure on line 60 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

cannot find type `tokioCursor` in this scope
ioBufReader::new(tokioCursor::new(&c));

Check failure on line 61 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

failed to resolve: use of undeclared crate or module `tokioCursor`

Check failure on line 61 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

failed to resolve: use of undeclared crate or module `tokioCursor`
self.parse_buffer(&mut reader).await
}

async fn parse_buffer(

Check failure on line 65 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

return type references an anonymous lifetime, which is not constrained by the fn input types

Check failure on line 65 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

return type references an anonymous lifetime, which is not constrained by the fn input types
&mut self,
reader: &mut tokioBufReader<tokioCursor<&[u8]>>,
reader: &mut ioBufReader<tokioCursor<&[u8]>>,

Check failure on line 67 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, stable)

cannot find type `tokioCursor` in this scope

Check failure on line 67 in src/config.rs

View workflow job for this annotation

GitHub Actions / Auto Build CI (ubuntu-latest, beta)

cannot find type `tokioCursor` in this scope
) -> Result<()> {
let mut section = String::new();

Expand Down Expand Up @@ -122,8 +122,8 @@ impl Config {
.collect();

if option_val.len() != 2 {
return Err(tokioIoError::new(
tokioErrorKind::Other,
return Err(ioError::new(
ioErrorKind::Other,
format!("parse content error, line={}", line),
)
.into());
Expand Down
6 changes: 3 additions & 3 deletions src/model/default_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use parking_lot::RwLock;
use ritelinked::{LinkedHashMap, LinkedHashSet};

#[cfg(all(feature = "runtime-async-std", not(target_arch = "wasm32")))]
use async_std::path::Path;
use async_std::path::Path as ioPath;

#[cfg(feature = "runtime-tokio")]
use std::path::Path as tokioPath;
use std::path::Path as ioPath;

use std::{collections::HashMap, sync::Arc};

Expand All @@ -28,7 +28,7 @@ pub struct DefaultModel {

impl DefaultModel {
#[cfg(not(target_arch = "wasm32"))]
pub async fn from_file<P: AsRef<tokioPath>>(p: P) -> Result<DefaultModel> {
pub async fn from_file<P: AsRef<ioPath>>(p: P) -> Result<DefaultModel> {
let cfg = Config::from_file(p).await?;

let mut model = DefaultModel::default();
Expand Down

0 comments on commit 081a30d

Please sign in to comment.