Skip to content

Commit

Permalink
Name change
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 16, 2023
1 parent 0e84d4f commit e6bda9c
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct RawStyle {
pub bibliography: Option<Bibliography>,
/// The style's settings. Must be present in dependent styles.
#[serde(flatten)]
pub independant_settings: Option<IndependentStyleSettings>,
pub independent_settings: Option<IndependentStyleSettings>,
/// Reusable formatting rules.
#[serde(rename = "macro", default)]
pub macros: Vec<CslMacro>,
Expand All @@ -137,25 +137,13 @@ struct RawStyle {
}

impl RawStyle {
/// Create a style from an XML file.
pub fn from_xml(xml: &str) -> Result<Self, quick_xml::de::DeError> {
let de = &mut deserializer(xml);
let style = RawStyle::deserialize(de)?;
Ok(style)
}

/// Retrieve the link to the parent style for dependent styles.
pub fn parent_link(&self) -> Option<&InfoLink> {
self.info
.link
.iter()
.find(|link| link.rel == InfoLinkRel::IndependentParent)
}

/// Check if the style is dependent.
pub fn is_dependent(&self) -> bool {
self.independant_settings.is_none() && self.citation.is_none()
}
}

/// An independent CSL style.
Expand All @@ -172,13 +160,21 @@ pub struct IndependentStyle {
/// How bibliographies are displayed.
pub bibliography: Option<Bibliography>,
/// The style's settings. Must be present in dependent styles.
pub independant_settings: IndependentStyleSettings,
pub settings: IndependentStyleSettings,
/// Reusable formatting rules.
pub macros: Vec<CslMacro>,
/// Override localized strings.
pub locale: Vec<Locale>,
}

impl IndependentStyle {
/// Create a style from an XML file.
pub fn from_xml(xml: &str) -> Result<Self, quick_xml::de::DeError> {
let de = &mut deserializer(xml);
IndependentStyle::deserialize(de)
}
}

impl<'de> Deserialize<'de> for IndependentStyle {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
Expand Down Expand Up @@ -209,6 +205,14 @@ pub struct DependentStyle {
pub parent_link: InfoLink,
}

impl DependentStyle {
/// Create a style from an XML file.
pub fn from_xml(xml: &str) -> Result<Self, quick_xml::de::DeError> {
let de = &mut deserializer(xml);
DependentStyle::deserialize(de)
}
}

impl<'de> Deserialize<'de> for DependentStyle {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
Expand All @@ -235,6 +239,14 @@ pub enum Style {
Dependent(DependentStyle),
}

impl Style {
/// Create a style from an XML file.
pub fn from_xml(xml: &str) -> Result<Self, quick_xml::de::DeError> {
let de = &mut deserializer(xml);
Style::deserialize(de)
}
}

impl<'de> Deserialize<'de> for Style {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
Expand All @@ -250,14 +262,14 @@ impl TryFrom<RawStyle> for Style {
fn try_from(value: RawStyle) -> Result<Self, Self::Error> {
let has_bibliography = value.bibliography.is_some();
if let Some(citation) = value.citation {
if let Some(settings) = value.independant_settings {
if let Some(settings) = value.independent_settings {
Ok(Self::Independent(IndependentStyle {
info: value.info,
default_locale: value.default_locale,
version: value.version,
citation,
bibliography: value.bibliography,
independant_settings: settings,
settings,
macros: value.macros,
locale: value.locale,
}))
Expand Down Expand Up @@ -301,12 +313,6 @@ impl fmt::Display for StyleValidationError {
}
}

/// Deserialize a CSL style from an XML string.
pub fn deserialize_csl_str(s: &str) -> Result<Style, quick_xml::DeError> {
let de = &mut deserializer(s);
Style::deserialize(de)
}

fn deserializer(xml: &str) -> Deserializer<SliceReader<'_>> {
let mut style_deserializer = Deserializer::from_str(xml);
style_deserializer.event_buffer_size(EVENT_BUFFER_SIZE);
Expand Down

0 comments on commit e6bda9c

Please sign in to comment.