Skip to content

Commit

Permalink
Expose parse_format_string to external users to allow for attemptin…
Browse files Browse the repository at this point in the history
…g to typecast raw data (PR #5)
  • Loading branch information
tjol authored Dec 11, 2023
2 parents ea86d5e + 2cc9da9 commit 10ca0af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod format;
mod parser;

pub use format::Printf;
use parser::{parse_format_string, FormatElement};
pub use parser::{parse_format_string, FormatElement};
pub use parser::{ConversionSpecifier, ConversionType, NumericParam};

/// Error type
Expand Down
27 changes: 23 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{PrintfError, Result};

#[derive(Debug, Clone)]
pub(crate) enum FormatElement {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FormatElement {
Verbatim(String),
Format(ConversionSpecifier),
}

/// Parsed printf conversion specifier
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConversionSpecifier {
/// flag `#`: use `0x`, etc?
pub alt_form: bool,
Expand Down Expand Up @@ -69,7 +69,26 @@ pub enum ConversionType {
PercentSign,
}

pub(crate) fn parse_format_string(fmt: &str) -> Result<Vec<FormatElement>> {
/// Parses a string to a vector of [FormatElement]
///
/// Takes a printf-style format string `fmt`
///
/// use sprintf::{parse_format_string, FormatElement, ConversionType, ConversionSpecifier, NumericParam};
/// let fmt = "Hello %#06x";
/// let parsed = parse_format_string(fmt).unwrap();
/// assert_eq!(parsed[0], FormatElement::Verbatim("Hello ".to_owned()));
/// assert_eq!(parsed[1], FormatElement::Format(ConversionSpecifier {
/// alt_form: true,
/// zero_pad: true,
/// left_adj: false,
/// space_sign: false,
/// force_sign: false,
/// width: NumericParam::Literal(6),
/// precision: NumericParam::Literal(6),
/// conversion_type: ConversionType::HexIntLower,
/// }));
///
pub fn parse_format_string(fmt: &str) -> Result<Vec<FormatElement>> {
// find the first %
let mut res = Vec::new();
let parts: Vec<&str> = fmt.splitn(2, '%').collect();
Expand Down

0 comments on commit 10ca0af

Please sign in to comment.