Skip to content

Commit

Permalink
Modify visitor and implement 'void' and 'echo' visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
fxpineau committed Dec 12, 2023
1 parent c8208db commit 40da839
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 52 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ is converted into:
* [ ] Make a JS/Wasm library.
* [X] Add everywhere builders, getters and setters like in the `FillFrameRefposFlavor` structure to make a clean API.
* [X] Create a visitor.
+ [X] implement an 'empty' visitor
+ [ ] implement an 'echo' visitor
+ [X] implement an `empty` visitor
+ [X] implement an `echo` visitor
+ [X] implement a `stcs2moc` visitor (available in [MOCLibRust](https://github.com/cds-astro/cds-moc-rust))
* [ ] Implement `fold` to avoid too wide lines.
* [ ] Support for STC XML serialization/deserialization?

Expand Down
9 changes: 2 additions & 7 deletions src/space/common/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,10 @@ pub trait RegionParams: Sized + Display {

#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
pub struct AllSkyParams;
/*impl AllSkyParams {
pub fn new() -> Self {
Self
}
}*/
impl RegionParams for AllSkyParams {
const REGION_NAME: &'static str = "AllSky";
fn accept<V: CompoundVisitor>(&self, visitor: &mut V) -> Result<V::Value, V::Error> {
visitor.visit_allsky(self)
visitor.visit_allsky()
}
fn parse<'a, E: NomErr<'a>>(input: &'a str) -> IResult<&'a str, Self, E> {
Ok((input, Self))
Expand Down Expand Up @@ -172,7 +167,7 @@ impl RegionParams for CircleParams {
}
fn parse<'a, E: NomErr<'a>>(input: &'a str) -> IResult<&'a str, Self, E> {
map(
many_m_n(2, usize::MAX, preceded(multispace1, double)),
many_m_n(3, usize::MAX, preceded(multispace1, double)),
|pos_and_radius| {
let (radius, pos) = pos_and_radius.split_last().unwrap();
let radius = *radius;
Expand Down
24 changes: 14 additions & 10 deletions src/space/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ impl<T: ExprArgs> Expression<T> {
self.post.pixsize()
}

fn accept_gen<V: SpaceVisitor>(&self) -> Result<V::Value, V::Error> {
V::C::new_from_params(&self.pre, &self.post)
fn accept_gen<V: SpaceVisitor>(
&self,
visitor: &mut V,
) -> Result<<V::C as CompoundVisitor>::Value, V::Error> {
visitor
.new_compound_visitor(&self.pre, &self.post)
.and_then(|mut visitor| self.args.accept(&mut visitor))
}

Expand Down Expand Up @@ -258,9 +262,9 @@ impl Expression<NotArgs> {
pub fn elem(&self) -> &RegionOrExpr {
self.args.elem()
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_not(res))
}
}
Expand All @@ -278,9 +282,9 @@ impl Expression<DifferenceArgs> {
pub fn right_elem(&self) -> &RegionOrExpr {
self.args.right_elem()
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_difference(res))
}
}
Expand All @@ -291,9 +295,9 @@ impl Expression<UnionArgs> {
pub fn elems(&self) -> &[RegionOrExpr] {
self.args.elems()
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_union(res))
}
}
Expand All @@ -304,9 +308,9 @@ impl Expression<IntersectionArgs> {
pub fn elems(&self) -> &[RegionOrExpr] {
self.args.elems()
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_intersection(res))
}
}
32 changes: 18 additions & 14 deletions src/space/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ impl<T: RegionParams> Geometry<T> {
self.post.pixsize()
}

fn accept_gen<V: SpaceVisitor>(&self) -> Result<V::Value, V::Error> {
V::C::new_from_params(&self.pre, &self.post)
fn accept_gen<V: SpaceVisitor>(
&self,
visitor: &mut V,
) -> Result<<V::C as CompoundVisitor>::Value, V::Error> {
visitor
.new_compound_visitor(&self.pre, &self.post)
.and_then(|mut visitor| self.params.accept(&mut visitor))
}

Expand Down Expand Up @@ -273,9 +277,9 @@ impl Geometry<AllSkyParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::Spher2)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_allsky(res))
}
}
Expand All @@ -292,9 +296,9 @@ impl Geometry<CircleParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::Spher2)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_circle(res))
}
}
Expand Down Expand Up @@ -326,9 +330,9 @@ impl Geometry<EllipseParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::Spher2)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_ellipse(res))
}
}
Expand All @@ -346,9 +350,9 @@ impl Geometry<BoxParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::Spher2)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_box(res))
}
}
Expand All @@ -363,9 +367,9 @@ impl Geometry<PolygonParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::Spher2)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_polygon(res))
}
}
Expand All @@ -380,9 +384,9 @@ impl Geometry<ConvexParams> {
pub fn flavor_or_default(&self) -> Flavor {
self.pre.flavor().unwrap_or(Flavor::UnitSpher)
}
pub fn accept<V: SpaceVisitor>(&self, visitor: V) -> Result<V::Value, V::Error> {
pub fn accept<V: SpaceVisitor>(&self, mut visitor: V) -> Result<V::Value, V::Error> {
self
.accept_gen::<V>()
.accept_gen(&mut visitor)
.and_then(|res| visitor.visit_convex(res))
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/visitor/impls/donothing.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::{
error::Error,
fmt::{Display, Formatter},
};

use crate::{
redshift::{RedshiftInterval, RedshiftValue},
space::{
common::{
region::{AllSkyParams, BoxParams, CircleParams, ConvexParams, EllipseParams, PolygonParams},
region::{BoxParams, CircleParams, ConvexParams, EllipseParams, PolygonParams},
FillFrameRefposFlavor, FromPosToVelocity,
},
position::Position,
Expand Down Expand Up @@ -94,14 +96,7 @@ impl CompoundVisitor for VoidVisitor {
type Value = ();
type Error = VoidError;

fn new_from_params(
_fill_fram_refpos_flavor: &FillFrameRefposFlavor,
_from_pos_to_velocity: &FromPosToVelocity,
) -> Result<Self, Self::Error> {
Ok(Self)
}

fn visit_allsky(&mut self, _allsky: &AllSkyParams) -> Result<Self::Value, Self::Error> {
fn visit_allsky(&mut self) -> Result<Self::Value, Self::Error> {
Ok(())
}

Expand Down Expand Up @@ -157,6 +152,14 @@ impl SpaceVisitor for VoidVisitor {
type Error = VoidError;
type C = Self;

fn new_compound_visitor(
&self,
_fill_fram_refpos_flavor: &FillFrameRefposFlavor,
_from_pos_to_velocity: &FromPosToVelocity,
) -> Result<Self, Self::Error> {
Ok(Self)
}

fn visit_position_simple(self, _position: &Position) -> Result<Self::Value, Self::Error> {
Ok(())
}
Expand Down
Loading

0 comments on commit 40da839

Please sign in to comment.