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

Minor reorganization #216

Merged
merged 1 commit into from
Dec 21, 2021
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
16 changes: 12 additions & 4 deletions src/fontinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ use serde::de::Deserializer;
use serde::ser::{SerializeSeq, Serializer};
use serde::{Deserialize, Serialize};

use crate::shared_types::{
Bitlist, Float, Integer, IntegerOrFloat, NonNegativeInteger, NonNegativeIntegerOrFloat,
PUBLIC_OBJECT_LIBS_KEY,
};
use crate::shared_types::{IntegerOrFloat, NonNegativeIntegerOrFloat, PUBLIC_OBJECT_LIBS_KEY};
use crate::{Error, FormatVersion, Guideline, Identifier, Plist};

/// A signed integer.
pub type Integer = i32;
/// An unsigned integer.
pub type NonNegativeInteger = u32;
/// A floating-point number.
pub type Float = f64;
/// a list of ["bit numbers"].
///
/// ["bit numbers"]: https://unifiedfontobject.org/versions/ufo3/fontinfo.plist/#bit-numbers
pub type Bitlist = Vec<u8>;

/// The contents of the [`fontinfo.plist`][] file. This structure is hard-wired to the
/// available attributes in UFO version 3.
///
Expand Down
143 changes: 68 additions & 75 deletions src/shared_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,74 @@ pub struct Color {
pub alpha: f64,
}

/// A number that can be a non-negative integer or float.
///
/// It serializes to an integer if it effectively represents one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NonNegativeIntegerOrFloat(f64);

/// A number that may be either an integer or float.
///
/// It serializes to an integer if it effectively represents one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IntegerOrFloat(f64);

impl NonNegativeIntegerOrFloat {
/// Returns a new [`NonNegativeIntegerOrFloat`] with the given `value` or `None`
/// if the value is less than or equal to zero.
pub fn new(value: f64) -> Option<Self> {
if value.is_sign_positive() {
Some(NonNegativeIntegerOrFloat(value))
} else {
None
}
}

/// Returns the value.
pub fn get(&self) -> f64 {
self.0
}

/// Sets the value.
///
/// An error is raised if `value` is less than or equal to zero.
pub fn try_set(&mut self, value: f64) -> Result<(), Error> {
if value.is_sign_positive() {
self.0 = value;
Ok(())
} else {
Err(Error::ExpectedPositiveValue)
}
}

/// Returns `true` if the value is an integer.
pub fn is_integer(&self) -> bool {
(self.0 - self.round()).abs() < std::f64::EPSILON
}
}

impl IntegerOrFloat {
/// Returns a new [`IntegerOrFloat`] with the given `value`.
pub fn new(value: f64) -> Self {
IntegerOrFloat(value)
}

/// Returns the value.
pub fn get(&self) -> f64 {
self.0
}

/// Sets the value.
pub fn set(&mut self, value: f64) {
self.0 = value
}

/// Returns `true` if the value is an integer.
pub fn is_integer(&self) -> bool {
(self.0 - self.round()).abs() < std::f64::EPSILON
}
}

impl FromStr for Color {
type Err = InvalidColorString;

Expand Down Expand Up @@ -71,41 +139,6 @@ impl<'de> Deserialize<'de> for Color {
}
}

// Types used in fontinfo.plist.

pub type Integer = i32;
pub type NonNegativeInteger = u32;
pub type Float = f64;
pub type Bitlist = Vec<u8>;

/// A number that may be either an integer or float.
///
/// It serializes to an integer if it effectively represents one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IntegerOrFloat(f64);

impl IntegerOrFloat {
/// Returns a new [`IntegerOrFloat`] with the given `value`.
pub fn new(value: f64) -> Self {
IntegerOrFloat(value)
}

/// Returns the value.
pub fn get(&self) -> f64 {
self.0
}

/// Sets the value.
pub fn set(&mut self, value: f64) {
self.0 = value
}

/// Returns `true` if the value is an integer.
pub fn is_integer(&self) -> bool {
(self.0 - self.round()).abs() < std::f64::EPSILON
}
}

impl Deref for IntegerOrFloat {
type Target = f64;

Expand Down Expand Up @@ -149,46 +182,6 @@ impl<'de> Deserialize<'de> for IntegerOrFloat {
}
}

/// A number that can be a non-negative integer or float.
///
/// It serializes to an integer if it effectively represents one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NonNegativeIntegerOrFloat(f64);

impl NonNegativeIntegerOrFloat {
/// Returns a new [`NonNegativeIntegerOrFloat`] with the given `value` or `None`
/// if the value is less than or equal to zero.
pub fn new(value: f64) -> Option<Self> {
if value.is_sign_positive() {
Some(NonNegativeIntegerOrFloat(value))
} else {
None
}
}

/// Returns the value.
pub fn get(&self) -> f64 {
self.0
}

/// Sets the value.
///
/// An error is raised if `value` is less than or equal to zero.
pub fn try_set(&mut self, value: f64) -> Result<(), Error> {
if value.is_sign_positive() {
self.0 = value;
Ok(())
} else {
Err(Error::ExpectedPositiveValue)
}
}

/// Returns `true` if the value is an integer.
pub fn is_integer(&self) -> bool {
(self.0 - self.round()).abs() < std::f64::EPSILON
}
}

impl Deref for NonNegativeIntegerOrFloat {
type Target = f64;

Expand Down