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

feat: fix use code to adapt to latest Rust version #325

Merged
merged 6 commits into from
Feb 2, 2024
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
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 @@

#[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 @@

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 @@
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 @@
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 @@
}

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?;

Check warning on line 109 in src/adapter/file_adapter.rs

View check run for this annotation

Codecov / codecov/patch

src/adapter/file_adapter.rs#L109

Added line #L109 was not covered by tests
file.write_all(text.as_bytes()).await?;
Ok(())
}
Expand All @@ -115,7 +115,7 @@
#[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 @@

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,

Check warning on line 141 in src/adapter/file_adapter.rs

View check run for this annotation

Codecov / codecov/patch

src/adapter/file_adapter.rs#L140-L141

Added lines #L140 - L141 were not covered by tests
"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 @@
#[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 @@

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 @@
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));

Check warning on line 67 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L67

Added line #L67 was not covered by tests
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 @@
.collect();

if option_val.len() != 2 {
return Err(IoError::new(
ErrorKind::Other,
return Err(ioError::new(
ioErrorKind::Other,

Check warning on line 132 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L131-L132

Added lines #L131 - L132 were not covered by tests
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
Loading