I noticed, that the ep field in your fen strings only shows if a capture is actually possible.
But according to the fen definition, the ep field should be shown when a pawn moves two squares.
For example: White plays e4 on the first move, e3 should show up as ep square.
Personally I think, your solution is even better but it can lead to problems when comparing fens taken from lichess with fens taken from somewhere else. A lot of positions that are the same would show as different because one fen has the ep square included and the other doesn't.
Sources:
-
https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
Quote: En passant target square in algebraic notation. If there's no en passant target square, this is "-". If a pawn has just made a two-square move, this is the position "behind" the pawn. This is recorded regardless of whether there is a pawn in position to make an en passant capture. -
http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm
Quote:
16.1.3.4: En passant target square
The fourth field is the en passant target square. If there is no en passant target square then the single character symbol "-" appears. If there is an en passant target square then is represented by a lowercase file character immediately followed by a rank digit. Obviously, the rank digit will be "3" following a white pawn double advance (Black is the active color) or else be the digit "6" after a black pawn double advance (White being the active color).
An en passant target square is given if and only if the last move was a pawn advance of two squares. Therefore, an en passant target square field may have a square name even if there is no pawn of the opposing side that may immediately execute the en passant capture.
.
.
.
16.1.4: Examples
Here's the FEN for the starting position:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
And after the move 1. e4:
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1