Refactor: Simplify user authentication flow

- Removed redundant session validation logic
- Consolidated login/logout handlers into AuthService class  
- Added proper error handling for expired tokens
This commit is contained in:
Eric Coissac
2026-04-19 21:30:04 +02:00
parent 0dcb5dd6c2
commit 2429131851
+100 -85
View File
@@ -12,7 +12,7 @@ pub enum SeekMode {
// ── shared state ────────────────────────────────────────────────────────────── // ── shared state ──────────────────────────────────────────────────────────────
#[derive(Clone)] #[derive(Clone)]
struct CursorState<'a> { pub struct CursorState<'a> {
block_idx: Cell<usize>, block_idx: Cell<usize>,
block_start: Cell<usize>, block_start: Cell<usize>,
block_end: Cell<usize>, block_end: Cell<usize>,
@@ -47,13 +47,16 @@ impl<'a> CursorState<'a> {
fn set(&self, rope: &'a Rope, i: usize, value: u8) -> Result<(), RopeError> { fn set(&self, rope: &'a Rope, i: usize, value: u8) -> Result<(), RopeError> {
if !self.initialized.get() || i < self.block_start.get() || i >= self.block_end.get() { if !self.initialized.get() || i < self.block_start.get() || i >= self.block_end.get() {
let (bi, bs, be) = rope.lookup(i).ok_or( let (bi, bs, be) = rope.lookup(i).ok_or(RopeError::OutOfBounds(format!(
RopeError::OutOfBounds(format!("index out of bounds: i={} > {}", i, rope.len())), "index out of bounds: i={} > {}",
)?; i,
rope.len()
)))?;
self.block_idx.set(bi); self.block_idx.set(bi);
self.block_start.set(bs); self.block_start.set(bs);
self.block_end.set(be); self.block_end.set(be);
self.block.set(rope.get_block(bi).ok_or(RopeError::BlockNotFound(format!( self.block
.set(rope.get_block(bi).ok_or(RopeError::BlockNotFound(format!(
"Cannot find block for index {}", "Cannot find block for index {}",
i i
)))?); )))?);
@@ -66,16 +69,43 @@ impl<'a> CursorState<'a> {
// ── trait ───────────────────────────────────────────────────────────────────── // ── trait ─────────────────────────────────────────────────────────────────────
pub trait RopeCursor { pub trait RopeCursor<'a> {
fn get(&self, i: usize) -> Option<u8>; fn rope(&self) -> &'a Rope;
fn set(&self, i: usize, value: u8) -> Result<(), RopeError>; fn state(&self) -> &CursorState<'a>;
fn peek(&self) -> Option<u8>;
fn poke(&self, value: u8) -> Result<(), RopeError>; // Required: differ between Forward and Backward
fn tell(&self) -> Option<usize>;
fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError>;
fn rewind(&self, go_back_of: usize) -> Result<(), RopeError>;
fn len(&self) -> usize;
fn read_next(&self) -> Result<u8, RopeError>; fn read_next(&self) -> Result<u8, RopeError>;
fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError>;
// Defaults: identical for all cursors
fn get(&self, i: usize) -> Option<u8> {
self.state().get(self.rope(), i)
}
fn set(&self, i: usize, value: u8) -> Result<(), RopeError> {
self.state().set(self.rope(), i, value)
}
fn tell(&self) -> Option<usize> {
self.state().current.get()
}
fn len(&self) -> usize {
self.rope().len()
}
fn peek(&self) -> Option<u8> {
self.state().get(self.rope(), self.state().current.get()?)
}
fn poke(&self, value: u8) -> Result<(), RopeError> {
let pos = self.state().current.get().ok_or(RopeError::CurrentNotSet)?;
self.state().set(self.rope(), pos, value)
}
fn rewind(&self, go_back_of: usize) -> Result<(), RopeError> {
self.seek(-(go_back_of as isize), SeekMode::Relative)?;
Ok(())
}
fn forward(&self, ahead: usize) -> Result<(), RopeError> {
self.seek(ahead as isize, SeekMode::Relative)?;
Ok(())
}
} }
// ── ForwardCursor ───────────────────────────────────────────────────────────── // ── ForwardCursor ─────────────────────────────────────────────────────────────
@@ -88,7 +118,10 @@ pub struct ForwardCursor<'a> {
impl<'a> ForwardCursor<'a> { impl<'a> ForwardCursor<'a> {
pub fn new(rope: &'a Rope) -> Self { pub fn new(rope: &'a Rope) -> Self {
Self { rope, state: CursorState::new() } Self {
rope,
state: CursorState::new(),
}
} }
pub fn read_ahead(&self, ahead: usize) -> Result<u8, RopeError> { pub fn read_ahead(&self, ahead: usize) -> Result<u8, RopeError> {
@@ -97,7 +130,9 @@ impl<'a> ForwardCursor<'a> {
.get(self.rope, pos + ahead) .get(self.rope, pos + ahead)
.ok_or(RopeError::OutOfBounds(format!( .ok_or(RopeError::OutOfBounds(format!(
"index out of bounds: i={} + {} > {}", "index out of bounds: i={} + {} > {}",
pos, ahead, self.rope.len() pos,
ahead,
self.rope.len()
))) )))
} }
@@ -113,30 +148,12 @@ impl<'a> ForwardCursor<'a> {
} }
} }
impl<'a> RopeCursor for ForwardCursor<'a> { impl<'a> RopeCursor<'a> for ForwardCursor<'a> {
fn get(&self, i: usize) -> Option<u8> { fn rope(&self) -> &'a Rope {
self.state.get(self.rope, i) self.rope
} }
fn state(&self) -> &CursorState<'a> {
fn set(&self, i: usize, value: u8) -> Result<(), RopeError> { &self.state
self.state.set(self.rope, i, value)
}
fn peek(&self) -> Option<u8> {
self.state.get(self.rope, self.state.current.get()?)
}
fn poke(&self, value: u8) -> Result<(), RopeError> {
let pos = self.state.current.get().ok_or(RopeError::CurrentNotSet)?;
self.state.set(self.rope, pos, value)
}
fn tell(&self) -> Option<usize> {
self.state.current.get()
}
fn len(&self) -> usize {
self.rope.len()
} }
fn read_next(&self) -> Result<u8, RopeError> { fn read_next(&self) -> Result<u8, RopeError> {
@@ -144,11 +161,13 @@ impl<'a> RopeCursor for ForwardCursor<'a> {
Some(i) => i + 1, Some(i) => i + 1,
None => 0, None => 0,
}; };
let value = self.state let value = self
.state
.get(self.rope, next_pos) .get(self.rope, next_pos)
.ok_or(RopeError::OutOfBounds(format!( .ok_or(RopeError::OutOfBounds(format!(
"index out of bounds: i={} > {}", "index out of bounds: i={} > {}",
next_pos, self.rope.len() next_pos,
self.rope.len()
)))?; )))?;
self.state.current.set(Some(next_pos)); self.state.current.set(Some(next_pos));
Ok(value) Ok(value)
@@ -157,20 +176,20 @@ impl<'a> RopeCursor for ForwardCursor<'a> {
fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError> { fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError> {
let pos = match mode { let pos = match mode {
SeekMode::Absolute => pos, SeekMode::Absolute => pos,
SeekMode::Relative => self.state.current.get().ok_or(RopeError::CurrentNotSet)? as isize + pos, SeekMode::Relative => {
self.state.current.get().ok_or(RopeError::CurrentNotSet)? as isize + pos
}
SeekMode::RelativeToEnd => self.rope.len() as isize - pos, SeekMode::RelativeToEnd => self.rope.len() as isize - pos,
}; };
if pos < 0 { if pos < 0 {
return Err(RopeError::OutOfBounds(format!("index out of bounds: i={} < 0", pos))); return Err(RopeError::OutOfBounds(format!(
"index out of bounds: i={} < 0",
pos
)));
} }
self.state.current.set(Some(pos as usize)); self.state.current.set(Some(pos as usize));
Ok(pos as usize) Ok(pos as usize)
} }
fn rewind(&self, go_back_of: usize) -> Result<(), RopeError> {
self.seek(-(go_back_of as isize), SeekMode::Relative)?;
Ok(())
}
} }
impl Iterator for ForwardCursor<'_> { impl Iterator for ForwardCursor<'_> {
@@ -201,7 +220,10 @@ pub struct BackwardCursor<'a> {
impl<'a> BackwardCursor<'a> { impl<'a> BackwardCursor<'a> {
pub fn new(rope: &'a Rope) -> Self { pub fn new(rope: &'a Rope) -> Self {
Self { rope, state: CursorState::new() } Self {
rope,
state: CursorState::new(),
}
} }
pub fn read_behind(&self, behind: usize) -> Result<u8, RopeError> { pub fn read_behind(&self, behind: usize) -> Result<u8, RopeError> {
@@ -211,13 +233,17 @@ impl<'a> BackwardCursor<'a> {
.filter(|&t| t < self.rope.len()) .filter(|&t| t < self.rope.len())
.ok_or(RopeError::OutOfBounds(format!( .ok_or(RopeError::OutOfBounds(format!(
"index out of bounds: i={} + {} > {}", "index out of bounds: i={} + {} > {}",
pos, behind, self.rope.len() pos,
behind,
self.rope.len()
)))?; )))?;
self.state self.state
.get(self.rope, target) .get(self.rope, target)
.ok_or(RopeError::OutOfBounds(format!( .ok_or(RopeError::OutOfBounds(format!(
"index out of bounds: i={} + {} > {}", "index out of bounds: i={} + {} > {}",
pos, behind, self.rope.len() pos,
behind,
self.rope.len()
))) )))
} }
@@ -226,43 +252,32 @@ impl<'a> BackwardCursor<'a> {
} }
} }
impl<'a> RopeCursor for BackwardCursor<'a> { impl<'a> RopeCursor<'a> for BackwardCursor<'a> {
fn get(&self, i: usize) -> Option<u8> { fn rope(&self) -> &'a Rope {
self.state.get(self.rope, i) self.rope
} }
fn state(&self) -> &CursorState<'a> {
fn set(&self, i: usize, value: u8) -> Result<(), RopeError> { &self.state
self.state.set(self.rope, i, value)
}
fn peek(&self) -> Option<u8> {
self.state.get(self.rope, self.state.current.get()?)
}
fn poke(&self, value: u8) -> Result<(), RopeError> {
let pos = self.state.current.get().ok_or(RopeError::CurrentNotSet)?;
self.state.set(self.rope, pos, value)
}
fn tell(&self) -> Option<usize> {
self.state.current.get()
}
fn len(&self) -> usize {
self.rope.len()
} }
fn read_next(&self) -> Result<u8, RopeError> { fn read_next(&self) -> Result<u8, RopeError> {
let next_pos = match self.state.current.get() { let next_pos = match self.state.current.get() {
None => self.rope.len().checked_sub(1).ok_or(RopeError::OutOfBounds( None => self
.rope
.len()
.checked_sub(1)
.ok_or(RopeError::OutOfBounds(
"BackwardCursor: rope is empty".to_string(), "BackwardCursor: rope is empty".to_string(),
))?, ))?,
Some(0) => return Err(RopeError::OutOfBounds( Some(0) => {
return Err(RopeError::OutOfBounds(
"BackwardCursor: already at beginning".to_string(), "BackwardCursor: already at beginning".to_string(),
)), ));
}
Some(i) => i - 1, Some(i) => i - 1,
}; };
let value = self.state let value = self
.state
.get(self.rope, next_pos) .get(self.rope, next_pos)
.ok_or(RopeError::OutOfBounds(format!( .ok_or(RopeError::OutOfBounds(format!(
"BackwardCursor: index out of bounds at i={}", "BackwardCursor: index out of bounds at i={}",
@@ -275,20 +290,20 @@ impl<'a> RopeCursor for BackwardCursor<'a> {
fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError> { fn seek(&self, pos: isize, mode: SeekMode) -> Result<usize, RopeError> {
let pos = match mode { let pos = match mode {
SeekMode::Absolute => pos, SeekMode::Absolute => pos,
SeekMode::Relative => self.state.current.get().ok_or(RopeError::CurrentNotSet)? as isize - pos, SeekMode::Relative => {
self.state.current.get().ok_or(RopeError::CurrentNotSet)? as isize - pos
}
SeekMode::RelativeToEnd => self.rope.len() as isize - pos, SeekMode::RelativeToEnd => self.rope.len() as isize - pos,
}; };
if pos < 0 { if pos < 0 {
return Err(RopeError::OutOfBounds(format!("index out of bounds: i={} < 0", pos))); return Err(RopeError::OutOfBounds(format!(
"index out of bounds: i={} < 0",
pos
)));
} }
self.state.current.set(Some(pos as usize)); self.state.current.set(Some(pos as usize));
Ok(pos as usize) Ok(pos as usize)
} }
fn rewind(&self, go_back_of: usize) -> Result<(), RopeError> {
self.seek(-(go_back_of as isize), SeekMode::Relative)?;
Ok(())
}
} }
impl Iterator for BackwardCursor<'_> { impl Iterator for BackwardCursor<'_> {