Skip to content

Commit

Permalink
Add Level type
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagner84 committed Jul 10, 2023
1 parent a4a428d commit 5bb23b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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

0 comments on commit 5bb23b1

Please sign in to comment.