-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metadata: file struct to read and write to files, reading bag-info.txt
need to do write need to write some tests
- Loading branch information
Showing
3 changed files
with
124 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod file; | ||
|
||
#[cfg(feature = "date")] | ||
use jiff::civil::Date; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use super::{Metadata, MetadataError}; | ||
use std::path::Path; | ||
use std::str::FromStr; | ||
use tokio::fs; | ||
use tokio::io::{AsyncBufReadExt, BufReader}; | ||
|
||
#[derive(Debug, PartialEq, Default)] | ||
pub struct MetadataFile(Vec<Metadata>); | ||
|
||
#[derive(thiserror::Error, Debug, PartialEq)] | ||
pub enum MetadataFileError { | ||
/// Metadata errors | ||
#[error(transparent)] | ||
Metadata(#[from] MetadataError), | ||
/// Read file error | ||
#[error("Failed to read file: `{0}`")] | ||
ReadFile(std::io::ErrorKind), | ||
} | ||
|
||
impl MetadataFile { | ||
pub async fn read(path: impl AsRef<Path>) -> Result<Self, MetadataFileError> { | ||
let file = fs::File::open(path.as_ref()) | ||
.await | ||
.map_err(|e| MetadataFileError::ReadFile(e.kind()))?; | ||
let file = BufReader::new(file); | ||
let mut lines = file.lines(); | ||
|
||
let mut tags = Vec::new(); | ||
|
||
while let Some(line) = lines | ||
.next_line() | ||
.await | ||
.map_err(|e| MetadataFileError::ReadFile(e.kind()))? | ||
{ | ||
tags.push(Metadata::from_str(&line)?); | ||
} | ||
|
||
Ok(Self(tags)) | ||
} | ||
|
||
pub async fn write(&self, path: impl AsRef<Path>) -> Result<(), std::io::Error> { | ||
let contents = self | ||
.0 | ||
.iter() | ||
.map(|tag| tag.to_string()) | ||
.collect::<Vec<_>>() | ||
.join("\n"); | ||
|
||
fs::write(path.as_ref(), contents).await | ||
} | ||
|
||
pub fn add(&mut self, tag: Metadata) { | ||
self.0.push(tag); | ||
} | ||
|
||
pub fn tags(&self) -> impl Iterator<Item = &Metadata> { | ||
self.0.iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters