Compare commits

..

3 Commits

Author SHA1 Message Date
Tom Alexander
47674a6907
Merge branch 'initial_getters'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
rust-foreign-document-test Build rust-foreign-document-test has succeeded
2023-09-29 20:40:45 -04:00
Tom Alexander
5d1582be4d
Remove multi_field_getter_iter.
This was written because I originally intended to make the fields of the ast node types entirely private, but that made constructing them tedious so they are pub(crate) which coincidentally also allows them to be used by the iterator.
2023-09-29 20:40:31 -04:00
Tom Alexander
dae10c2eef
Initial work for exposing getters and hiding the fields of the ast nodes.
Ultimately this is about semver and exposing a stable interface while allowing the internal representation to change. The fields are still pub(crate) to make constructing the types easier inside this crate, which should be fine because we can refactor the code inside this crate whenever the internal structure changes.
2023-09-29 20:40:31 -04:00
3 changed files with 58 additions and 8 deletions

View File

@ -1,5 +1,7 @@
use super::element::Element;
use super::lesser_element::TableCell;
use super::macros::ref_getter;
use super::macros::simple_getter;
use super::Keyword;
use super::Object;
use super::StandardProperties;
@ -23,14 +25,14 @@ pub type IndentationLevel = u16;
#[derive(Debug)]
pub struct PlainListItem<'s> {
pub source: &'s str,
pub indentation: IndentationLevel,
pub bullet: &'s str,
pub counter: Option<PlainListItemCounter>,
pub checkbox: Option<(CheckboxType, &'s str)>,
pub tag: Vec<Object<'s>>,
pub pre_blank: PlainListItemPreBlank,
pub children: Vec<Element<'s>>,
pub(crate) source: &'s str,
pub(crate) indentation: IndentationLevel,
pub(crate) bullet: &'s str,
pub(crate) counter: Option<PlainListItemCounter>,
pub(crate) checkbox: Option<(CheckboxType, &'s str)>,
pub(crate) tag: Vec<Object<'s>>,
pub(crate) pre_blank: PlainListItemPreBlank,
pub(crate) children: Vec<Element<'s>>,
}
pub type PlainListItemCounter = u16;
@ -157,3 +159,29 @@ impl<'s> StandardProperties<'s> for TableRow<'s> {
self.source
}
}
impl<'s> PlainListItem<'s> {
simple_getter!(get_indentation_level, indentation, IndentationLevel);
simple_getter!(
/// Get the bullet
///
/// Example output: "1. "
get_bullet,
bullet,
&'s str
);
simple_getter!(get_counter, counter, Option<PlainListItemCounter>);
simple_getter!(get_pre_blank, pre_blank, PlainListItemPreBlank);
ref_getter!(get_tag, tag, Vec<Object<'s>>);
ref_getter!(get_children, children, Vec<Element<'s>>);
pub fn get_checkbox(&self) -> Option<&'s str> {
self.checkbox.as_ref().map(|(_, checkbox)| *checkbox)
}
pub fn get_checkbox_type(&self) -> Option<&CheckboxType> {
self.checkbox
.as_ref()
.map(|(checkbox_type, _)| checkbox_type)
}
}

21
src/types/macros.rs Normal file
View File

@ -0,0 +1,21 @@
// TODO: Would be nice if I didn't have to specify a function name but it looks like concat_idents!() cannot be used to create an ident.
// TODO: Find out if proc macros could do this easier (for example, parsing out the field type)
macro_rules! simple_getter {
($(#[$meta:meta])* $funcname: ident, $field:ident, $fieldtype:ty) => {
$(#[$meta])*
pub fn $funcname(&self) -> $fieldtype {
self.$field
}
};
}
pub(crate) use simple_getter;
macro_rules! ref_getter {
($(#[$meta:meta])* $funcname: ident, $field:ident, $fieldtype:ty) => {
$(#[$meta])*
pub fn $funcname(&self) -> &$fieldtype {
&self.$field
}
};
}
pub(crate) use ref_getter;

View File

@ -3,6 +3,7 @@ mod element;
mod get_standard_properties;
mod greater_element;
mod lesser_element;
mod macros;
mod object;
mod source;
mod standard_properties;