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

Add Level type #655

Merged
merged 1 commit into from
Jul 10, 2023
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
46 changes: 46 additions & 0 deletions pica-record/src/level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::str::FromStr;

use thiserror::Error;

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum Level {
#[default]
Main,
Local,
Copy,
}

/// An error that can occur when parsing PICA+ level.
#[derive(Error, PartialEq, Eq, Debug)]
#[error("{0}")]
pub struct ParseLevelError(String);

impl FromStr for Level {
type Err = ParseLevelError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"main" => Ok(Self::Main),
"local" => Ok(Self::Local),
"copy" => Ok(Self::Copy),
_ => Err(ParseLevelError(format!("invalid level '{}'", s))),
}
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_from_str() -> anyhow::Result<()> {
assert_eq!("main".parse::<Level>().unwrap(), Level::Main);
assert_eq!("local".parse::<Level>().unwrap(), Level::Local);
assert_eq!("copy".parse::<Level>().unwrap(), Level::Copy);
assert!("master".parse::<Level>().is_err());
assert!("foo".parse::<Level>().is_err());

Ok(())
}
}
2 changes: 2 additions & 0 deletions pica-record/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
mod error;
mod field;
pub mod io;
mod level;
mod occurrence;
mod record;
mod subfield;
mod tag;

pub use error::ParsePicaError;
pub use field::{Field, FieldMut, FieldRef};
pub use level::Level;
pub use occurrence::{Occurrence, OccurrenceMut, OccurrenceRef};
pub use record::{
ByteRecord, Record, RecordMut, RecordRef, StringRecord,
Expand Down
Loading