Skip to content

Commit

Permalink
feat: fix use code to adapt to latest Rust version (#325)
Browse files Browse the repository at this point in the history
* Fix #6

* fix #6

* Fix : #6

* Fix : #6

* fix #6

* Update README.md

---------

Co-authored-by: caofengyi <[email protected]>
Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent 7a6bedb commit 1dd39a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
36 changes: 18 additions & 18 deletions src/adapter/file_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ use crate::{

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

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

use async_trait::async_trait;

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

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

impl<P> FileAdapter<P>
where
P: AsRef<Path> + Send + Sync,
P: AsRef<ioPath> + Send + Sync,
{
pub fn new(p: P) -> FileAdapter<P> {
FileAdapter {
Expand All @@ -63,8 +63,8 @@ where
m: &mut dyn Model,
handler: LoadPolicyFileHandler,
) -> Result<()> {
let f = File::open(&self.file_path).await?;
let mut lines = BufReader::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 @@ -84,8 +84,8 @@ where
filter: Filter<'a>,
handler: LoadFilteredPolicyFileHandler<'a>,
) -> Result<bool> {
let f = File::open(&self.file_path).await?;
let mut lines = BufReader::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 @@ -106,7 +106,7 @@ where
}

async fn save_policy_file(&self, text: String) -> Result<()> {
let mut file = File::create(&self.file_path).await?;
let mut file = file::create(&self.file_path).await?;
file.write_all(text.as_bytes()).await?;
Ok(())
}
Expand All @@ -115,7 +115,7 @@ where
#[async_trait]
impl<P> Adapter for FileAdapter<P>
where
P: AsRef<Path> + Send + Sync,
P: AsRef<ioPath> + Send + Sync,
{
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> {
self.is_filtered = false;
Expand All @@ -137,8 +137,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(IoError::new(
ErrorKind::Other,
return Err(ioError::new(
ioErrorKind::Other,
"save policy failed, file path is empty",
)
.into());
Expand Down
36 changes: 21 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ 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 as ioCursor, Error as ioError,
ErrorKind as ioErrorKind,
},
};

#[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, path::Path};
use std::{io::Cursor as ioCursor, path::Path as ioPath};
#[cfg(feature = "runtime-tokio")]
use tokio::io::{
AsyncBufReadExt, AsyncReadExt, BufReader, Error as IoError, ErrorKind,
AsyncBufReadExt, AsyncReadExt, BufReader as ioBufReader, Error as ioError,
ErrorKind as ioErrorKind,
};

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

use std::collections::HashMap;

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

impl Config {
#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn from_file<P: AsRef<Path>>(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 +50,27 @@ impl Config {
data: HashMap::new(),
};

c.parse_buffer(&mut BufReader::new(Cursor::new(s.as_ref().as_bytes())))
.await?;
c.parse_buffer(&mut ioBufReader::new(ioCursor::new(
s.as_ref().as_bytes(),
)))
.await?;
Ok(c)
}

#[cfg(not(target_arch = "wasm32"))]
async fn parse<P: AsRef<Path>>(&mut self, p: P) -> Result<()> {
let mut f = File::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: BufReader<Cursor<&[u8]>> =
BufReader::new(Cursor::new(&c));
let mut reader: ioBufReader<ioCursor<&[u8]>> =
ioBufReader::new(ioCursor::new(&c));
self.parse_buffer(&mut reader).await
}

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

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

if option_val.len() != 2 {
return Err(IoError::new(
ErrorKind::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;
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<Path>>(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 1dd39a0

Please sign in to comment.