summaryrefslogtreecommitdiff
path: root/src/frame.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-03 11:44:53 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-03 13:35:39 +0100
commit37a7afddfaffd44cb9bc013c9506599267e08983 (patch)
tree20e7d62d3c5418baff01a21d0406b91bf3096214 /src/frame.rs
parent56342bd972a13ffe21beaf2b87ab7eb1597704b4 (diff)
Split crates
Diffstat (limited to 'src/frame.rs')
-rw-r--r--src/frame.rs69
1 files changed, 39 insertions, 30 deletions
diff --git a/src/frame.rs b/src/frame.rs
index e85256f5..2eac6879 100644
--- a/src/frame.rs
+++ b/src/frame.rs
@@ -2,6 +2,7 @@
use std::fmt::{self, Debug, Formatter, Write};
use std::num::NonZeroUsize;
+use std::str::FromStr;
use std::sync::Arc;
use crate::font::Font;
@@ -9,7 +10,7 @@ use crate::geom::{
Abs, Align, Axes, Dir, Em, Numeric, Paint, Point, Shape, Size, Transform,
};
use crate::image::Image;
-use crate::model::{Dict, Value};
+use crate::model::{dict, Dict, Value};
use crate::util::EcoString;
/// A finished layout with elements at fixed positions.
@@ -396,7 +397,7 @@ pub struct Glyph {
pub c: char,
}
-/// A code for a natural language.
+/// An identifier for a natural language.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Lang([u8; 3], u8);
@@ -404,19 +405,6 @@ impl Lang {
/// The code for the english language.
pub const ENGLISH: Self = Self(*b"en ", 2);
- /// Construct a language from a two- or three-byte ISO 639-1/2/3 code.
- pub fn from_str(iso: &str) -> Option<Self> {
- let len = iso.len();
- if matches!(len, 2 ..= 3) && iso.is_ascii() {
- let mut bytes = [b' '; 3];
- bytes[.. len].copy_from_slice(iso.as_bytes());
- bytes.make_ascii_lowercase();
- Some(Self(bytes, len as u8))
- } else {
- None
- }
- }
-
/// Return the language code as an all lowercase string slice.
pub fn as_str(&self) -> &str {
std::str::from_utf8(&self.0[.. usize::from(self.1)]).unwrap_or_default()
@@ -432,28 +420,49 @@ impl Lang {
}
}
-/// A code for a region somewhere in the world.
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Region([u8; 2]);
+impl FromStr for Lang {
+ type Err = &'static str;
-impl Region {
- /// Construct a region from its two-byte ISO 3166-1 alpha-2 code.
- pub fn from_str(iso: &str) -> Option<Self> {
- if iso.is_ascii() {
- let mut bytes: [u8; 2] = iso.as_bytes().try_into().ok()?;
- bytes.make_ascii_uppercase();
- Some(Self(bytes))
+ /// Construct a language from a two- or three-byte ISO 639-1/2/3 code.
+ fn from_str(iso: &str) -> Result<Self, Self::Err> {
+ let len = iso.len();
+ if matches!(len, 2 ..= 3) && iso.is_ascii() {
+ let mut bytes = [b' '; 3];
+ bytes[.. len].copy_from_slice(iso.as_bytes());
+ bytes.make_ascii_lowercase();
+ Ok(Self(bytes, len as u8))
} else {
- None
+ Err("expected two or three letter language code (ISO 639-1/2/3)")
}
}
+}
+/// An identifier for a region somewhere in the world.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Region([u8; 2]);
+
+impl Region {
/// Return the region code as an all uppercase string slice.
pub fn as_str(&self) -> &str {
std::str::from_utf8(&self.0).unwrap_or_default()
}
}
+impl FromStr for Region {
+ type Err = &'static str;
+
+ /// Construct a region from its two-byte ISO 3166-1 alpha-2 code.
+ fn from_str(iso: &str) -> Result<Self, Self::Err> {
+ if iso.len() == 2 && iso.is_ascii() {
+ let mut bytes: [u8; 2] = iso.as_bytes().try_into().unwrap();
+ bytes.make_ascii_uppercase();
+ Ok(Self(bytes))
+ } else {
+ Err("expected two letter region code (ISO 3166-1 alpha-2)")
+ }
+ }
+}
+
/// A link destination.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Destination {
@@ -529,9 +538,9 @@ impl Role {
pub fn is_weak(self) -> bool {
// In Typst, all text is in a paragraph, so paragraph isn't very
// descriptive.
- match self {
- Self::Paragraph | Self::GenericBlock | Self::GenericInline => true,
- _ => false,
- }
+ matches!(
+ self,
+ Self::Paragraph | Self::GenericBlock | Self::GenericInline
+ )
}
}