-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse): add
proto::PlanVersion
parser (#165)
A parser for `proto::PlanVersion`, it parses iff the version is not missing and the version parses.
- Loading branch information
Showing
7 changed files
with
134 additions
and
6 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
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
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
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,105 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Parsing of [proto::PlanVersion]. | ||
|
||
use crate::{ | ||
parse::{context::Context, proto::Version, Parse}, | ||
proto, | ||
}; | ||
use thiserror::Error; | ||
|
||
use super::VersionError; | ||
|
||
/// A parsed [proto::PlanVersion]. | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct PlanVersion { | ||
/// The version of the plan. | ||
version: Version, | ||
} | ||
|
||
impl PlanVersion { | ||
/// Returns the version of this plan version. | ||
/// | ||
/// See [proto::PlanVersion::version]. | ||
pub fn version(&self) -> &Version { | ||
&self.version | ||
} | ||
} | ||
|
||
/// Parse errors for [proto::PlanVersion]. | ||
#[derive(Debug, Error, PartialEq)] | ||
pub enum PlanVersionError { | ||
/// Version is missing. | ||
#[error("version must be specified")] | ||
Missing, | ||
|
||
/// Version error. | ||
#[error("version must be valid")] | ||
Version(#[from] VersionError), | ||
} | ||
|
||
impl<C: Context> Parse<C> for proto::PlanVersion { | ||
type Parsed = PlanVersion; | ||
type Error = PlanVersionError; | ||
|
||
fn parse(self, ctx: &mut C) -> Result<Self::Parsed, Self::Error> { | ||
let proto::PlanVersion { version } = self; | ||
|
||
// The version is required, and must be valid. | ||
let version = version | ||
.map(|version| ctx.parse(version)) | ||
.transpose()? | ||
.ok_or(PlanVersionError::Missing)?; | ||
|
||
let plan_version = PlanVersion { version }; | ||
|
||
Ok(plan_version) | ||
} | ||
} | ||
|
||
impl From<PlanVersion> for proto::PlanVersion { | ||
fn from(plan_version: PlanVersion) -> Self { | ||
let PlanVersion { version } = plan_version; | ||
|
||
proto::PlanVersion { | ||
version: Some(version.into()), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ | ||
parse::{context::tests::Context, proto::VersionError}, | ||
version, | ||
}; | ||
|
||
#[test] | ||
fn parse() { | ||
let plan_version = proto::PlanVersion { | ||
version: Some(version::version()), | ||
}; | ||
assert!(plan_version.parse(&mut Context::default()).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn missing() { | ||
let plan_version = proto::PlanVersion::default(); | ||
assert_eq!( | ||
plan_version.parse(&mut Context::default()), | ||
Err(PlanVersionError::Missing) | ||
); | ||
} | ||
|
||
#[test] | ||
fn version_error() { | ||
let plan_version = proto::PlanVersion { | ||
version: Some(proto::Version::default()), | ||
}; | ||
assert_eq!( | ||
plan_version.parse(&mut Context::default()), | ||
Err(PlanVersionError::Version(VersionError::Missing)) | ||
); | ||
} | ||
} |
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
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
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